Search This Blog

2020/05/27

Proxy and login sample | PowerShell

# proxy
# password must be secure string
$password = "yourPassword" | ConvertTo-SecureString -AsPlainText -Force
$proxyCred = New-Object System.Management.Automation.PsCredential("$uname@domain.tld", $password)
$proxyUrl = "your proxy url"
$proxy = New-Object System.Net.WebProxy $proxyUrl
$proxy.Credentials = $proxyCred
[System.Net.WebRequest]::DefaultWebProxy = $proxy
Write-Host("Proxy setting")

# login
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
$browser = New-Object System.Net.WebClient
$browser.Proxy.Credentials = $proxyCred

$loginUrl = "login url"
$loginPassword= "yourLoginPassword" |ConvertTo-SecureString -AsPlainText -Force
$BSTR = [Sysrem.Runtime.InteropServices.Marshal]::SecureStringToBSTR($loginPassword)
$unsecurePassword = [System.Rutime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$postParams = @{
  username = "username"
  password = $unsecurePassword
}

$Auth = Invoke-RestMethod -Uri $loginUrl -Method POST -Body $postParams
# $result = Invoke-RestMethod -Uri $loginUrl -Method GET -Headers $headers