Public/Update-TeamviewerDevice.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
# .ExternalHelp Teamviewer-Help.xml
Function Update-TeamviewerDevice
{
    [CmdletBinding()]
    param
    (        
        [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
        [string]$ComputerName,
        
        [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
        [string]$Description,
        
        [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
        [string]$Alias,
        
        [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
        [string]$Password,
        
        [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
        [Switch]$UpdateDeviceList,

        [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$false)]
        [securestring]$AccessToken
    )

    Begin
    {   
        Write-Verbose -Message 'Starting: Update Teamviewer Device'
        
        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)

        if ($PSBoundParameters.ContainsKey('UpdateDeviceList'))
        {
            Write-Verbose -Message "Updating Teamviewer Device List before Updating Device"
            Set-TeamviewerDeviceList -AccessToken $AccessToken
        }
    }

    Process
    {
        Write-Verbose "Changing Device Information: [$ComputerName]"
        
        Try
        {
            Write-Verbose -Message "Getting the Device ID for [$ComputerName]"
            $deviceId = Get-TeamviewerDeviceProperty -ComputerName $ComputerName -device_id
        }
        catch
        {
            Throw $Error[0]
        }

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

        $deviceFields = @{}
        
        if ($PSBoundParameters.ContainsKey('Description')) 
        { 
            Write-Verbose -Message "Adding Description Field: [$Description]"
            $deviceFields.description = $Description 
        }
        
        if ($PSBoundParameters.ContainsKey('Password')) 
        { 
            Write-Verbose -Message "Adding Password Field"
            $deviceFields.password = $Password 
        }
    
        if ($PSBoundParameters.ContainsKey('Alias')) 
        { 
            Write-Verbose -Message "Adding Alias Field: [$Alias]"
            $deviceFields.alias = $Alias
        }

        $psobject = New-Object psobject -Property $deviceFields
        $Body = $psobject | Microsoft.PowerShell.Utility\ConvertTo-Json
        
        Write-Verbose -Message "Body: $($Body)"

        Invoke-RestMethod -Method Put -Uri $Uri -Headers $Headers -ContentType $ContetType -Body $Body -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)"
        }
    }

    End
    {

    }
}