public/New-OneShortcut.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
function New-OneShortcut {
    [CmdletBinding(SupportsShouldProcess)]
    Param(
        [Parameter(Mandatory = $true)]
        [string] $SiteUrl,

        [Parameter(Mandatory = $true)]
        [string] $DocumentLibraryName,

        [Parameter(Mandatory = $false)]
        [string] $FolderPath,

        [Parameter(Mandatory = $true)]
        [string] $ShortcutName,

        [Parameter(Mandatory = $true)]
        [string] $UserPrincipalName
    )
    
    begin {
        Write-Verbose "[$(Get-Date)] [BEGIN ] $($MyInvocation.MyCommand)"

        Write-Verbose "[$(Get-Date)] [CALL ] Test-OneConnection()"
        if (!(Test-OneConnection)) {
            Write-Error "[$(Get-Date)] [ERROR ] No connection established. Please use Connect-OneShortcut to create a session." -ErrorAction Stop
        }
    }

    process {
        Write-Verbose "[$(Get-Date)] [CALL ] Get-OneSite($($SiteUrl))"
        $Site = (Get-OneSite -SiteUrl $SiteUrl)

        if ($Site.Response.StatusCode -eq "200") {
            Write-Verbose "[$(Get-Date)] [CALL ] Get-OneList($($Site.Data.sharepointIds.siteId), $($DocumentLibraryName))"
            $List = (Get-OneList -SiteId $Site.Data.sharepointIds.siteId -DocumentLibraryName $DocumentLibraryName)

            if ($List.Response.StatusCode -eq "200") {
                Write-Verbose "[$(Get-Date)] [CALL ] Get-OneListItem($($Site.Data.sharepointIds.siteId), $($List.id), $($FolderPath))"

                $UniqueItemId = "root"

                if ($FolderPath) {
                    Write-Verbose "[$(Get-Date)] [CALL ] Get-OneListItem($($Site.Data.sharepointIds.siteId), $($List.Data.id), $($FolderPath))"
                    $ListItem = (Get-OneListItem -SiteId $Site.Data.sharepointIds.siteId -DocumentLibraryId $List.Data.id -FolderPath $FolderPath)

                    if ($ListItem.Response.StatusCode -eq "200") {
                        $UniqueItemIdSearch = (Select-String "[\da-zA-Z]{8}-([\da-zA-Z]{4}-){3}[\da-zA-Z]{12}" -InputObject $ListItem.Data.eTag)
    
                        if ($UniqueItemIdSearch.Matches.Count -gt 0) {
                            $UniqueItemId = $UniqueItemIdSearch.Matches[0].Value
                        }
                    } else {
                        return $ListItem
                    }
                }

                $ApiRequestBody = @{
                    name = $ShortcutName
                    remoteItem = @{
                        sharepointIds = @{
                            listId = $List.Data.id
                            listItemUniqueId = $UniqueItemId
                            siteId = $Site.Data.sharepointIds.siteId
                            siteUrl = $SiteUrl
                            webId = $Site.Data.sharepointIds.webId
                        }
                    }
                    '@microsoft.graph.conflictBehavior' = "rename"
                }
            
                $ApiRequest = @{
                    ApiResource = "drives/$($UserPrincipalName)/root/children"
                    ApiRequestMethod = [Microsoft.PowerShell.Commands.WebRequestMethod]::Post
                    ApiRequestBody = ($ApiRequestBody | ConvertTo-Json)
                }

                if ($PSCmdlet.ShouldProcess("$($UserPrincipalName)'s OneDrive", "Creating shortcut '$($ShortcutName)'")) {
                    Write-Verbose "[$(Get-Date)] [CALL ] Invoke-OneApiRequest($($ApiRequest.ApiResource), $($ApiRequest.ApiRequestMethod), $($ApiRequest.ApiRequestBody))"
                    $Shortcut = (Invoke-OneApiRequest @ApiRequest)

                    return $Shortcut
                }
            } else {
                return $List
            }
        } else {
            return $Site
        }
    }

    end {
        Write-Verbose "[$(Get-Date)] [END ] $($MyInvocation.MyCommand)"
    }
}