powershell - 使用动态主机调用 WebRequest 失败

标签 powershell httpwebrequest

这个问题在这里已经有了答案:





PowerShell String Formatting: Why is the colon character causing my variable's value to be blank?

(2 个回答)


4年前关闭。




我想使用由参数定义的主机执行 Web 请求:

$serverHost = "myhost"
Invoke-WebRequest http://$serverHost:1234/service -Method Get

Powershell 提示这个并出现以下错误:

Invoke-WebRequest : Cannot bind parameter 'Uri'. Cannot convert value "http:///service" to type "System.Uri". Error: "Invalid URI: The hostname could not be parsed."



我必须做什么才能让 PowerShell 正确解释我的 URL?

最佳答案

PowerShell 根本不会解析 URL 中的变量。您正在尝试通过 URI http://$serverHost:1234/service 查询服务这是行不通的。你可以做

$serverHost = "myHost"
$service = "http://$serverHost`:1234/service"
Invoke-WebRequest $service -Method Get

(请注意,为了转义冒号,需要反引号 `)。

关于powershell - 使用动态主机调用 WebRequest 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33013061/

相关文章:

powershell - 从 Samaccountname 获取电子邮件地址

c# - 使用 c# 在 HttpWebRequest 中添加预告片

web-services - 我可以使用 PowerShell 编写 WebService 吗?

C#:将 CookieContainer 写入磁盘并加载回来使用

httpwebrequest - C# 在 aspx 页面上遇到屏幕抓取问题

c# - HttpWebRequest POST 向表单变量添加 NULL 值

android - 如何从 ESHA Research API 调用多个营养信息? (apid.esha.com)

windows - 如何在 Powershell 中执行 head、tail、more、less、sed 的操作?

powershell - 使用 Kudu Rest API 将文件上传到 Azure Web App - 409 错误

azure - 单点登录时如何使用 powershell 将 Azure 中的 API 连接授权为不同用户