# encoding: ascii
# api: powershell
# title: Set-Proxy
# description: get and or set proxy thru remote registry
# version: 0.1
# type: function
# author: chriskenis
# license: CC0
# x-poshcode-id: 3795
# x-archived: 2012-12-03T17:07:57
# x-published: 2012-11-28T18:45:00
#
#
param(
[Parameter(Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)]
[alias("Name","ComputerName")] $Computer = @($env:computername),
[switch] $ClearProxy
)

begin{
$IEsettingsKey = ".DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
[array]$ProxyResult = @()

function SetProxySettings($Computer){
	Set-RegDWord -ComputerName $Computer -Hive Users -Key $IEsettingsKey -Value ProxyEnable -Data 0 -Force
	Set-RegString -ComputerName $Computer -Hive Users -Key $IEsettingsKey -Value ProxyServer -Force
	Set-RegString -ComputerName $Computer -Hive Users -Key $IEsettingsKey -Value ProxyOverride -Force
	Set-RegBinary -ComputerName $Computer -Hive Users -Key "$IEsettingsKey\Connections" -Value DefaultConnectionSettings -Force
	Set-RegBinary -ComputerName $Computer -Hive Users -Key "$IEsettingsKey\Connections" -Value SavedLegacySettings -Force
}

function GetProxySettings($Computer){
	$Output = @()
	$Output += (Get-RegDWord -ComputerName $Computer -Hive Users -Key $IEsettingsKey -Value "ProxyEnable" | Select-Object ComputerName,Value,Data)
	$Output += (Get-RegString -ComputerName $Computer -Hive Users -Key $IEsettingsKey -Value "ProxyServer" | Select-Object ComputerName,Value,Data)
	$Output += (Get-RegString -ComputerName $Computer -Hive Users -Key $IEsettingsKey -Value "ProxyOverride" | Select-Object ComputerName,Value,Data)
	$Output += (Get-RegBinary -ComputerName $Computer -Hive Users -Key "$IEsettingsKey\Connections" -Value "DefaultConnectionSettings" | Select-Object ComputerName,Value,Data)
	$Output += (Get-RegBinary -ComputerName $Computer -Hive Users -Key "$IEsettingsKey\Connections" -Value "SavedLegacySettings" | Select-Object ComputerName,Value,Data)
	return $Output
}
}

process{
if (Test-connection $Computer -quiet -count 1){
	Try {
		$ProxyResult += GetProxySettings $Computer
		if ($ClearProxy){SetProxySettings $Computer; $ProxyResult += GetProxySettings $Computer}
		}
	Catch {
		Write-Warning "$($error[0]) "
		Break
		}
	}
}

end{
$ProxyResult
}
