function Get-RegistryValue { param ( [Parameter(Mandatory)] [string]$RegistryPath, [Parameter(Mandatory)] [string]$RegistryValue ) try { if (Test-Path -Path $RegistryPath -PathType Container) { $key = Get-Item -Path $RegistryPath -ErrorAction Stop $value = $key.GetValue($RegistryValue, $null) return $value } } catch {} } try { $arch = ((Get-CimInstance -ClassName Win32_OperatingSystem).OSArchitecture).substring(0, 2) } catch { switch ([System.Environment]::Is64BitOperatingSystem) { $true { $arch = '64' } $false { $arch = '32' } } } switch ($arch) { '32' { $registryPath = 'HKLM:\SOFTWARE\Action1' } '64' { $registryPath = 'HKLM:\SOFTWARE\WOW6432Node\Action1' } } $proxy = Get-RegistryValue -RegistryPath $registryPath -RegistryValue "agent.use.proxy" $hostname = Get-RegistryValue -RegistryPath $registryPath -RegistryValue "agent.proxy.hostname" $port = Get-RegistryValue -RegistryPath $registryPath -RegistryValue "agent.proxy.port" $username = Get-RegistryValue -RegistryPath $registryPath -RegistryValue "agent.proxy.username" $password = Get-RegistryValue -RegistryPath $registryPath -RegistryValue "agent.proxy.password" if ($null -eq $proxy -or $proxy.ToLower() -ne 'yes') { $Host.UI.WriteLine("The proxy is not configured.") return } New-ItemProperty -Path $registryPath -Name 'agent.use.proxy' -Value 'yes' -PropertyType String -Force if (($null -eq $port) -or ([string]::IsNullOrEmpty($hostname) -or $port -eq 0)) { $Host.UI.WriteLine("The proxy host name or port is not specified.") return } $argument = "winhttp set proxy $hostname" + ":" + [string]$port Start-Process -FilePath netsh -ArgumentList $argument -NoNewWindow -Wait