Public/Initialize-Teamviewer.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
# .ExternalHelp Teamviewer-Help.xml
function Initialize-Teamviewer
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
        [securestring]$MasterPassword
    )

    Begin
    {        
        # Test if configuration file exists.
        $TeamviewerConfigPath = Resolve-TeamviewerConfigPath        

        if (!(Test-Path "$($TeamviewerConfigPath)\api.key"))
        {
            throw 'Configuration has not been set, Set-TeamviewerAccessToken to configure the Access Token.'
        }
    }
    Process
    {
        Try
        {
            Write-Verbose -Message "Reading key from $($TeamviewerConfigPath)\api.key."
        
            $ConfigFileContent = Get-Content -Path "$($TeamviewerConfigPath)\api.key"
        
            Write-Debug -Message "Secure string is $($ConfigFileContent)"
        
            $SaltBytes = Get-Content -Encoding Byte -Path "$($TeamviewerConfigPath)\salt.rnd" 
            $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList 'user', $MasterPassword

            # Derive Key, IV and Salt from Key
            $Rfc2898Deriver = New-Object System.Security.Cryptography.Rfc2898DeriveBytes -ArgumentList $Credentials.GetNetworkCredential().Password, $SaltBytes, 10000
            $KeyBytes  = $Rfc2898Deriver.GetBytes(32)

            $SecString = ConvertTo-SecureString -Key $KeyBytes $ConfigFileContent

            # Decrypt the secure string.
            #$SecureStringToBSTR = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecString)
            #$AccessToken = [Runtime.InteropServices.Marshal]::PtrToStringAuto($SecureStringToBSTR)

            # Set session variable with the Token.
            #$Global:TeamviewerAccessToken = $AccessToken
            $Global:TeamviewerAccessToken = $SecString
        
            Write-Verbose -Message 'Token has been set.'
        }
        Catch
        {
            Throw
        }
        Try
        {
            Write-Verbose -Message "Setting Device List"
            Set-TeamviewerDeviceList -AccessToken $AccessToken
        }
        Catch
        {
            Throw
        }
    }
    End
    {

    }
}