PowerShell:如果其他

发布于 01-22 22:44 字数 553 浏览 2 评论 0原文

批处理脚本和powershell 如果此脚本在此脚本上使用其他问题,是否可以使用一种方法来检查是否可以执行其他操作:

PS C:\Users\Amine>
>> Get-PnpDevice -FriendlyName "*LuminonCore IDDCX*" | ft -wrap -autosize Status

Status
------
OK

//after disabling the driver


PS C:\Users\Amine>
>> Get-PnpDevice -FriendlyName "*LuminonCore IDDCX*" | ft -wrap -autosize Status

Status
------
Error

< img src =“ https://i.sstatic.net/wb01l.png” alt =“ powerShell”>

m new to batch scripts and powershell
is there a way to use if else on this script to check if ok do something if error do something else:

PS C:\Users\Amine>
>> Get-PnpDevice -FriendlyName "*LuminonCore IDDCX*" | ft -wrap -autosize Status

Status
------
OK

//after disabling the driver


PS C:\Users\Amine>
>> Get-PnpDevice -FriendlyName "*LuminonCore IDDCX*" | ft -wrap -autosize Status

Status
------
Error

powershell

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

末骤雨初歇 2025-01-29 22:44:21

您可以使用分组操作员() 要包装表达式,这样做允许您引用状态>状态 value >返回对象的属性,然后您可以使用 -eq 进行等式评估:

if((Get-PnpDevice -FriendlyName "*LuminonCore IDDCX*").Status -eq 'ok') {
    # ok here, do something
}
else {
    # error here, do something
}

上面的示例假设只有一个对象返回get-pnpdevice,但是,由于您使用的是通配符(*),因此将可能性打开到一个以上的结果,在这种情况下,您需要

foreach($device in Get-PnpDevice -FriendlyName "*LuminonCore IDDCX*") {
    if($device.Status -eq 'ok') {
        # ok here
    }
    else {
        # fail here
    }
}

You can use the Grouping Operator ( ) to wrap the expression, doing so allows you to reference the Value of the Status property of the returned object, then you can use -eq for equality evaluation:

if((Get-PnpDevice -FriendlyName "*LuminonCore IDDCX*").Status -eq 'ok') {
    # ok here, do something
}
else {
    # error here, do something
}

The example above assumes there would be only one object returned by Get-PnpDevice, however since you're using wildcards (*), opens up the possibility to more than one result, in which case you would need to loop over each returned object:

foreach($device in Get-PnpDevice -FriendlyName "*LuminonCore IDDCX*") {
    if($device.Status -eq 'ok') {
        # ok here
    }
    else {
        # fail here
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文