Public/Set-TeamviewerDeviceList.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
# .ExternalHelp Teamviewer-Help.xml
Function Set-TeamviewerDeviceList
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$false)]
        [securestring]$AccessToken
    )

    Begin
    {   
        Write-Verbose -Message 'Starting: Set Teamviewer Device List'
        
        if (!(Test-Path variable:Global:TeamviewerAccessToken ) -and !($AccessToken))
        {
            throw 'No Teamviewer Access Token has been specified or set. Use Set-TeamviewerAccessToken to set your AccessToken or Initialize-Teamviewer to load Teamviewer Global Variables.'
        }
        elseif ((Test-Path variable:Global:TeamviewerAccessToken ) -and !($AccessToken))
        {
            $AccessToken = $Global:TeamviewerAccessToken
        }

        $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($AccessToken)
        $PlainAccessToken = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)     
    }

    Process
    {                
        $Headers = @{ 'Authorization' = "Bearer $PlainAccessToken" }
        $ContetType = 'application/json; charset=utf-8'
        $Uri = 'https://webapi.teamviewer.com/api/v1/devices/'
        
        Write-Verbose -Message "[GET] RestMethod: [$Uri]"                        

        $Result = Invoke-RestMethod -Method Get -Uri $Uri -Headers $Headers -ContentType $ContetType -ErrorVariable TVError -ErrorAction SilentlyContinue
            
        if ($TVError)
        {
            $JsonError = $TVError.Message | ConvertFrom-Json
            $HttpResponse = $TVError.ErrorRecord.Exception.Response
            Throw "Error: $($JsonError.error) `nDescription: $($JsonError.error_description) `nErrorCode: $($JsonError.error_code) `nHttp Status Code: $($HttpResponse.StatusCode.value__) `nHttp Description: $($HttpResponse.StatusDescription)"
        }
        else 
        {
            Write-Verbose -Message "Setting Device List to variable for use by other commands."

            $Global:TeamviewerDeviceList = $Result
        }
    }

    End
    {
    
    }
}