Powershell: Webrequest with Basic Authentication

Theory:
Practise:
# Credentials
$Username="user"
$Password="pass"
# creating pair string with scheme user:password
$Pair = "$($Username):$($Password)"
# encode pair to ascii bytes octet
$Bytes=[System.Text.Encoding]::ASCII.GetBytes($Pair)
# convert bytes string to Base64
$Base64=[Convert]::ToBase64String($Bytes)
# prepare headet for webrequest
$Header = @{Authorization="Basic $Base64"}
Invoke-WebRequest -Uri $Uri -Method GET -Headers $Header
Dodaj komentarz