Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View Tinus IT Wiki

View my Photo Website

Proxy

published: May 27, 2021 author: Tinu tags: PowerShell categories: PowerShell-Basic


Table of Contents

Proxy variables

Thers are many possibilities for proxy addresses in a Windows Server:

System proxy

netsh winhttp show proxy

HTTP proxy

$env:HTTP_PROXY
$env:HTTPS_PROXY

The Problem

Puppet and Chocolatey raise an error, if there a proxy set.

Puppet

Using proxy server 'http://proxy.domain.com:8080'

Chocolatey

[NuGet] Not abel to contact source 'https://myserver.domain.com/source'. The server commited a protocol violation. Section=ResponseStatusLine

Bypass HTTP proxy

$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)

PowerShell Proxy

You can set a proxy for PowerShell to perform web requests in the Invoke-WebRequest, Find-Module, Install-Module, etc. cmdlets.

Proxy for Windows PowerShell

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

Proxy for PowerShell Core

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

See also

Windows Sysinternals on Microsoft Docs


← Previous Post [ Top ] Copyright © 2024 by tinuwalther [ Blog ] Next Post →