PowerShell - 为什么某些请求类型不起作用?

PowerShell - why doesn't certain request types work?

这对我来说真的很奇怪.. http 和 https 的看起来很好.. 端口 8080 和 8443 的不..

$ip="1.2.3.4"
$urls = "http://$ip","http://"$ip":8080","https://$ip","https://"$ip":8443"
foreach ($u in $urls)
{ 
"$u"
}

结果显示:

PS C:\Documents and Settings\user> D:\WORK\ps\test.ps1
Unexpected token 'ip' in expression or statement.
At D:\WORK\ps\test.ps1:2 char:37
+ $rsaurls = "http://$ip","http://"$ip <<<< ":8080","https://$ip","https://"$ip":8443"
    + CategoryInfo          : ParserError: (ip:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

如果我们删除引号..

$ip="1.2.3.4"
$urls = "http://$ip","http://$ip:8080","https://$ip","https://$ip:8443"
foreach ($u in $urls)
{ 
"$u"
}

And the results show:
PS C:\Documents and Settings\user> D:\WORK\ps\test.ps1
http://1.2.3.4
http://
https://1.2.3.4
https://

我想我需要转义“:”..

我更近..

$ip="1.2.3.4"
$urls = "http://$ip","http://$ip^:8080","https://$ip","https://$ip^:8443"
foreach ($u in $urls)
{ 
"$u"
}

结果:

PS C:\Documents and Settings\user> D:\WORK\ps\test.ps1
http://1.2.3.4
http://1.2.3.4^:8080
https://1.2.3.4
https://1.2.3.4^:8443

谢谢!

OK..用`字符转义成功了!

$ip="1.2.3.4"
$urls = "http://$ip","http://$ip`:8080","https://$ip","https://$ip`:8443"
foreach ($u in $urls)
{ 
"$u"
}

结果:

PS C:\Documents and Settings\user> D:\WORK\ps\test.ps1
http://1.2.3.4
http://1.2.3.4:8080
https://1.2.3.4
https://1.2.3.4:8443

我相信 PowerShell 将 $ip:8080 解释为范围为 "ip" 且名称为“8080”的变量。使用大括号明确变量名:

"https://${ip}:8080"