我正在运行以下代码
$client = new-object System.Net.WebClient
$client.DownloadFile( $UriValue, "C:\Temp\BHRout.json" )
$json = Get-Content "C:\Temp\BHRout.json"
这不起作用,因为它需要将我的提示凭据传递到下载字符串函数中。我用上面的代码来代替这个:
$NagiosResults = Invoke-WebRequest -Uri $Uri -UseDefaultCredentials | ConvertFrom-Json
唯一的问题是我运行脚本的服务器没有 Powershell v3;所以这也行不通。是否有替代
Invoke-WebRequest
的替代方案?对于 Powershell v2?如果没有,是否可以通过 System.Net.WebClient 对象“使用默认凭据”?
最佳答案
只需设置 UseDefaultCredentials
WebClient 的属性为 $true
这将使用当前用户的凭据进行身份验证。
$uri = "http://myserver/service"
$wc = New-Object System.Net.WebClient
$wc.UseDefaultCredentials = $true
$json = $wc.DownloadString($uri)
关于powershell - 如何将默认凭据传递给 PowerShell Web 客户端对象? PS v2 的 Invoke-WebRequest 的替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18882853/