public/Get-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
function Get-OneShortcut {
    [CmdletBinding()]
    Param(    
        [Parameter(Mandatory = $false)]
        [string] $ShortcutName,
    
        [Parameter(Mandatory = $false)]
        [string] $UniqueItemId,

        [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 "No connection established. Please use Connect-OneShortcut to create a session." -ErrorAction Stop
        }
    }

    process {
        $ApiRequest = @{
            ApiRequestMethod = [Microsoft.PowerShell.Commands.WebRequestMethod]::Get
        }

        if ($ShortcutName) {
            $ApiRequest.ApiResource = "drives/$($UserPrincipalName)/root:/$([uri]::EscapeDataString($ShortcutName))"
        } elseif ($UniqueItemId) {
            $ApiRequest.ApiResource = "drives/$($UserPrincipalName)/items/$($UniqueItemId)"
        } else {
            Write-Error "[$(Get-Date)] [ERROR ] ShortcutName or ItemId required." -ErrorAction Stop
        }

        Write-Verbose "[$(Get-Date)] [CALL ] Invoke-OneApiRequest($($ApiRequest.ApiResource), $($ApiRequest.ApiRequestMethod))"
        $ApiResponse = (Invoke-OneApiRequest @ApiRequest)

        return $ApiResponse
    }

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