locked
General question about Powershell try and catch statements RRS feed

  • Question

  • Greetings folks,

    I always have to turn to you guys when I have tough questions.

    With try and catch statements, can you test multiple statements?

    For example, I want to test two statements.

    try
     {
           $machine = Get-ADObject "CN=syssec-$servername,OU=servers,OU=NISNetgroups,OU=Applications,DC=company,DC=com"; $machine = Get-ADObject "CN=syssec-$servername,OU=WindowsServers,OU=Groups,DC=company,DC=com"
            }
           
    catch {$machine = $null; Write-Host "AD Object syssec-$servername not found"}

    When I run this code, it will execute but it won't evaluate the second statement.

    Your thoughts?

    Thanks,

    Mike B.


    Mike B.

    Thursday, September 10, 2015 4:15 PM

Answers

  • If the first statement fails, then it catches the error.

    In that case, of course it won't execute the second statement.

    If you want to test two independent statements, you will need to use two try/catch statements.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Mike Laughlin Thursday, September 10, 2015 4:26 PM
    • Marked as answer by Bellm79 Thursday, September 10, 2015 4:30 PM
    Thursday, September 10, 2015 4:25 PM

All replies

  • EDIT: My example was bad, see Bill's response below.


    EDIT2: You could also loop, if you'd rather have a single try/catch:

    $userList = 'tester1','noexist','tester2'
    
    $userList | ForEach {
    
        $user = $_
    
        try {
    
            Get-ADUser $user | Select -ExpandProperty SamAccountName
        
    
        } catch {
    
            Write-Host "Caught while testing $user" -ForegroundColor Red
    
        }
    
    }
    
    
    Example output:
    tester1
    Caught while testing noexist
    tester2


    Thursday, September 10, 2015 4:23 PM
  • If the first statement fails, then it catches the error.

    In that case, of course it won't execute the second statement.

    If you want to test two independent statements, you will need to use two try/catch statements.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Mike Laughlin Thursday, September 10, 2015 4:26 PM
    • Marked as answer by Bellm79 Thursday, September 10, 2015 4:30 PM
    Thursday, September 10, 2015 4:25 PM
  • Oh ok.  That's what I thought. 

    Thanks folks!


    Mike B.

    Thursday, September 10, 2015 4:37 PM
  • Is this what you need? 

    try {
      $machine = Get-ADObject "CN=syssec-$servername,OU=servers,OU=NISNetgroups,OU=Applications,DC=company,DC=com"
     } 
    catch {
      $machine = $null
     }
    
    if ($machine -eq $null) {
      try {
        $machine = Get-ADObject "CN=syssec-$servername,OU=WindowsServers,OU=Groups,DC=company,DC=com"
       } 
      catch {
        $machine = $null
       }
     }
     
    if ($machine -eq $null) {
      Write-Host "AD Object syssec-$servername not found"
     }


    • Edited by LarryWeiss Thursday, September 10, 2015 9:44 PM
    Thursday, September 10, 2015 9:43 PM