Advertisement
nullzilla

Monitor - ScreenConnect

Aug 28th, 2022 (edited)
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Import-Module $env:SyncroModule -DisableNameChecking
  2.  
  3. # Notes on behavior:
  4. #   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.
  5. #   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.
  6. #   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
  7.  
  8. # Create two Syncro platform variables for the script:
  9. #   $CompanyName // Variable Type - platform // Value - customer_business_name_or_customer_full_name
  10. #   $FriendlyName // Variable Type - platform // Value - asset_custom_field_device_name
  11. $CompanyName = [uri]::EscapeDataString($CompanyName) # Escape characters like & so name doesn't get abbreviated
  12.  
  13. # Your ScreenConnect Client service name
  14. #   Go to Computer Management > Services and open the ScreenConnect Client service
  15. #     Copy and paste your service name below. Example: 'ScreenConnect Client (hhff44iiaallcc44aa)'
  16. $serviceName = 'ScreenConnect Client ()'
  17.  
  18. # Your ScreenConnect MSI URL
  19. #   To make the ScreenConnect portal match what you have in Syncro we need to modify the download URL
  20. #   In ScreenConnect click the Build button, choose type "Windows (.msi)" from the dropdown and copy the URL
  21. #     Edit the URL to include the variables $FriendlyName and $CompanyName
  22. #   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="
  23. $msiURL = "https://"
  24.  
  25. # Check the agent installation age
  26. # 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!
  27. $checkAge = $true
  28.     # Number of days since installation to consider an agent 'too old'
  29.     $days = '180'
  30.  
  31. # Force reinstall even if service already exists
  32. $forceReinstall = $false
  33.  
  34. # Download path and filename
  35. $file = "$env:temp\sc.msi"
  36.  
  37. ##### END OF VARIABLES #####
  38.  
  39. function Install-SC {
  40.     Invoke-WebRequest -Uri "$msiURL" -OutFile "$file"
  41.     Start-Process "msiexec.exe" -ArgumentList "/i `"$file`" /quiet" -Wait
  42.     Remove-Item $file -Force
  43.     if ((Get-Service $serviceName -ErrorAction SilentlyContinue).Status -ne "Running") {
  44.         RMM-Alert -Category 'Monitor - ScreenConnect (Service)' -Body "Service not running, install failed"
  45.         exit 1
  46.     }
  47.     else { Write-Host "Install successful" }
  48. }
  49.  
  50. if ($forceReinstall -eq $true) { Install-SC }
  51.  
  52. # Test the service
  53. if ($null -eq (Get-Service $serviceName -ErrorAction SilentlyContinue)) {
  54.     Write-Host "$serviceName service not found, installing"
  55.     Install-SC
  56. } else {
  57.     if ((Get-Service $serviceName -ErrorAction SilentlyContinue).Status -eq 'Running') {
  58.         Write-Host "$serviceName service is running"
  59.     }
  60.     else {
  61.         # Try to start the service
  62.         Start-Service $serviceName
  63.         # Wait to make sure it stays started
  64.         Start-Sleep 3
  65.         if ((Get-Service $serviceName -ErrorAction SilentlyContinue).Status -eq 'Running') {
  66.             Write-Host "Service was not running, it has been started"
  67.             RMM-Alert -Category 'Monitor - ScreenConnect (Service)' -Body "Service was not running, it has been started"
  68.         }
  69.         else {
  70.             Write-Host "Service was not running and failed to start"
  71.             RMM-Alert -Category 'Monitor - ScreenConnect (Service)' -Body "Service was not running and failed to start"
  72.             exit 1
  73.         }
  74.     }
  75. }
  76.  
  77. # Check to see if version is old
  78. if ($checkAge -eq $true) {
  79.     $scinstall = Get-ChildItem "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall","HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall" | Where-Object { $_.GetValue( "DisplayName" ) -like $serviceName }
  80.     $scdate = Get-ItemProperty -Path "Registry::$scinstall" | Select-Object -ExpandProperty InstallDate
  81.     Write-Host "$serviceName installed on: $scdate"
  82.     if ($scdate -lt ((Get-Date).AddDays(-120).ToString("yyyyMMdd"))) {
  83.         Write-Host "Install is old, attempting update..."
  84.         Install-SC
  85.         $scdate = Get-ItemProperty -Path "Registry::$scinstall" | Select-Object -ExpandProperty InstallDate
  86.         if ($scdate -lt ((Get-Date).AddDays(-120).ToString("yyyyMMdd"))) {
  87.             Write-Host "Update failed, possible install corruption"
  88.             RMM-Alert -Category 'Monitor - ScreenConnect (Old Install)' -Body "Version is old, update failed, possible install corruption"
  89.             exit 1
  90.         }
  91.     }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement