这个问题在这里已经有了答案:
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/