Amine Mo
Amine Mo

Reputation: 91

PowerShell :if else

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

Upvotes: 2

Views: 238

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 59780

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
    }
}

Upvotes: 2

Related Questions