在C#中,我可能会这样做:
System.Net.WebClient w = new System.Net.WebClient(); w.Credentials = new System.Net.NetworkCredential(username, auth, domain); string webpage = w.DownloadString(url);
是否有Powershell版本,或者我应该打电话给CLR?
PowerShell几乎完全相同.
$webclient = new-object System.Net.WebClient $webclient.Credentials = new-object System.Net.NetworkCredential($username, $password, $domain) $webpage = $webclient.DownloadString($url)
对于需要Powershell返回其他信息的人,比如Http StatusCode,这是一个例子.包括两种最有可能传递凭证的方法.
它是这个SO答案的略微修改版本:
如何在PowerShell中获取数字HTTP状态代码
$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"