PowerShell'de Proxy Kullanımı
PowerShell'de Proxy'ler
PowerShell, Windows'ta güçlü bir otomasyon aracıdır. Kurumsal ağlarda çalışan veya anonimlik gerektiren betikler için proxy'lerle çalışmak kritik önemdedir.
Proxy ile Invoke-WebRequest
Proxy üzerinden temel istek
$proxy = "http://proxy_ip:8080"
$response = Invoke-WebRequest -Uri "https://httpbin.org/ip" -Proxy $proxy
$response.Content
Kimlik doğrulamalı
$proxy = "http://proxy_ip:8080"
$proxyCred = New-Object System.Management.Automation.PSCredential(
"username",
(ConvertTo-SecureString "password" -AsPlainText -Force)
)
$response = Invoke-WebRequest -Uri "https://httpbin.org/ip" `
-Proxy $proxy `
-ProxyCredential $proxyCred
$response.Content
Invoke-RestMethod (JSON API için)
$proxy = "http://proxy_ip:8080"
$result = Invoke-RestMethod -Uri "https://httpbin.org/ip" -Proxy $proxy
$result.origin # proxy üzerinden görünen IP adresiniz
System.Net.WebProxy
Programatik yapılandırma
$webProxy = New-Object System.Net.WebProxy("http://proxy_ip:8080", $true)
$webProxy.Credentials = New-Object System.Net.NetworkCredential("user", "pass")
# WebClient'a uygula
$client = New-Object System.Net.WebClient
$client.Proxy = $webProxy
$result = $client.DownloadString("https://httpbin.org/ip")
Write-Host $result
Belirli adresler için proxy'yi atlama
$webProxy = New-Object System.Net.WebProxy("http://proxy_ip:8080", $true)
$webProxy.BypassList = @("localhost", "*.local", "192.168.*")
$webProxy.BypassProxyOnLocal = $true
HttpClient (.NET)
Gelişmiş senaryolar için .NET HttpClient'ı kullanın:
$proxy = New-Object System.Net.WebProxy("http://proxy_ip:8080")
$proxy.Credentials = New-Object System.Net.NetworkCredential("user", "pass")
$handler = New-Object System.Net.Http.HttpClientHandler
$handler.Proxy = $proxy
$handler.UseProxy = $true
$client = New-Object System.Net.Http.HttpClient($handler)
$response = $client.GetStringAsync("https://httpbin.org/ip").Result
Write-Host $response
Sistem Proxy'si
Mevcut ayarları okuma
# Windows kayıt defterinden
$regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
$proxyEnabled = (Get-ItemProperty -Path $regPath).ProxyEnable
$proxyServer = (Get-ItemProperty -Path $regPath).ProxyServer
Write-Host "Proxy enabled: $proxyEnabled"
Write-Host "Proxy server: $proxyServer"
Sistem proxy'sini ayarlama
$regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
# Proxy'yi etkinleştir
Set-ItemProperty -Path $regPath -Name ProxyEnable -Value 1
Set-ItemProperty -Path $regPath -Name ProxyServer -Value "proxy_ip:8080"
# İstisnalar
Set-ItemProperty -Path $regPath -Name ProxyOverride -Value "localhost;*.local"
Write-Host "Proxy configured"
Sistem proxy'sini devre dışı bırakma
Set-ItemProperty -Path $regPath -Name ProxyEnable -Value 0
Write-Host "Proxy disabled"
Ortam Değişkenleri
Ayarlama
$env:HTTP_PROXY = "http://proxy_ip:8080"
$env:HTTPS_PROXY = "http://proxy_ip:8080"
$env:NO_PROXY = "localhost,*.local"
Kalıcı ayarlama
[Environment]::SetEnvironmentVariable("HTTP_PROXY", "http://proxy_ip:8080", "User")
[Environment]::SetEnvironmentVariable("HTTPS_PROXY", "http://proxy_ip:8080", "User")
Doğrulama
Write-Host "HTTP_PROXY: $env:HTTP_PROXY"
Write-Host "HTTPS_PROXY: $env:HTTPS_PROXY"
Betiklerde Proxy Rotasyonu
$proxies = @(
"http://user:pass@proxy1:8080",
"http://user:pass@proxy2:8080",
"http://user:pass@proxy3:8080"
)
$urls = @(
"https://httpbin.org/ip",
"https://httpbin.org/headers",
"https://httpbin.org/user-agent"
)
foreach ($url in $urls) {
$proxy = $proxies | Get-Random
try {
$response = Invoke-RestMethod -Uri $url -Proxy $proxy -TimeoutSec 10
Write-Host "OK: $url via $proxy"
Write-Host $response
} catch {
Write-Host "FAIL: $url via $proxy - $($_.Exception.Message)"
}
}
Proxy Testi
function Test-Proxy {
param(
[string]$ProxyServer,
[string]$ProxyPort,
[string]$Username,
[string]$Password
)
$proxy = "http://${ProxyServer}:${ProxyPort}"
$cred = $null
if ($Username) {
$cred = New-Object System.Management.Automation.PSCredential(
$Username,
(ConvertTo-SecureString $Password -AsPlainText -Force)
)
}
try {
$params = @{
Uri = "https://httpbin.org/ip"
Proxy = $proxy
TimeoutSec = 10
}
if ($cred) { $params.ProxyCredential = $cred }
$response = Invoke-RestMethod @params
Write-Host "OK - IP: $($response.origin)" -ForegroundColor Green
return $true
} catch {
Write-Host "FAIL - $($_.Exception.Message)" -ForegroundColor Red
return $false
}
}
# Kullanım
Test-Proxy -ProxyServer "proxy_ip" -ProxyPort "8080" -Username "user" -Password "pass"
Toplu Proxy Testi
$proxyList = Get-Content "proxies.txt" # biçim: ip:port:user:pass
$results = foreach ($line in $proxyList) {
$parts = $line.Split(":")
$ip = $parts[0]; $port = $parts[1]
$user = $parts[2]; $pass = $parts[3]
$proxy = "http://${ip}:${port}"
try {
$cred = New-Object PSCredential($user, (ConvertTo-SecureString $pass -AsPlainText -Force))
$resp = Invoke-RestMethod "https://httpbin.org/ip" -Proxy $proxy -ProxyCredential $cred -TimeoutSec 5
[PSCustomObject]@{Proxy=$line; Status="OK"; IP=$resp.origin}
} catch {
[PSCustomObject]@{Proxy=$line; Status="FAIL"; IP="N/A"}
}
}
$results | Format-Table -AutoSize
$results | Export-Csv "proxy_check_results.csv" -NoTypeInformation
Sonuç
PowerShell, proxy'lerle çalışmak için esnek araçlar sunar: basit cmdlet'lerden (Invoke-WebRequest) tam .NET API'sine kadar. Sistem proxy'sini kayıt defteri ve ortam değişkenleri üzerinden yönetebilme imkânı, PowerShell'i Windows makinelerinde proxy yapılandırmasını otomatikleştirmek için ideal kılar.
