powershell - 如何将默认凭据传递给 PowerShell Web 客户端对象? PS v2 的 Invoke-WebRequest 的替代方案?

标签 powershell powershell-2.0 powershell-3.0 webclient-download

我正在运行以下代码

$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/

相关文章:

powershell - 在 PowerShell 中重命名文件夹并创建新文件夹

powershell - 提取/查看 ScriptMethod 的值

xml - 如何使用 Powershell 将 XML 正确导出到文件

powershell - 在 powershell 中使用 new-guid 重命名每个文件

windows - 获取目录总大小的 PowerShell 脚本

powershell - 适用于 Azure Arm 模板的 Bootstrap DSC 扩展

powershell - 如何使用powershell进行多线程复制?

linux - windows 上的远程 powershell 脚本不通过 linux 中的 python 脚本运行

powershell - 将setCaseSensitiveInfo递归应用于所有文件夹和子文件夹

powershell - 使用 powershell 对象执行脚本