forked from schtritoff/hyperv-vm-provisioning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set-PowershellProxy.ps1
40 lines (37 loc) · 1.22 KB
/
Set-PowershellProxy.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<#
.SYNOPSIS
Set proxy settings for powershell session
.EXAMPLE
Corporate proxy using integrated authentication:
Set-PowershellProxy "proxy.domain.local" "9090" $true
#>
function Set-PowershellProxy {
[CmdletBinding()]
param (
[string] $proxy_host,
[int] $proxy_port,
[bool] $useDefaultCredentials = $false,
[switch] $Reset = $false
)
if ($Reset) {
$global:PSDefaultParameterValues = @{}
Write-Host "Proxy settings reset"
return $true
}
# if proxy available, use it
#if(Test-Connection $proxy_host -Count 1 -Quiet) # only ping
$script:nc = Test-NetConnection -Computername $proxy_host -Port $proxy_port
if ($script:nc.TcpTestSucceeded)
{
$global:PSDefaultParameterValues = @{
'Invoke-RestMethod:Proxy'="http://$($proxy_host):$($proxy_port)"
'Invoke-WebRequest:Proxy'="http://$($proxy_host):$($proxy_port)"
'*:ProxyUseDefaultCredentials'=$useDefaultCredentials
}
Write-Host "Proxy configured $($proxy_host):$($proxy_port)"
return $true
} else {
Write-Host "Proxy $($proxy_host):$($proxy_port) not available"
}
return $false
}