none
Can you guys "Try and catch" with me? RRS feed

  • Question

  • I am trying to sort out how can I stop a command within foreach loop when error is detected for a cmdlet.  In this case cmdlet "Get-ADComputer". I only want server that able to run cmdlet "get-adcomputer" to return output

    $servername = Get-Content "C:\Users\John\desktop\computers.txt"
    foreach ($Server in $ServerName)

    {

    Write-Host "checking get-adcomputer for $Server"


            try {$output = Get-ADComputer $Server -Properties MemberOf | Select-Object -ExpandProperty MemberOf -ErrorAction Stop}

            catch{ "something wrong"} #command stop here for failed server but continue for the rest if possible

    ###############################################  

    #I don't want exception server to be passed here

           $server

    }




    • Edited by herngyih Friday, September 20, 2019 7:17 AM
    Friday, September 20, 2019 7:14 AM

Answers

  • you already have an -erroraction stop but not at the right place

    try {$output = Get-ADComputer $Server -Properties MemberOf -ErrorAction Stop | Select-Object -ExpandProperty MemberOf }



    Please remember to mark the replies as answers if they helped.

    • Marked as answer by herngyih Sunday, September 22, 2019 4:10 AM
    Friday, September 20, 2019 8:18 AM

All replies

  • Use "-ErrorAction Stop"

    Read the help for try catch to learn how to use it.

    help about_try_catch


    \_(ツ)_/

    Friday, September 20, 2019 8:05 AM
  • you already have an -erroraction stop but not at the right place

    try {$output = Get-ADComputer $Server -Properties MemberOf -ErrorAction Stop | Select-Object -ExpandProperty MemberOf }



    Please remember to mark the replies as answers if they helped.

    • Marked as answer by herngyih Sunday, September 22, 2019 4:10 AM
    Friday, September 20, 2019 8:18 AM
  • Moved that , but still not working
    Saturday, September 21, 2019 3:34 AM
  • I am trying to sort out how can I stop a command within foreach loop when error is detected for a cmdlet.  In this case cmdlet "Get-ADComputer". I only want server that able to run cmdlet "get-adcomputer" to return output

    $servername = Get-Content "C:\Users\John\desktop\computers.txt"
    foreach ($Server in $ServerName)

    {

    Write-Host "checking get-adcomputer for $Server"


            try {$output = Get-ADComputer $Server -Properties MemberOf | Select-Object -ExpandProperty MemberOf -ErrorAction Stop}

            catch{ "something wrong"} #command stop here for failed server but continue for the rest if possible

    ###############################################  

    #I don't want exception server to be passed here

           $server

    }


    I'just put the rest of the code under the try nest and that solve the problem


    Sunday, September 22, 2019 4:11 AM
  • help about_foreach

    Read the whole thing carefully to learn how to use a "foreach" loop.


    \_(ツ)_/

    Sunday, September 22, 2019 5:04 AM