param ( [string]$DeviceAlias, [string]$DeviceGroup, [string]$Path, [string]$OrganizationId, [string]$ServerUrl, [switch]$Uninstall ) [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 $LogPath = "$env:TEMP\Remotely_Install.txt" [string]$HostName = "https://remotely.aiursoft.cn" if ($ServerUrl) { $HostName = $ServerUrl } [string]$Organization = "5ff766e6-d383-4924-a34d-18f6b90c0c68" if ($OrganizationId) { $Organization = $OrganizationId } $ConnectionInfo = $null if ([System.Environment]::Is64BitOperatingSystem){ $Platform = "x64" } else { $Platform = "x86" } $InstallPath = "$env:ProgramFiles\Remotely" function Write-Log($Message){ Write-Host $Message "$((Get-Date).ToString()) - $Message" | Out-File -FilePath $LogPath -Append } function Do-Exit(){ Write-Host "Exiting..." Start-Sleep -Seconds 3 exit } function Is-Administrator() { $Identity = [Security.Principal.WindowsIdentity]::GetCurrent() $Principal = New-Object System.Security.Principal.WindowsPrincipal -ArgumentList $Identity return $Principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } function Run-StartupChecks { if (!$HostName -or !$Organization) { Write-Log "Required parameters are missing. Please try downloading the installer again." Do-Exit } if (!(Is-Administrator)) { Write-Log -Message "Install script requires elevation. Attempting to self-elevate..." Start-Sleep -Seconds 3 $Params = "-File `"$($MyInvocation.ScriptName)`""; if ($OrganizationId) { $Params += " -OrganizationId $OrganizationId" } if ($ServerUrl) { $Params += " -ServerUrl $ServerUrl" } if ($DeviceAlias) { $Params += " -DeviceAlias $DeviceAlias" } if ($DeviceGroup) { $Params += " -DeviceGroup $DeviceGroup" } if ($Path) { $Params += " -Path `"$Path`"" } if ($Uninstall) { $Params += " -Uninstall" } Start-Process -FilePath powershell.exe -ArgumentList $Params -Verb RunAs exit } } function Stop-Remotely { Start-Process -FilePath "cmd.exe" -ArgumentList "/c sc delete Remotely_Service" -Wait -WindowStyle Hidden Stop-Process -Name Remotely_Agent -Force -ErrorAction SilentlyContinue Stop-Process -Name Remotely_Desktop -Force -ErrorAction SilentlyContinue } function Uninstall-Remotely { Stop-Remotely Remove-Item -Path $InstallPath -Force -Recurse -ErrorAction SilentlyContinue Remove-NetFirewallRule -Name "Remotely Desktop Unattended" -ErrorAction SilentlyContinue } function Install-Remotely { $HeadResponse = Invoke-WebRequest -Uri "$HostName/Content/Remotely-Win-$Platform.zip" -Method Head -UseBasicParsing $ETag = $HeadResponse.Headers["ETag"] if (!$Etag) { Write-Host "Failed to get ETag from server. Aborting install." } if ((Test-Path -Path "$InstallPath") -and (Test-Path -Path "$InstallPath\ConnectionInfo.json")) { $ConnectionInfo = Get-Content -Path "$InstallPath\ConnectionInfo.json" | ConvertFrom-Json if ($ConnectionInfo) { $ConnectionInfo.Host = $HostName $ConnectionInfo.OrganizationID = $Organization $ConnectionInfo.ServerVerificationToken = "" } } else { New-Item -ItemType Directory -Path "$InstallPath" -Force } if (!$ConnectionInfo) { $ConnectionInfo = @{ DeviceID = (New-Guid).ToString(); Host = $HostName; OrganizationID = $Organization; ServerVerificationToken = ""; } } if ($HostName.EndsWith("/")) { $HostName = $HostName.Substring(0, $HostName.LastIndexOf("/")) } if ($Path) { Write-Log "Copying install files..." Copy-Item -Path $Path -Destination "$env:TEMP\Remotely-Win-$Platform.zip" } else { $ProgressPreference = 'SilentlyContinue' Write-Log "Downloading client..." Invoke-WebRequest -Uri "$HostName/Content/Remotely-Win-$Platform.zip" -OutFile "$env:TEMP\Remotely-Win-$Platform.zip" -UseBasicParsing $ProgressPreference = 'Continue' } if (!(Test-Path -Path "$env:TEMP\Remotely-Win-$Platform.zip")) { Write-Log "Client files failed to download." Do-Exit } Stop-Remotely Get-ChildItem -Path $InstallPath | Where-Object {$_.Name -notlike "ConnectionInfo.json"} | Remove-Item -Recurse -Force Expand-Archive -Path "$env:TEMP\Remotely-Win-$Platform.zip" -DestinationPath "$InstallPath" -Force New-Item -ItemType File -Path "$InstallPath\ConnectionInfo.json" -Value (ConvertTo-Json -InputObject $ConnectionInfo) -Force New-Item -ItemType File -Path "$InstallPath\etag.txt" -Value $ETag -Force if ($DeviceAlias -or $DeviceGroup) { $DeviceSetupOptions = @{ DeviceAlias = $DeviceAlias; DeviceGroupName = $DeviceGroup; OrganizationID = $Organization; DeviceID = $ConnectionInfo.DeviceID; } $Body = $DeviceSetupOptions | ConvertTo-Json Invoke-RestMethod -Method Post -ContentType "application/json" -Uri "$HostName/api/devices" -Body $Body } New-Service -Name "Remotely_Service" -BinaryPathName "$InstallPath\Remotely_Agent.exe" -DisplayName "Remotely Service" -StartupType Automatic -Description "Background service that maintains a connection to the Remotely server. The service is used for remote support and maintenance by this computer's administrators." Start-Process -FilePath "cmd.exe" -ArgumentList "/c sc.exe failure `"Remotely_Service`" reset=5 actions=restart/5000" -Wait -WindowStyle Hidden Start-Service -Name Remotely_Service } try { Run-StartupChecks Write-Log "Install/uninstall logs are being written to `"$LogPath`"" if ($Uninstall) { Write-Log "Uninstall started." Uninstall-Remotely Write-Log "Uninstall completed." exit } else { Write-Log "Install started." Install-Remotely Write-Log "Install completed." exit } } catch { Write-Log -Message "Error occurred: $($Error[0].InvocationInfo.PositionMessage)" Do-Exit }