Skip to content

Instantly share code, notes, and snippets.

@b4tman
Last active April 25, 2017 12:53
Show Gist options
  • Save b4tman/a4ed14e37175dd47acc082abc3846fcf to your computer and use it in GitHub Desktop.
Save b4tman/a4ed14e37175dd47acc082abc3846fcf to your computer and use it in GitHub Desktop.
Скрипт для тестирования доступности и включения/отключения прокси
function SetProxy{
[CmdletBinding()]
param(
[parameter(Mandatory=$false, Position=1)]
[string]$ProxyServer="",
[parameter(Mandatory=$false)]
[switch]$Disable,
[parameter(Mandatory=$false)]
[switch]$Enable
)
if ((-Not $Disable -And $Enable) -And ($Disable -Or $Enable) ){
[int]$val_enable = 0;
if ($Enable) {$val_enable =1}
Set-ItemProperty ‘HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings’ -Name ProxyEnable -Value $val_enable
}
if (-Not "" -eq $ProxyServer){
Set-ItemProperty ‘HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings’ -Name ProxyServer -Value $ProxyServer
}
}
function SetProxy-AutoConfigURL{
[CmdletBinding()]
param(
[parameter(Mandatory=$false, Position=1)]
[string]$AutoConfigURL=""
)
if (-Not "" -eq $AutoConfigURL){
Set-ItemProperty ‘HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings’ -Name AutoConfigURL -Value $AutoConfigURL
} else {
Remove-ItemProperty ‘HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings’ -Name AutoConfigURL
}
}
function IsEnabled-ProxyAutoConfigURL{
[CmdletBinding()]
$result = $false
try{
$ret = Get-ItemProperty ‘HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings’ -Name AutoConfigURL -ErrorAction SilentlyContinue
if ($ret -ne $null) {
$result = $true
}
} catch {
$result = $false
}
return $result
}
function IsEnabled-ProxyServer{
[CmdletBinding()]
$result = $true
try{
$p = Get-ItemProperty ‘HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings’ -Name ProxyEnable
if ($p -eq $null) {
$result = $false
} else {
if ($p.ProxyEnable -eq 0) {
$result = $false
}
}
} catch {
$result = $false
}
return $result
}
function GetTestURL()
{
$arr = [string[]] @('http://ya.ru', 'http://google.com', 'http://rosintel.ru', 'http://www.microsoft.com', 'http://rt.ru', 'http://mail.ru', 'http://yandex.ru', 'http://www.bing.com', 'http://rambler.ru')
return $arr | Get-Random
}
function TestProxy{
[CmdletBinding()]
param(
[parameter(Mandatory=$true, Position=1)]
[string]$ProxyServer="",
[parameter(Mandatory=$false, Position=2)]
[string]$TestURL="http://ya.ru"
)
try {
Invoke-WebRequest -uri $TestURL -Method HEAD -MaximumRedirection 0 -TimeoutSec 14 -UseBasicParsing -UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer) -DisableKeepAlive -Proxy $ProxyServer -ErrorAction Stop | Out-Null
$result = $true
} catch [System.InvalidOperationException] {
if ($_.FullyQualifiedErrorId -match "MaximumRedirectExceeded") {
$result = $true
} else {
$result = $false
}
} catch {
$result = $false
}
return $result
}
#script config
[string]$Proxy_Server = ""
[string]$Proxy_AutoConfigURL = ""
#--------------------------------
[bool]$Last_ProxyReady = $False
[bool]$Last_ProxyEnabled = $False
[bool]$First_Entry = $True
while (1) {
[string]$TestURL = GetTestURL
[bool]$ProxyReady = TestProxy $Proxy_Server $TestURL
[bool]$ProxyEnabled = IsEnabled-ProxyAutoConfigURL
if ( $First_Entry )
{
$First_Entry = $False
} elseif ( ($Last_ProxyReady -eq $ProxyReady) -and ($Last_ProxyEnabled -eq $ProxyEnabled) )
{
Start-Sleep 15
continue
}
[string]$DateSt = Get-Date -Format HH:mm:ss
[string]$ProxyReady_Color = "Red"
[string]$ProxyEnabled_Color = "Red"
[string]$ProxyReady_Text = "Недоступен"
[string]$ProxyEnabled_Text = "Отключен"
if ($ProxyReady) {
$ProxyReady_Color = "Green"
$ProxyReady_Text = "Готов "
}
if ($ProxyEnabled) {
$ProxyEnabled_Color = "Green"
$ProxyEnabled_Text = "Включен "
}
Write-Host "| ${DateSt} |" -NoNewline
Write-Host "|`t${ProxyReady_Text}`t|" -ForegroundColor $ProxyReady_Color -NoNewline
Write-Host "|`t${ProxyEnabled_Text}`t|" -ForegroundColor $ProxyEnabled_Color -NoNewline
Write-Host "|`t${TestURL}"
if ($ProxyReady) {
if (-not $ProxyEnabled) {
Write-Host "---> Включение" -ForegroundColor Green
SetProxy-AutoConfigURL $Proxy_AutoConfigURL
Set3ProxyParent $Proxy_3P_Parent
}
} else { # proxy not ready
if ($ProxyEnabled) {
Write-Host "---> Отключение" -ForegroundColor Red
SetProxy-AutoConfigURL
Set3ProxyParent
}
}
$Last_ProxyReady = $ProxyReady
$Last_ProxyEnabled = $ProxyEnabled
Start-Sleep 15
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment