qshinoの日記

Powershell関係と徒然なこと

PS:web認証

Powershell Web認証

 

Powershell的な方法

$req = [system.Net.WebRequest]::Create($url)
# method 1 $req.UseDefaultCredentials = $true
# method 2 $req.Credentials = new NetworkCredential($username, $pwd, $domain);
try
{
$res = $req.GetResponse()
}
catch [System.Net.WebException]
{
$res = $_.Exception.Response
}

$int = [int]$res.StatusCode
$status = $res.StatusCode
return "$int $status"

 

但し、Credential部分は、下記に変更した方が良いかも。

I may be wrong, but I had to replace new NetworkCredential with $passwd = ConvertTo-SecureString "nowisthetime4U" -AsPlainText -Force; followed by $request.Credentials = New-Object System.Management.Automation.PSCredential ("pwatson_at_phs_org", $passwd);

 

 

下記参照

http://stackoverflow.com/questions/508565/how-to-make-an-authenticated-web-request-in-powershell