Try - Catch differences in foreach loop

Welcome Forums General PowerShell Q&A Try - Catch differences in foreach loop

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 month, 3 weeks ago.

  • Author
    Posts
  • #114489

    Participant
    Points: 0
    Rank: Member

    Dear guys,

    I've notices strage behaviour of the "try-catch" function.

    For testing purposes I've stored to variable 2 random windows services with 2 properties – "name" and "displayname". Then I've changed the property "name" on the second object in array to value "something".

    $services= get-service | select -first 2 name,displayname

    $services[1].name="Something"

     

    Now, I want to achieve thru loop that when error appears, while trying to get service with the name "something" (in the "try" script block), it tries to get the service by property "displayname" (in catch scriptblock).

     

    $services |    foreach  {

    try {get-service $_.name -ErrorAction Stop} catch

    {get-service $_.displayname}

    }

    The code above, failes to  get the service by the value stored in the "$_.displayname" because it is empty.. But in the code below it normally executes $s.displayname.

    foreach ($s in $services) {

    try {get-service $s.name -ErrorAction stop} catch

    {get-service $s.displayname}

    }

    Can someone please explain why this is happening.

    Thanks,

    Dorian

     

  • #114490

    Participant
    Points: 819
    Helping Hand
    Rank: Major Contributor

    Well, $_ is the notation for Object passed through between cmdlets in a pipeline. But when you use Try{} Catch{}, $_ in catch is the Error details where ever you place it.

    Try{Get-Process -Name NoProcess -ErrorAction Stop}
    Catch{$_.Exception}
    #or
    Try{Get-Process -Name NoProcess -ErrorAction Stop}
    Catch{$_ | Get-Member}
    

    When you use foreach statement(not ForEach-Object cmdlet), each object is not identified using $_, but its having its own temp variable which we give, here it is $S.

  • #114495

    Participant
    Points: 0
    Rank: Member

    Thanks for the explanation 🙂

You must be logged in to reply to this topic.