Func_New-OneDriveFolder.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
################################################################################
# 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 New-OneDriveFolder {
    <#
        .SYNOPSIS
        Create a new folder in the specified location.
         
        .EXAMPLE
        New-OneDriveFolder -Path 'path/to/folder'
 
        .EXAMPLE
        New-OneDriveFolder -Name 'NewFolder' -ParentId '1234ABC!123'
 
        .EXAMPLE
        'path/to/folder1', 'path/to/folder2' | odmkdir
 
        .NOTES
        The API will create all folders in the path, even if they don't exist.
        This command does not fail if the folder already exists.
    #>

    [CmdletBinding(DefaultParameterSetName='Folder Path')]
    [Alias('odnf','odmkdir')]
    [OutputType([PsObject])]
    Param
    (
        # API path of the new folder.
        [Parameter(Mandatory=$False,
                   Position=1,
                   ValueFromPipeline=$True,
                   ValueFromPipelineByPropertyName=$True,
                   ParameterSetName='Folder Path')]
        [Alias('ApiUrl', 'Resource')]
        [string]$Path,

        # Name of the folder
        [Parameter(Mandatory=$True,
                   Position=1,
                   ValueFromPipelineByPropertyName=$True,
                   ParameterSetName='Parent ID')]
        [string]$Name,

        # Id of the parent
        [Parameter(Mandatory=$True,
                   Position=2,
                   ValueFromPipelineByPropertyName=$True,
                   ParameterSetName='Parent ID')]
        [Alias('id')]
        [string]$ParentId,

        # By default, if folder exists is fails. Set to overwrite.
        [Parameter(Mandatory=$False)]
        [switch]$Force
    )

    Process {

        if ($ParentId) {
            $parent = joinPath $PSOD.drive.itemRoot $ParentId
            $parent = joinPath $parent 'children'
        } else {
            $parent = joinPath $PSOD.drive.pathRoot (Split-Path $Path)
            $parent = joinPath $parent 'children' ':/'
            $Name   = Split-Path $Path -Leaf
        }

        Write-Verbose "Folder name: $Name"
        Write-Verbose "Parent path: $parent"

        $body = [ordered]@{
            name   = $Name
            folder = @{}
        } | ConvertTo-Json

        Write-Verbose "Request body:`n$body"

        if ($Force) {
            $parent += '?@name.conflictBehavior=replace'
        } else {
            $parent += '?@name.conflictBehavior=fail'
        }

        $rsp = Invoke-OneDriveApiCall -Path $parent `
                                      -Method POST `
                                      -Body $body
        
        if ($rsp) {
            Write-Output $rsp | newOneDriveItem
        }
    }

}

Export-ModuleMember -Function 'New-OneDriveFolder' -Alias 'odnf', 'odmkdir'