Automatisierung: Agents per PowerShell neu installieren

Bei einer Server-Migration kann es erforderlich sein, ConnectWise Automate Agents neu zu installieren. In diesem Artikel erfahren Sie, wie Sie die Neuinstallation mit Hilfe eines PowerShell-Scripts automatisieren können.

Das folgende PowerShell-Script lädt automatisch den Uninstaller für Windows-Agenten herunter, entfernt jede vorhandene Instanz eines bestehenden Automate-Agenten, lädt dann den .MSI-Installer herunter und installiert den Agenten erneut. Die Location-ID des Standorts, in dem ein Computer platziert werden soll, kann angegeben werden.

Das Script kann manuell auf dem Kundenrechner, über ConnectWise ScreenConnect oder über ein Automate-Script ausgeführt werden. 
#!ps
#timeout=9000000
#maxlength=9000000

# Tragen Sie Ihren FQDN Ihres neuen Servers ein, z.B.: 'automate.myfqdn.com' (ohne https://)
$FQDN = 'automate.myfqdn.com'

# Öffnen Sie die "config"-Tabelle in der Mysql-Datenbank auf Ihrem neuen Server (z.B. mit https://www.heidisql.com/).
# Tragen Sie das Server-Passwort aus dem Feld SystemPassword ein.
$SystemPass = 'SystemPass' 

# Tragen Sie hier die ID der Location ein, der der Agent zugeordnet werden soll. 
# Die Location-ID finden Sie in der Mysql-Datenbank Ihres neuen Servers in der Tabelle locations
# sowie in der Titelzeile des Location-Fensters im Control Center.
$LocationID = '2'

# Tragen Sie hier einen Download-Pfad ein, in dem die Installer abgelegt werden sollen.
$SoftwarePath = 'C:\Windows\Temp'    

# Die folgende Variable konvertiert den FQDN in eine URL für Downloads. Wenn HTTPS geblockt ist, bitte in HTTP ändern. 
# Bitte beachten: HTTPS-Downloads funktionieren nicht bei älteren Computern (XP /2003)!
$AutomateURL = "HTTPS://$($FQDN)"    

    Write-Host "Ihr FQDN ist: $($AutomateURL)" -ForegroundColor Yellow
    Write-Host "Ihr System Password ist: $($SystemPass)" -ForegroundColor Yellow
# Aktuellen Agent entfernen 
    if (Test-Path c:\windows\ltsvc) {
    $DownloadPath = "$($AutomateURL)/labtech/service/LabUninstall.exe"
    $Filename = [System.IO.Path]::GetFileName($DownloadPath)
    $SoftwareFullPath = "$($SoftwarePath)\$Filename"
    $wc = New-Object System.Net.WebClient
    if (!(Test-Path $SoftwarePath)) {md $SoftwarePath}
    if ((Test-Path $SoftwareFullPath)) {remove-item $SoftwareFullPath}
    $wc.DownloadFile($DownloadPath, $SoftwareFullPath)
    Write-Host "Aktuellen Agent entfernen..."
        sc.exe stop "LTService"
        sc.exe stop "LTSvcMon"
        Start-Sleep 10
        sc.exe query "LTService"
        sc.exe query "LTSvcMon"
        taskkill /im ltsvcmon.exe /f
        taskkill /im lttray.exe /f
        taskkill /im ltsvc.exe /f
    Invoke-Expression "$($SoftwareFullPath) /quiet /norestart"
    Write-Host "60 Sekunden warten..." -ForegroundColor Gray
    Start-Sleep 60
        if (Test-Path c:\windows\ltsvc\lterrors.txt) {
        Write-Host "  weiter warten..." -ForegroundColor Gray
        Start-Sleep 90}
        if (Test-Path c:\windows\ltsvc\lterrors.txt) {
        Write-Host "Fehler: C:\Windows\LTSVC existiert noch!" -ForegroundColor Red} else {
        Write-Host "Automate-Agent erfolgreich entfernt!" -ForegroundColor Green
        }}
# Neuen Agent installieren
    $DownloadPath2 = "$($AutomateURL)/labtech/service/LabTechRemoteAgent.msi"
    $Filename2 = [System.IO.Path]::GetFileName($DownloadPath2)
    $SoftwareFullPath2 = "$($SoftwarePath)\$Filename2"
    $wc = New-Object System.Net.WebClient
    if (!(Test-Path $SoftwarePath)) {md $SoftwarePath}
    if ((test-path $SoftwareFullPath2)) {remove-item $SoftwareFullPath2}
    $wc.DownloadFile($DownloadPath2, $SoftwareFullPath2)
    Write-Host "Automate-Agent von $($AutomateURL) wird installiert..." -ForegroundColor Yellow
    msiexec.exe /i $($SoftwareFullPath2) /quiet /norestart SERVERADDRESS=$($AutomateURL) SERVERPASS=$($SystemPass) LOCATION=$($LocationID)
    Write-Host "3 Minuten warten, bis Installation abgeschlossen ist..." -ForegroundColor Gray
    Start-Sleep -s 180
    # sc.exe start ltsvcmon
    # sc.exe start ltservice
        if (Test-Path c:\windows\ltsvc\lterrors.txt) {
        C:\WINDOWS\LTSVC\LTTray.exe
        $AutomateSrvAddrReg = (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").'Server Address'
        $AutomateCompIDReg = (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").ID
        Write-Host "Der Automate-Agent hat sich erfolgreich gemeldet bei:" -ForegroundColor Green
        Write-Host "$($AutomateSrvAddrReg)" -ForegroundColor Green
        Write-Host "Computer ID: $($AutomateCompIDReg)" -ForegroundColor Green 
        }

Alternativ kann das hier verfügbare PowerShell-Modul verwendet werden. Beispiel:
$inputLocation = Read-Host "LocationID"
iex (new-object Net.WebClient).DownloadString('https://bit.ly/LTPoSh') 
Install-LTService -Server 'https://automate.server.com' -LocationID $inputLocation -ServerPassword 'ServerPassword' -Force
Das Server-Passwort finden Sie in der config-Tabelle (Feld SystemPassword).
Bitte beachten Sie: Die Verwendung dieses Moduls erfolgt auf eigene Gefahr. EBERTLANG und ConnectWise bieten keinen Support für eventuelle Fehler oder Probleme an.