Jump to content
Braingears

Automate Install - Rip & Replace - Powershell

Recommended Posts

This PowerShell script will automatically download the Windows Agent Uninstaller, remove any existing instance of an existing Automate (aka LabTech) agent (from any MSP); then download the .MSI and reinstall the agent to your Automate server. Although it defaults to the 'New Computers' location, you can specify any Location ID to place the computers into. 

The script can be run manually on the client computer, from Control (aka ScreenConnect) Commands, or from Automate scripting using the Execute function (if you are performing a migration to another Automate server).

Requirements:
• Automate FQDN
• System Password (located in the Config table)

SELECT `SystemPassword` FROM `labtech`.`config

 

# Automate Agent - Rip & Replace
# created by Chuck Fowler 2018-05-14
#!ps
#timeout=9000000
#maxlength=9000000
#### Enter Automate Server FQDN - Example: 'https://company.hostedrmm.com'
$FQDN = 'https://server.hostedrmm.com'

#### Enter Location ID to place the computer in assigned Client's Location (Location ID '1' = New Computers (1) Location)
$LocationID = '1'

#### Enter 'System Password' (Located in the CONFIG table in the Automate database)
$SERVERPASS = 'LBu7NjVHOgcknPeA'

#### Enter Software Path in order to download the files to
$SoftwarePath = 'C:\Support\Automate'

	Write-Host "Your FQDN is set to: $($FQDN)" -ForegroundColor Yellow
	Write-Host "Your System Password is set to: $($SERVERPASS)" -ForegroundColor Yellow
# Remove Existing Automate Agent
	if (Test-Path c:\windows\ltsvc)
    {
    $DownloadPath = "$($FQDN)/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 "Removing existing Automate agent..."
        sc.exe config "LTService" start= disabled
        sc.exe config "LTSvcMon" start= disabled
        taskkill /im ltsvcmon.exe /f
        taskkill /im lttray.exe /f
        taskkill /im ltsvc.exe /f
    Invoke-Expression "$($SoftwareFullPath) /quiet /norestart"
    Write-Host "Waiting 60 seconds to continue..." -ForegroundColor Gray
    Start-Sleep 60
        if (Test-Path c:\windows\ltsvc\lterrors.txt) {
        Write-Host "  still waiting..." -ForegroundColor Gray
        Start-Sleep 90}
        if (Test-Path c:\windows\ltsvc\lterrors.txt) {
        Write-Host "The C:\Windows\LTSVC folder still exists" -ForegroundColor Red} else {
        Write-Host "The Automate Agent Removed Successfully" -ForegroundColor Green
        }
    }
# Install Automate Agent
    $DownloadPath2 = "$($FQDN)/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 "Installing Automate Agent on $($FQDN)" -ForegroundColor Yellow
    msiexec.exe /i $($SoftwareFullPath2) /quiet /norestart SERVERADDRESS=$($FQDN) SERVERPASS=$($SERVERPASS) LOCATION=$($LocationID)
    Write-Host "Waiting 3 minutes for Automate to install..." -ForegroundColor Gray
    Start-Sleep -s 180
    # sc.exe start ltsvcmon
    # sc.exe start ltservice
        if (Test-Path c:\windows\ltsvc\lterrors.txt) {
        $AutomateSrvAddrReg = (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").'Server Address'
        $AutomateCompIDReg = (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service").ID
        Write-Host "The Automate Agent is successfully checking-in to:" -ForegroundColor Green
        Write-Host "$($AutomateSrvAddrReg)" -ForegroundColor Green
        Write-Host "Computer ID: $($AutomateCompIDReg)" -ForegroundColor Green 
        }

 

R&R Automate Agent.ps1

Share this post


Link to post
Share on other sites

Thanks for posting this up!

You may well benefit from knowing about the LabTech Powershell Module out there in the wild. http://labtechconsulting.com/labtech-agent-powershell-module/

The module is on Github and is primarily maintained by the below, I'm sure they'd appreciate additional contributions! What I really like about that method is you can just call it from anywhere with an internet connection, the below, for example, would Reinstall the agent with no additional dependencies needed:

(new-object Net.WebClient).DownloadString('http://bit.ly/ltposh') | iex

 

@CTaylor and @DarrenWhite99

  • Thanks 1

Share this post


Link to post
Share on other sites
56 minutes ago, Gavsto said:

Thanks for posting this up!

You may well benefit from knowing about the LabTech Powershell Module out there in the wild. http://labtechconsulting.com/labtech-agent-powershell-module/

The module is on Github and is primarily maintained by the below, I'm sure they'd appreciate additional contributions! What I really like about that method is you can just call it from anywhere with an internet connection, the below, for example, would Reinstall the agent with no additional dependencies needed:


(new-object Net.WebClient).DownloadString('http://bit.ly/ltposh') | iex

 

@CTaylor and @DarrenWhite99

Since you have to customize the script with your Automate server's FQDN and 'System Password', it cannot be called and run on demand from a public / generic source. 

Share this post


Link to post
Share on other sites
2 hours ago, Braingears said:

Since you have to customize the script with your Automate server's FQDN and 'System Password', it cannot be called and run on demand from a public / generic source. 

It can - ie the following would work:

(new-object Net.WebClient).DownloadString('http://bit.ly/ltposh') | iex; Reinstall-LTService -Server https://yourrmm.yourdomain.com -Password whateveritis


 

Share this post


Link to post
Share on other sites

To get the System Password from Automate:  System > General > MySQL Prompt

SELECT `SystemPassword` FROM `labtech`.`config`;

I found that older computers such as Windows 2003 Server did not download the files using the HTTPS links, but would work with HTTP; so I've revised the script accordingly. I've also added the capability to target the Location ID the computers are placed into.

## Automate Agent - Rip & Replace v1.3 | created by Chuck Fowler 2018-07-10 ##
#!ps
#timeout=900000
#maxlength=9000000
$FQDN = 'automate.yourdomain.com'               # Enter Automate Server FQDN - Example: 'company.hostedrmm.com' - Leave out HTTP:\\ or HTTPS:\\
$LocationID = '2'                               # Enter Location ID to place the computer in assigned Client's Location (Location ID '1' = New Computers)
$SystemPass = 'AhhVCoXR1xas4Vko'                # Enter 'System Password' (Located in the CONFIG table in the Automate database)
$SoftwarePath = 'C:\Support\Automate'           # Enter Software Path in order to download the files to
    $AutomateURL = "HTTPS://$($FQDN)" 
	Write-Host "Your FQDN is set to: $($AutomateURL)" -ForegroundColor Yellow
    Write-Host "Your System Password is: $($SystemPass)" -ForegroundColor Yellow
	$AutomateSrvAddrReg = (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service" -ErrorAction SilentlyContinue).'Server Address'
    $AutomateCompIDReg = (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service" -ErrorAction SilentlyContinue).ID
    if (($AutomateSrvAddrReg -like "*$($FQDN)*") -and ($AutomateCompIDReg -ne $null)) {
        Start-Service ltservice,ltsvcmon -PassThru
        Write-Host ""
	    Write-Host "The Automate Agent is already installed." -ForegroundColor Green
        Write-Host "The Automate Agent is checking-in to: $($AutomateSrvAddrReg)" -ForegroundColor Green
        Write-Host "ComputerID: $($AutomateCompIDReg)" -ForegroundColor Green 
        }
	    else {
      # Remove Existing Automate Agent
	    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 "Removing existing Automate agent..."
            Stop-Service ltservice,ltsvcmon -Force -ErrorAction SilentlyContinue
            Stop-Process -Name "ltsvcmon", "lttray","ltsvc" -Force -PassThru -ErrorAction SilentlyContinue
        Invoke-Expression "$($SoftwareFullPath) /quiet /norestart"
        Write-Host "Waiting 60 seconds to continue..." -ForegroundColor Gray
        Start-Sleep 60
            if (Test-Path c:\windows\ltsvc\lterrors.txt) {
            Write-Host "  still waiting..." -ForegroundColor Gray
            Start-Sleep 90}
            if (Test-Path c:\windows\ltsvc\lterrors.txt) {
            Write-Host "The C:\Windows\LTSVC folder still exists" -ForegroundColor Red} else {
            Write-Host "The Automate Agent Removed Successfully" -ForegroundColor Green
            }}
      # Install Automate Agent
        $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 "Installing Automate Agent on $($AutomateURL)" -ForegroundColor Yellow
        msiexec.exe /i $($SoftwareFullPath2) /quiet /norestart SERVERADDRESS=$($AutomateURL) SERVERPASS=$($SystemPass) LOCATION=$($LocationID)
        Write-Host "Waiting 3 minutes for Automate to install..." -ForegroundColor Gray
        Start-Sleep -s 180
        Start-Service ltservice,ltsvcmon -PassThru
        $AutomateSrvAddrReg = (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service" -ErrorAction SilentlyContinue).'Server Address'
        $AutomateCompIDReg = (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service" -ErrorAction SilentlyContinue).ID
		    if (($AutomateSrvAddrReg -like "*$($FQDN)*") -and ($AutomateCompIDReg -ne $null)) {
            Write-Host "The Automate Agent is checking-in to:" -ForegroundColor Green
            Write-Host "$($AutomateSrvAddrReg)" -ForegroundColor Green
            Write-Host "ComputerID: $($AutomateCompIDReg)" -ForegroundColor Green 
            }
		    else {
                Write-Host "The Automate Agent is NOT installed and/or checking-in" -ForegroundColor Red
                Write-Host "Server Address: $($AutomateSrvAddrReg)" -ForegroundColor Yellow
                Write-Host "ComputerID: $($AutomateCompIDReg)" -ForegroundColor Yellow
            }
    }

 

Edited by Braingears

Share this post


Link to post
Share on other sites

This is an update to verify if YOUR agent is already installed. If it's from another MSP, it will replace it with yours.

 

## Automate Agent - Rip & Replace v1.3 | created by Chuck Fowler 2018-07-10 ##
#!ps
#timeout=900000
#maxlength=9000000
$FQDN = 'automate.yourdomain.com'               # Enter Automate Server FQDN - Example: 'company.hostedrmm.com' - Leave out HTTP:\\ or HTTPS:\\
$LocationID = '2'                               # Enter Location ID to place the computer in assigned Client's Location (Location ID '1' = New Computers)
$SystemPass = 'AhhVCoXR1xas4Vko'                # Enter 'System Password' (Located in the CONFIG table in the Automate database)
$SoftwarePath = 'C:\Support\Automate'           # Enter Software Path in order to download the files to
    $AutomateURL = "HTTPS://$($FQDN)" 
	Write-Host "Your FQDN is set to: $($AutomateURL)" -ForegroundColor Yellow
    Write-Host "Your System Password is: $($SystemPass)" -ForegroundColor Yellow
	$AutomateSrvAddrReg = (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service" -ErrorAction SilentlyContinue).'Server Address'
    $AutomateCompIDReg = (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service" -ErrorAction SilentlyContinue).ID
    if (($AutomateSrvAddrReg -like "*$($FQDN)*") -and ($AutomateCompIDReg -ne $null)) {
        Start-Service ltservice,ltsvcmon -PassThru
        Write-Host ""
	    Write-Host "The Automate Agent is already installed." -ForegroundColor Green
        Write-Host "The Automate Agent is checking-in to: $($AutomateSrvAddrReg)" -ForegroundColor Green
        Write-Host "ComputerID: $($AutomateCompIDReg)" -ForegroundColor Green 
        }
	    else {
      # Remove Existing Automate Agent
	    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 "Removing existing Automate agent..."
            Stop-Service ltservice,ltsvcmon -Force -ErrorAction SilentlyContinue
            Stop-Process -Name "ltsvcmon", "lttray","ltsvc" -Force -PassThru -ErrorAction SilentlyContinue
        Invoke-Expression "$($SoftwareFullPath) /quiet /norestart"
        Write-Host "Waiting 60 seconds to continue..." -ForegroundColor Gray
        Start-Sleep 60
            if (Test-Path c:\windows\ltsvc\lterrors.txt) {
            Write-Host "  still waiting..." -ForegroundColor Gray
            Start-Sleep 90}
            if (Test-Path c:\windows\ltsvc\lterrors.txt) {
            Write-Host "The C:\Windows\LTSVC folder still exists" -ForegroundColor Red} else {
            Write-Host "The Automate Agent Removed Successfully" -ForegroundColor Green
            }}
      # Install Automate Agent
        $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 "Installing Automate Agent on $($AutomateURL)" -ForegroundColor Yellow
        msiexec.exe /i $($SoftwareFullPath2) /quiet /norestart SERVERADDRESS=$($AutomateURL) SERVERPASS=$($SystemPass) LOCATION=$($LocationID)
        Write-Host "Waiting 3 minutes for Automate to install..." -ForegroundColor Gray
        Start-Sleep -s 180
        Start-Service ltservice,ltsvcmon -PassThru
        $AutomateSrvAddrReg = (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service" -ErrorAction SilentlyContinue).'Server Address'
        $AutomateCompIDReg = (Get-ItemProperty "HKLM:\SOFTWARE\LabTech\Service" -ErrorAction SilentlyContinue).ID
		    if (($AutomateSrvAddrReg -like "*$($FQDN)*") -and ($AutomateCompIDReg -ne $null)) {
            Write-Host "The Automate Agent is checking-in to:" -ForegroundColor Green
            Write-Host "$($AutomateSrvAddrReg)" -ForegroundColor Green
            Write-Host "ComputerID: $($AutomateCompIDReg)" -ForegroundColor Green 
            }
		    else {
                Write-Host "The Automate Agent is NOT installed and/or checking-in" -ForegroundColor Red
                Write-Host "Server Address: $($AutomateSrvAddrReg)" -ForegroundColor Yellow
                Write-Host "ComputerID: $($AutomateCompIDReg)" -ForegroundColor Yellow
            }
    }

 

Edited by Braingears

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×