Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Import-Module $env:SyncroModule -DisableNameChecking
- # Notes on behavior:
- # The Friendly Name will only apply on initial install. If you change the name later in Syncro it will not sync, you'll have to change it in ScreenConnect manually.
- # If a Syncro asset has ever had a Friendly Name it will still have that name even if you have turned it off in Syncro. There's no way to check if it's on or off, so if you want to avoid this just don't add the script variable.
- # If you an agent already exists in ScreenConnect with a company name or you change it to a different company name, this script will not overwrite it
- # Create two Syncro platform variables for the script:
- # $CompanyName // Variable Type - platform // Value - customer_business_name_or_customer_full_name
- # $FriendlyName // Variable Type - platform // Value - asset_custom_field_device_name
- $CompanyName = [uri]::EscapeDataString($CompanyName) # Escape characters like & so name doesn't get abbreviated
- # Your ScreenConnect Client service name
- # Go to Computer Management > Services and open the ScreenConnect Client service
- # Copy and paste your service name below. Example: 'ScreenConnect Client (hhff44iiaallcc44aa)'
- $serviceName = 'ScreenConnect Client ()'
- # Your ScreenConnect MSI URL
- # To make the ScreenConnect portal match what you have in Syncro we need to modify the download URL
- # In ScreenConnect click the Build button, choose type "Windows (.msi)" from the dropdown and copy the URL
- # Edit the URL to include the variables $FriendlyName and $CompanyName
- # Example: "https://my.screenconnect.com/Bin/ConnectWiseControl.ClientSetup.msi?h=instance-***lots-of-letters-and-numbers_blah-blah-blah***=Access&y=Guest&t=$FriendlyName&c=$CompanyName&c=&c=&c=&c=&c=&c=&c="
- $msiURL = "https://"
- # Check the agent installation age
- # If you update your ScreenConnect server manually make sure you set a calendar reminder recurring ticket or similar to keep it updated or you risk getting a flood of alerts!
- $checkAge = $true
- # Number of days since installation to consider an agent 'too old'
- $days = '180'
- # Force reinstall even if service already exists
- $forceReinstall = $false
- # Download path and filename
- $file = "$env:temp\sc.msi"
- ##### END OF VARIABLES #####
- function Install-SC {
- Invoke-WebRequest -Uri "$msiURL" -OutFile "$file"
- Start-Process "msiexec.exe" -ArgumentList "/i `"$file`" /quiet" -Wait
- Remove-Item $file -Force
- if ((Get-Service $serviceName -ErrorAction SilentlyContinue).Status -ne "Running") {
- RMM-Alert -Category 'Monitor - ScreenConnect (Service)' -Body "Service not running, install failed"
- exit 1
- }
- else { Write-Host "Install successful" }
- }
- if ($forceReinstall -eq $true) { Install-SC }
- # Test the service
- if ($null -eq (Get-Service $serviceName -ErrorAction SilentlyContinue)) {
- Write-Host "$serviceName service not found, installing"
- Install-SC
- } else {
- if ((Get-Service $serviceName -ErrorAction SilentlyContinue).Status -eq 'Running') {
- Write-Host "$serviceName service is running"
- }
- else {
- # Try to start the service
- Start-Service $serviceName
- # Wait to make sure it stays started
- Start-Sleep 3
- if ((Get-Service $serviceName -ErrorAction SilentlyContinue).Status -eq 'Running') {
- Write-Host "Service was not running, it has been started"
- RMM-Alert -Category 'Monitor - ScreenConnect (Service)' -Body "Service was not running, it has been started"
- }
- else {
- Write-Host "Service was not running and failed to start"
- RMM-Alert -Category 'Monitor - ScreenConnect (Service)' -Body "Service was not running and failed to start"
- exit 1
- }
- }
- }
- # Check to see if version is old
- if ($checkAge -eq $true) {
- $scinstall = Get-ChildItem "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall","HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall" | Where-Object { $_.GetValue( "DisplayName" ) -like $serviceName }
- $scdate = Get-ItemProperty -Path "Registry::$scinstall" | Select-Object -ExpandProperty InstallDate
- Write-Host "$serviceName installed on: $scdate"
- if ($scdate -lt ((Get-Date).AddDays(-120).ToString("yyyyMMdd"))) {
- Write-Host "Install is old, attempting update..."
- Install-SC
- $scdate = Get-ItemProperty -Path "Registry::$scinstall" | Select-Object -ExpandProperty InstallDate
- if ($scdate -lt ((Get-Date).AddDays(-120).ToString("yyyyMMdd"))) {
- Write-Host "Update failed, possible install corruption"
- RMM-Alert -Category 'Monitor - ScreenConnect (Old Install)' -Body "Version is old, update failed, possible install corruption"
- exit 1
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement