Automation: Download custom TeamViewer with Powershell

C_Is
C_Is Posts: 4
edited December 2021 in Deployment & Integrations

I needed this and couldn't find a solution online so I sat down and figured this out last night.

In my use case I'm using this in a schedule to monthly get the newest QuickSupport on my Domain-Controller and from there on I distribute it to all domain computers' public desktop.

Note: Make sure to replace "<yourcustomurl>" and the destination path where it should be saved to.

$iebrowser = New-Object -ComObject InternetExplorer.Application
$iebrowser.Navigate('https://get.teamviewer.com/<yourcustomurl>')
Start-Sleep -Seconds 3
$CustomTVQS_URL = $iebrowser.Document.getElementById('MasterBodyContent_btnRetry').href
Start-BitsTransfer -source $CustomTVQS_URL -Destination C:\test\TeamViewerQS.exe

PS: I put a 3 second pause after calling the website URL since it takes a little time to create the ComObject and call the website URL. Powershell is working through the commands too fast and otherwise throws an error that the $CustomTVQS_URL is a null object. You may increase or decrease this if needed.

Comments

  • C_Is
    C_Is Posts: 4

    I noticed some difficulties on automating this on a windows server.

    Will update this as soon as I get it fully working and wrote a decent documentation.

  • C_Is
    C_Is Posts: 4
    edited December 2021

    Note that you need to:

    • configure your Internet Explorer security settings for the 'internet zone':
      • Enable 'Active Scripting'
      • Enable 'File Download'
    • configure execution policy for powershell

    When using this in task scheduler on a Microsoft Windows Server make sure to set the account to 'System'.

    # Download path
    $downloadpath = "C:\TeamViewer"
    # URL of your customized TeamViewer
    $URL = "https://get.teamviewer.com/<yourcustomURL>"
    
    # Function to create the browser object, navigate to the website, isolate the download url and download the TeamViewer QS
    function Download_TeamViewerQS () {
        # Internet Explorer application to fool the website into thinking the connection is a client session (this is needed to return the result from javascript function that generates the downloadlink of your custom TeamViewer using the newest version of it)
        $iebrowser = New-Object -ComObject InternetExplorer.Application
        # navigates to the given Website/URL
        $iebrowser.Navigate($URL)
        # Pausing 3 seconds because creating the browser object and navigating to the website takes a little while depending on your system and internet connection
        Start-Sleep -Seconds 3
        # isolates the generated download URL of the custom TeamViewer
        $CustomTVQS_URL = $iebrowser.Document.getElementById('MasterBodyContent_btnRetry').href
        # Downloads the custom TeamViewer QS
        Start-BitsTransfer -source $CustomTVQS_URL -Destination $downloadpath\TeamViewerQS.exe       
    }
    
    # Try (creating path and) downloading and catch problem if this is not possible
    try {
        Write-Host "Downloading custom TeamViewer QuickSupport..." -BackgroundColor Black -ForegroundColor Yellow
        # Tests if path exists and (creates the path and) downloads TeamViewer QS into it
        if (Test-Path $downloadpath) {
            # Calls the download function
            Download_TeamViewerQS
        } else {
            # Creates the download path if not existent
            New-Item -Path $downloadpath -ItemType Directory | Out-Null
            # Calls the download function
            Download_TeamViewerQS
        }
    } catch {
        # Display error if one occurs
        $Error
        # Write information for user
        Write-Host "Download can not be started.`n`nReason: The security settings for `"Internet Zone`" in your Internet Explorer need following configuration:`n- Filedownloads need to be activated`n- Active Scripting needs to be activated" -ForegroundColor Yellow -BackgroundColor Black
        Start-Sleep -Seconds 10
    }