powershell交换:if then语句语法?
在我的脚本中,我试图测试真假。 这个语法不正确吗?
$pdaout = ""
if ($pda.ActiveSyncEnabled.tostring() -like "True") {$pdaout = "TRUE"}
if ($pda.ActiveSyncEnabled.tostring() -like "False") {$pdaout = "-"}
write-host $pdaout
In my script i'm trying to test for true and false. Is this syntax incorrect?
$pdaout = ""
if ($pda.ActiveSyncEnabled.tostring() -like "True") {$pdaout = "TRUE"}
if ($pda.ActiveSyncEnabled.tostring() -like "False") {$pdaout = "-"}
write-host $pdaout
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎直接检查布尔值而不是使用 ToString() 会更好:
这是来自 Windows Powershell 团队回复:布尔值和运算符
Seems like it would be better to just check the boolean value directly instead of using ToString():
Here is a blog post from the Windows Powershell team re: Boolean Values and Operators
应该是这样,除了
tostring()
函数(使用toString()
确保不存在区分大小写的问题)另外,您可能想要使用
elseif
以避免在第一次测试成功时进行第二次测试:It should be, except may be for the
tostring()
function (usetoString()
to make sure there is no issue with the case sensitivity)Plus, you may want to use
elseif
to avoid doing a second test if the first one was successful: