OpsaC - Operating as PowerShell code
published: May 27, 2021 author: Tinu tags: PowerShell categories: PowerShell-Basic
Thers are many possibilities for proxy addresses in a Windows Server:
netsh winhttp show proxy
$env:HTTP_PROXY
$env:HTTPS_PROXY
Puppet and Chocolatey raise an error, if there a proxy set.
Using proxy server 'http://proxy.domain.com:8080'
[NuGet] Not abel to contact source 'https://myserver.domain.com/source'. The server commited a protocol violation. Section=ResponseStatusLine
$env:NO_PROXY
Set the NO_PROXY variable for the current session
$env:NO_PROXY = 'myserver.domain.com'
Set the NO_PROXY variable persistently
[System.Environment]::SetEnvironmentVariable('NO_PROXY','myserver.domain.com',[System.EnvironmentVariableTarget]::Machine)
You can set a proxy for PowerShell to perform web requests in the Invoke-WebRequest, Find-Module, Install-Module, etc. cmdlets.
In Windows PowerShell 5x, the System.Net.WebRequest class is used to perform web requests.
Accordingly, to set proxy server settings in Windows PowerShell, you need to use the command:
$Proxy = 'http://your-proxy:tcp-port'
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
[System.Net.WebRequest]::DefaultWebProxy = New-Object System.Net.WebProxy($Proxy)
[System.Net.WebRequest]::DefaultWebProxy.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
[System.Net.WebRequest]::DefaultWebProxy.BypassProxyOnLocal = $true
In new versions of PowerShell 7.x, the System.Net.HttpClient class is used instead of the System.Net.WebRequest class to perform web requests.
Accordingly, to set proxy server settings in PowerShell Core, you need to use the command:
$Proxy = 'http://your-proxy:tcp-port'
[System.Net.Http.HttpClient]::DefaultProxy = New-Object System.Net.WebProxy($Proxy)
[System.Net.Http.HttpClient]::DefaultProxy.BypassProxyOnLocal = $true
Windows Sysinternals on Microsoft Docs