powershell交换:if then语句语法?

发布于 2024-07-13 15:39:34 字数 232 浏览 4 评论 0原文

在我的脚本中,我试图测试真假。 这个语法不正确吗?

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨后咖啡店 2024-07-20 15:39:34

似乎直接检查布尔值而不是使用 ToString() 会更好:

$pdaout = ""

if ($pda.ActiveSyncEnabled -eq $True) { $pdaout = "TRUE" }
else { $pdaout = "-" }

write-host $pdaout

这是来自 Windows Powershell 团队回复:布尔值和运算符

Seems like it would be better to just check the boolean value directly instead of using ToString():

$pdaout = ""

if ($pda.ActiveSyncEnabled -eq $True) { $pdaout = "TRUE" }
else { $pdaout = "-" }

write-host $pdaout

Here is a blog post from the Windows Powershell team re: Boolean Values and Operators

夢归不見 2024-07-20 15:39:34

应该是这样,除了 tostring() 函数(使用 toString() 确保不存在区分大小写的问题)

另外,您可能想要使用 elseif 以避免在第一次测试成功时进行第二次测试:

if ($pda.ActiveSyncEnabled.toString() -like "True") {$pdaout = "TRUE"}
elseif ($pda.ActiveSyncEnabled.toString() -like "False") {$pdaout = "-"}

It should be, except may be for the tostring() function (use toString() 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:

if ($pda.ActiveSyncEnabled.toString() -like "True") {$pdaout = "TRUE"}
elseif ($pda.ActiveSyncEnabled.toString() -like "False") {$pdaout = "-"}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文