2

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

1
  • 1
    For batch files, look near the bottom of this answer for code with lines containing ) && ( and ) || ( in them: stackoverflow.com/a/71358755/4190564 Commented Apr 21, 2022 at 23:55

1 Answer 1

2

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.