tacticalrmm/scripts/ScreenConnectAIO.ps1

90 lines
2.6 KiB
PowerShell
Raw Normal View History

<#
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
}
}
}