Public/Proxy/Set-CWAAProxy.ps1
Function Set-CWAAProxy{ [CmdletBinding(SupportsShouldProcess=$True)] [Alias('Set-LTProxy')] Param( [parameter(Mandatory = $False, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, Position = 0)] [string]$ProxyServerURL, [parameter(Mandatory = $False, ValueFromPipeline = $False, ValueFromPipelineByPropertyName = $True, Position = 1)] [string]$ProxyUsername, [parameter(Mandatory = $False, ValueFromPipeline = $False, ValueFromPipelineByPropertyName = $True, Position = 2)] [SecureString]$ProxyPassword, [parameter(Mandatory = $False, ValueFromPipeline = $False, ValueFromPipelineByPropertyName = $True)] [string]$EncodedProxyUsername, [parameter(Mandatory = $False, ValueFromPipeline = $False, ValueFromPipelineByPropertyName = $True)] [SecureString]$EncodedProxyPassword, [parameter(Mandatory = $False, ValueFromPipeline = $False, ValueFromPipelineByPropertyName = $True)] [alias('Detect')] [alias('AutoDetect')] [switch]$DetectProxy, [parameter(Mandatory = $False, ValueFromPipeline = $False, ValueFromPipelineByPropertyName = $True)] [alias('Clear')] [alias('Reset')] [alias('ClearProxy')] [switch]$ResetProxy ) Begin { Clear-Variable LTServiceSettingsChanged,LTSS,LTServiceRestartNeeded,proxyURL,proxyUser,proxyPass,passwd,Svr -EA 0 -WhatIf:$False -Confirm:$False #Clearing Variables for use Write-Debug "Starting $($myInvocation.InvocationName) at line $(LINENUM)" try { $LTSS=Get-CWAASettings -EA 0 -Verbose:$False -WA 0 -Debug:$False } catch {} } Process{ If ( (($ResetProxy -eq $True) -and (($DetectProxy -eq $True) -or ($ProxyServerURL) -or ($ProxyUsername) -or ($ProxyPassword) -or ($EncodedProxyUsername) -or ($EncodedProxyPassword))) -or (($DetectProxy -eq $True) -and (($ResetProxy -eq $True) -or ($ProxyServerURL) -or ($ProxyUsername) -or ($ProxyPassword) -or ($EncodedProxyUsername) -or ($EncodedProxyPassword))) -or ((($ProxyServerURL) -or ($ProxyUsername) -or ($ProxyPassword) -or ($EncodedProxyUsername) -or ($EncodedProxyPassword)) -and (($ResetProxy -eq $True) -or ($DetectProxy -eq $True))) -or ((($ProxyUsername) -or ($ProxyPassword)) -and (-not ($ProxyServerURL) -or ($EncodedProxyUsername) -or ($EncodedProxyPassword) -or ($ResetProxy -eq $True) -or ($DetectProxy -eq $True))) -or ((($EncodedProxyUsername) -or ($EncodedProxyPassword)) -and (-not ($ProxyServerURL) -or ($ProxyUsername) -or ($ProxyPassword) -or ($ResetProxy -eq $True) -or ($DetectProxy -eq $True))) ) {Write-Error "ERROR: Line $(LINENUM): Set-CWAAProxy: Invalid Parameter specified" -ErrorAction Stop} If (-not (($ResetProxy -eq $True) -or ($DetectProxy -eq $True) -or ($ProxyServerURL) -or ($ProxyUsername) -or ($ProxyPassword) -or ($EncodedProxyUsername) -or ($EncodedProxyPassword))) { If ($Args.Count -gt 0) {Write-Error "ERROR: Line $(LINENUM): Set-CWAAProxy: Unknown Parameter specified" -ErrorAction Stop} Else {Write-Error "ERROR: Line $(LINENUM): Set-CWAAProxy: Required Parameters Missing" -ErrorAction Stop} } Try{ If ($($ResetProxy) -eq $True) { Write-Verbose "ResetProxy selected. Clearing Proxy Settings." If ( $PSCmdlet.ShouldProcess("LTProxy", "Clear") ) { $Script:LTProxy.Enabled=$False $Script:LTProxy.ProxyServerURL='' $Script:LTProxy.ProxyUsername='' $Script:LTProxy.ProxyPassword='' $Script:LTWebProxy=New-Object System.Net.WebProxy $Script:LTServiceNetWebClient.Proxy=$Script:LTWebProxy } } ElseIf ($($DetectProxy) -eq $True) { Write-Verbose "DetectProxy selected. Attempting to Detect Proxy Settings." If ( $PSCmdlet.ShouldProcess("LTProxy", "Detect") ) { $Script:LTWebProxy=[System.Net.WebRequest]::GetSystemWebProxy() $Script:LTProxy.Enabled=$False $Script:LTProxy.ProxyServerURL='' $Servers = @($("$($LTSS|Select-Object -Expand 'ServerAddress' -EA 0)|www.connectwise.com").Split('|')|ForEach-Object {$_.Trim()}) Foreach ($Svr In $Servers) { If (-not ($Script:LTProxy.Enabled)) { If ($Svr -match '^(https?://)?(([12]?[0-9]{1,2}\.){3}[12]?[0-9]{1,2}|[a-z0-9][a-z0-9_-]*(\.[a-z0-9][a-z0-9_-]*)*)$') { $Svr = $Svr -replace 'https?://','' Try{ $Script:LTProxy.ProxyServerURL=$Script:LTWebProxy.GetProxy("http://$($Svr)").Authority } catch {} If (($Null -ne $Script:LTProxy.ProxyServerURL) -and ($Script:LTProxy.ProxyServerURL -ne '') -and ($Script:LTProxy.ProxyServerURL -notcontains "$($Svr)")) { Write-Debug "Line $(LINENUM): Detected Proxy URL: $($Script:LTProxy.ProxyServerURL) on server $($Svr)" $Script:LTProxy.Enabled=$True } } } } If (-not ($Script:LTProxy.Enabled)) { if (($Script:LTProxy.ProxyServerURL -eq '') -or ($Script:LTProxy.ProxyServerURL -contains '$Svr')) { $Script:LTProxy.ProxyServerURL = netsh winhttp show proxy | select-string -pattern '(?i)(?<=Proxyserver.*http\=)([^;\r\n]*)' -EA 0|ForEach-Object {$_.matches}|Select-Object -Expand value } if (($Null -eq $Script:LTProxy.ProxyServerURL) -or ($Script:LTProxy.ProxyServerURL -eq '')) { $Script:LTProxy.ProxyServerURL='' $Script:LTProxy.Enabled=$False } else { $Script:LTProxy.Enabled=$True Write-Debug "Line $(LINENUM): Detected Proxy URL: $($Script:LTProxy.ProxyServerURL)" } } $Script:LTProxy.ProxyUsername='' $Script:LTProxy.ProxyPassword='' $Script:LTServiceNetWebClient.Proxy=$Script:LTWebProxy } } ElseIf (($ProxyServerURL)) { If ( $PSCmdlet.ShouldProcess("LTProxy", "Set") ) { foreach ($ProxyURL in $ProxyServerURL) { $Script:LTWebProxy = New-Object System.Net.WebProxy($ProxyURL, $true); $Script:LTProxy.Enabled=$True $Script:LTProxy.ProxyServerURL=$ProxyURL } Write-Verbose "Setting Proxy URL to: $($ProxyServerURL)" If ((($ProxyUsername) -and ($ProxyPassword)) -or (($EncodedProxyUsername) -and ($EncodedProxyPassword))) { If (($ProxyUsername)) { foreach ($proxyUser in $ProxyUsername) { $Script:LTProxy.ProxyUsername=$proxyUser } } If (($EncodedProxyUsername)) { foreach ($proxyUser in $EncodedProxyUsername) { $Script:LTProxy.ProxyUsername=$(ConvertFrom-CWAASecurity -InputString "$($proxyUser)" -Key ("$($Script:LTServiceKeys.PasswordString)",'')) } } If (($ProxyPassword)) { foreach ($proxyPass in $ProxyPassword) { $Script:LTProxy.ProxyPassword=$proxyPass $passwd = ConvertTo-SecureString $proxyPass -AsPlainText -Force; ## Website credentials } } If (($EncodedProxyPassword)) { foreach ($proxyPass in $EncodedProxyPassword) { $Script:LTProxy.ProxyPassword=$(ConvertFrom-CWAASecurity -InputString "$($proxyPass)" -Key ("$($Script:LTServiceKeys.PasswordString)",'')) $passwd = ConvertTo-SecureString $Script:LTProxy.ProxyPassword -AsPlainText -Force; ## Website credentials } } $Script:LTWebProxy.Credentials = New-Object System.Management.Automation.PSCredential ($Script:LTProxy.ProxyUsername, $passwd); } $Script:LTServiceNetWebClient.Proxy=$Script:LTWebProxy } } } Catch{ Write-Error "ERROR: Line $(LINENUM): There was an error during the Proxy Configuration process. $($Error[0])" -ErrorAction Stop } } End{ If ($?){ $LTServiceSettingsChanged=$False If ($Null -ne ($LTSS)) { If (($LTSS|Get-Member|Where-Object {$_.Name -eq 'ProxyServerURL'})) { If (($($LTSS|Select-Object -Expand ProxyServerURL -EA 0) -replace 'https?://','' -ne $Script:LTProxy.ProxyServerURL) -and (($($LTSS|Select-Object -Expand ProxyServerURL -EA 0) -replace 'https?://','' -eq '' -and $Script:LTProxy.Enabled -eq $True -and $Script:LTProxy.ProxyServerURL -match '.+\..+') -or ($($LTSS|Select-Object -Expand ProxyServerURL -EA 0) -replace 'https?://','' -ne '' -and ($Script:LTProxy.ProxyServerURL -ne '' -or $Script:LTProxy.Enabled -eq $False)))) { Write-Debug "Line $(LINENUM): ProxyServerURL Changed: Old Value: $($LTSS|Select-Object -Expand ProxyServerURL -EA 0) New Value: $($Script:LTProxy.ProxyServerURL)" $LTServiceSettingsChanged=$True } If (($LTSS|Get-Member|Where-Object {$_.Name -eq 'ProxyUsername'}) -and ($LTSS|Select-Object -Expand ProxyUsername -EA 0)) { If ($(ConvertFrom-CWAASecurity -InputString "$($LTSS|Select-Object -Expand ProxyUsername -EA 0)" -Key ("$($Script:LTServiceKeys.PasswordString)",'')) -ne $Script:LTProxy.ProxyUsername) { Write-Debug "Line $(LINENUM): ProxyUsername Changed: Old Value: $(ConvertFrom-CWAASecurity -InputString "$($LTSS|Select-Object -Expand ProxyUsername -EA 0)" -Key ("$($Script:LTServiceKeys.PasswordString)",'')) New Value: $($Script:LTProxy.ProxyUsername)" $LTServiceSettingsChanged=$True } } If ($Null -ne ($LTSS) -and ($LTSS|Get-Member|Where-Object {$_.Name -eq 'ProxyPassword'}) -and ($LTSS|Select-Object -Expand ProxyPassword -EA 0)) { If ($(ConvertFrom-CWAASecurity -InputString "$($LTSS|Select-Object -Expand ProxyPassword -EA 0)" -Key ("$($Script:LTServiceKeys.PasswordString)",'')) -ne $Script:LTProxy.ProxyPassword) { Write-Debug "Line $(LINENUM): ProxyPassword Changed: Old Value: $(ConvertFrom-CWAASecurity -InputString "$($LTSS|Select-Object -Expand ProxyPassword -EA 0)" -Key ("$($Script:LTServiceKeys.PasswordString)",'')) New Value: $($Script:LTProxy.ProxyPassword)" $LTServiceSettingsChanged=$True } } } ElseIf ($Script:LTProxy.Enabled -eq $True -and $Script:LTProxy.ProxyServerURL -match '(https?://)?.+\..+') { Write-Debug "Line $(LINENUM): ProxyServerURL Changed: Old Value: NOT SET New Value: $($Script:LTProxy.ProxyServerURL)" $LTServiceSettingsChanged=$True } } Else { $svcRun = ('LTService') | Get-Service -EA 0 | Where-Object {$_.Status -eq 'Running'} | Measure-Object | Select-Object -Expand Count if (($svcRun -gt 0) -and ($($Script:LTProxy.ProxyServerURL) -match '.+')) { $LTServiceSettingsChanged=$True } } If ($LTServiceSettingsChanged -eq $True) { If ((Get-Service 'LTService','LTSvcMon' -ErrorAction SilentlyContinue|Where-Object {$_.Status -match 'Running'})) { $LTServiceRestartNeeded=$True; try {Stop-CWAA -EA 0 -WA 0} catch {} } Write-Verbose "Updating LabTech\Service\Settings Proxy Configuration." If ( $PSCmdlet.ShouldProcess("LTService Registry", "Update") ) { $Svr=$($Script:LTProxy.ProxyServerURL); If (($Svr -ne '') -and ($Svr -notmatch 'https?://')) {$Svr = "http://$($Svr)"} @{"ProxyServerURL"=$Svr; "ProxyUserName"="$(ConvertTo-CWAASecurity -InputString "$($Script:LTProxy.ProxyUserName)" -Key "$($Script:LTServiceKeys.PasswordString)")"; "ProxyPassword"="$(ConvertTo-CWAASecurity -InputString "$($Script:LTProxy.ProxyPassword)" -Key "$($Script:LTServiceKeys.PasswordString)")"}.GetEnumerator() | Foreach-Object { Write-Debug "Line $(LINENUM): Setting Registry value for $($_.Name) to `"$($_.Value)`"" Set-ItemProperty -Path 'HKLM:Software\LabTech\Service\Settings' -Name $($_.Name) -Value $($_.Value) -EA 0 -Confirm:$False } } If ($LTServiceRestartNeeded -eq $True) { try {Start-CWAA -EA 0 -WA 0} catch {} } } } Else {$Error[0]} Write-Debug "Exiting $($myInvocation.InvocationName) at line $(LINENUM)" } } |