Func_Set-OneDriveContent.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
################################################################################
# Author : Antony Onipko
# Copyright : (c) 2016 Antony Onipko. All rights reserved.
################################################################################
# This work is licensed under the
# Creative Commons Attribution-ShareAlike 4.0 International License.
# To view a copy of this license, visit
# https://creativecommons.org/licenses/by-sa/4.0/
################################################################################

Function Set-OneDriveContent {
    <#
        .SYNOPSIS
        Uploads a new file, or update the contents of an existing OneDrive file.
         
        .EXAMPLE
        Set-OneDriveContent -Path "Documents" -Source 'C:\Temp\test.pdf' -Force
 
        .EXAMPLE
        Set-OneDriveContent -Path "Documents/aNewDirectory/" -Source 'C:\Temp\test.pdf'
 
        .EXAMPLE
        Set-OneDriveContent -Path "Documents/another-name.pdf" -Source 'C:\Temp\test.pdf'
 
        .EXAMPLE
        Set-OneDriveContent -ItemId "85B75A4CE0397EE!1450" -Source 'C:\Temp\test.pdf'
 
        .NOTES
        Add a '/' to the end of the path if you're uploading to a directory that does not exist; otherwise, the path leaf is considered the remote file name.
    #>

    [CmdletBinding(DefaultParameterSetName='Item Path')]
    [Alias('odsc')]
    [OutputType([PsObject])]
    Param
    (
        # API resource destination path.
        [Parameter(Mandatory=$True,
                   Position=1,
                   ValueFromPipeline=$True,
                   ValueFromPipelineByPropertyName=$True,
                   ParameterSetName='Item Path')]
        [Alias('ApiUrl', 'Resource')]
        [string]$Path,

        # API destination item ID.
        [Parameter(Mandatory=$True,
                   Position=1,
                   ValueFromPipelineByPropertyName=$True,
                   ParameterSetName='Item ID')]
        [Alias('id')]
        [string]$ItemId,

        # The local path of the source file or directory.
        [Parameter(Mandatory=$True,
                   Position=2)]
        [string]$Source,

        # By default, remote items are not overwritten. Set this to overwrite.
        [Parameter(Mandatory=$False)]
        [switch]$Force
    )

    Process {

        $upload = Get-Item -Path $Source

        if (!$upload) {
            return
        }

        Write-Verbose "Source path: $($upload.FullName)"

        if ($ItemId) {
            $remote = Get-OneDriveItem -ItemId $ItemId
            if (!$remote) {
                return
            }
        } else {
            $remote = Get-OneDriveItem -Path $Path -ErrorAction SilentlyContinue
        }

        if (!$remote -and !$Path.EndsWith('/')) {
            # Assume path leaf is the file name even if no extension
            $uploadPath = joinPath $PSOD.drive.pathRoot $Path
            $uploadPath = joinPath $uploadPath 'content' ':/'
        } elseif ($remote.Type -eq 'folder' -or $Path.EndsWith('/')) {
            if ($ItemId) {
                $uploadPath = joinPath $PSOD.drive.itemRoot $ItemId
                $uploadPath = joinPath $uploadPath 'children'
                $uploadPath = joinPath $uploadPath $upload.Name
                $uploadPath = joinPath $uploadPath 'content'
            } else {
                $uploadPath = joinPath $PSOD.drive.pathRoot $Path
                $uploadPath = joinPath $uploadPath $upload.Name
                $uploadPath = joinPath $uploadPath 'content' ':/'
            }
        } else {
            if ($ItemId) {
                $uploadPath = joinPath $PSOD.drive.itemRoot $remote.ParentId
                $uploadPath = joinPath $uploadPath $remote.Name ':/' 
                $uploadPath = joinPath $uploadPath 'content' ':/'
            } else {
                $uploadPath = joinPath $PSOD.drive.pathRoot $Path
                $uploadPath = joinPath $uploadPath 'content' ':/'
            }
        }

        Write-Verbose "Destination path: $uploadPath"

        if ($Force) {
            $uploadPath += '?@name.conflictBehavior=replace'
        } else {
            $uploadPath += '?@name.conflictBehavior=fail'
        }
        
        $rsp = Invoke-OneDriveApiCall -Path $uploadPath `
                                      -Method PUT `
                                      -InFile $upload.FullName

        if ($rsp) {
            Write-Output $rsp | newOneDriveItem
        }
        
    }

}

Export-ModuleMember -Function 'Set-OneDriveContent' -Alias 'odsc'