Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force function RXUninstallQualysAgent { #start: global variables $ScriptDir = Get-Location $homepath = Get-ChildItem -Path "$ScriptDir" #setting up folder directory variable based on $ScriptDir #end: global variables #removes existing RXUninstallQualysAgentResult.txt pior running all checks if ($homepath.Name -eq "RXUninstallQualysAgentResult.txt") { Get-ChildItem -Path $ScriptDir\RXUninstallQualysAgentResult.txt | Remove-Item -Force } #check required script if ($ScriptDir.Name -eq 'RXUninstallQualysAgent_module.ps1') { Write-host "Required script found." } else { Write-Host 'Required script not found!' Start-Sleep 2 Write-Host 'Downloading required script from Cloud' Invoke-WebRequest -Uri 'url.domain.com' ` -OutFile "$ScriptDir\RXUninstallQualysAgent_module.ps1" } Get-PSSession | Remove-PSSession -ErrorAction Ignore #removes any active powershell remote session #start: text file test Write-Host "`nFinding RXUninstallQualysAgentTarget.txt..." if ($homepath.Name -eq "RXUninstallQualysAgentTarget.txt") { Start-Sleep -Seconds 2 Write-Host "PASSED: RXUninstallQualysAgentTarget.txt is found." -ForegroundColor Green #start: connectivity test Write-Host "`nValidating VPN connectivity..." $corp_lan = Test-Connection -ComputerName "domain.com" -Count 2 -ErrorAction Ignore if ($corp_lan.StatusCode -eq "0") { Start-Sleep -Seconds 2 Write-Host "PASSED: PC is connected to VPN." -ForegroundColor Green #start: credential test Write-Host "`nValidating credentials..." try { $creds = Get-Credential -Message "FORMAT: domain\a-account" -ErrorAction Stop $psremote = New-PSSession -ComputerName "REXEU1MANP004.b2b.regn.net" ` -Credential $creds -ErrorAction Ignore #creating powershell remote session if ($psremote.ComputerName -eq "server.domain.com") { Start-Sleep -Seconds 2 Write-Host "PASSED: Credentials are valid." -ForegroundColor Green Invoke-Command -Session $psremote -ScriptBlock { if ((Get-ChildItem -Path "C:\").Name -eq "rxtemp") {} } if ((Get-ChildItem -Path "C:\").Name -eq "rxtemp") { $remotecommand_uninstall = { Invoke-Command -ScriptBlock { c:\RXtemp\RXUninstallQualysAgent_module.ps1 } ` -Credential $using:creds } #command to run on remote host Copy-Item -Path $ScriptDir\RXUninstallQualysAgent_module.ps1 -ToSession $psremote ` -Destination "C:\RXtemp" #copy script module Copy-Item -Path $ScriptDir\RXUninstallQualysAgentTarget.txt -ToSession $psremote ` -Destination "C:\RXtemp" #copy target file Write-Host "`nUninstalling Qualys agent..." Invoke-Command -Session $psremote -ScriptBlock $remotecommand_uninstall Copy-Item -FromSession $psremote "C:\rxtemp\RXUninstallQualysAgentResult.txt" -Destination "$ScriptDir\" Write-Host "`nCOMPLETED: Result extract can be found at $ScriptDir\RXUninstallQualysAgentResult.tx" -ForegroundColor Yellow Start-Sleep -Seconds 8 } else { Invoke-Command -Session $psremote -ScriptBlock { New-Item -Path "c:\" -Name "RXtemp" -ItemType Directory} -Credential $creds $remotecommand_uninstall = { Invoke-Command -ScriptBlock { c:\temp\RXUninstallQualysAgent_module.ps1 }` -Credential $using:creds } #command to run on remote host Copy-Item -Path $ScriptDir\RXUninstallQualysAgent_module.ps1 -ToSession $psremote ` -Destination "C:\RXtemp" #copy script module Copy-Item -Path $ScriptDir\RXUninstallQualysAgentTarget.txt -ToSession $psremote ` -Destination "C:\RXtemp" #copy target file Write-Host "`nUninstalling Qualys agent..." Invoke-Command -Session $psremote -ScriptBlock $remotecommand_uninstall Copy-Item -FromSession $psremote "c:\rxtemp\RXUninstallQualysAgentResult.txt" -Destination "$ScriptDir\" Write-Host "`nCOMPLETED: Result extract can be found at $ScriptDir\RXUninstallQualysAgentResult.tx" -ForegroundColor Yellow Start-Sleep -Seconds 8 } } #end: credential test else { Start-Sleep -Seconds 2 Write-Host "FAILED: Wrong password/user name. Please try again." -ForegroundColor Red Start-Sleep -Seconds 8 } ##+ } catch { Write-Host "FAILED: Blank password/user name. Please try again." -ForegroundColor Red Start-Sleep -Seconds 8 } } #end: connectivity test else { Start-Sleep -Seconds 2 Write-Host "FAILED: Please connect first to VPN and try again." -ForegroundColor Red Start-Sleep -Seconds 8 } } #end: text file test else { Start-Sleep -Seconds 2 Write-Host "FAILED: RXUninstallQualysAgentTarget.txt not found." -ForegroundColor Red Write-Host "Make sure RXUninstallQualysAgentTarget.txt is present where this script is run." -ForegroundColor Red Start-Sleep -Seconds 5 } Get-PSSession | Remove-PSSession -ErrorAction Ignore #removes any active powershell remote session } RXUninstallQualysAgent #function call