From ed61e0b0fc1eff1401106c0a08e789ccd435116e Mon Sep 17 00:00:00 2001 From: bradhawkins85 Date: Mon, 5 Apr 2021 16:42:05 +1000 Subject: [PATCH] Create ScreenConnectAIO.ps1 Install, Uninstall, Start and Stop ScreenConnect Access Agent --- scripts/ScreenConnectAIO.ps1 | 89 ++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 scripts/ScreenConnectAIO.ps1 diff --git a/scripts/ScreenConnectAIO.ps1 b/scripts/ScreenConnectAIO.ps1 new file mode 100644 index 00000000..4a73b4cd --- /dev/null +++ b/scripts/ScreenConnectAIO.ps1 @@ -0,0 +1,89 @@ +<# +Requires global variables for serviceName "ScreenConnectService" and url "ScreenConnectInstaller" +serviceName is the name of the ScreenConnect Service once it is installed EG: "ScreenConnect Client (1327465grctq84yrtocq)" +url is the path the download the exe version of the ScreenConnect Access installer +Both variables values must start and end with " +Also accepts uninstall variable to remove the installed instance if required. +#> + +param ( + [string] $serviceName, + [string] $url, + [string] $action +) + +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + +if ($action -eq "uninstall") { + $MyApp = Get-WmiObject -Class Win32_Product | Where-Object{$_.Name -eq "$serviceName"} + $MyApp.Uninstall() +} else { + If (Get-Service $serviceName -ErrorAction SilentlyContinue) { + + If ((Get-Service $serviceName).Status -eq 'Running') { + Try + { + Write-Output "Stopping $serviceName" + Set-Service -Name $serviceName -Status stopped -StartupType disabled + exit 0 + } + Catch + { + $ErrorMessage = $_.Exception.Message + $FailedItem = $_.Exception.ItemName + Write-Error -Message "$ErrorMessage $FailedItem" + exit 1 + } + Finally + { + } + + } Else { + + Try + { + Write-Host "Starting $serviceName" + Set-Service -Name $serviceName -Status running -StartupType automatic + exit 0 + } + Catch + { + $ErrorMessage = $_.Exception.Message + $FailedItem = $_.Exception.ItemName + Write-Error -Message "$ErrorMessage $FailedItem" + exit 1 + } + Finally + { + } + + } + + } Else { + + $OutPath = $env:TMP + $output = "screenconnect.exe" + + Try + { + $start_time = Get-Date + $wc = New-Object System.Net.WebClient + $wc.DownloadFile("$url", "$OutPath\$output") + Start-Process -FilePath $OutPath\$output -Wait + Write-Output "Time taken to download and install: $((Get-Date).Subtract($start_time).Seconds) second(s)" + exit 0 + } + Catch + { + $ErrorMessage = $_.Exception.Message + $FailedItem = $_.Exception.ItemName + Write-Error -Message "$ErrorMessage $FailedItem" + exit 1 + } + Finally + { + Remove-Item -Path $OutPath\$output + } + + } +}