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

时间:2015-03-30 18:57:18

标签: powershell ssl web port

这对我来说真的很奇怪..对于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://

我想我需要逃避&#34;:&#34; ..

我离...更近了。

$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

谢谢!

2 个答案:

答案 0 :(得分:4)

我相信PowerShell会将$ip:8080解释为一个变量,其范围为&#34; ip&#34;名字是&#34; 8080&#34;。使用大括号来阐明变量名称:

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

答案 1 :(得分:0)

好的..逃脱与'角色做了!

$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