Parse the error output into a variable

Welcome Forums General PowerShell Q&A Parse the error output into a variable

Viewing 2 reply threads
  • Author
    Posts
    • #216318
      Participant
      Topics: 12
      Replies: 21
      Points: 135
      Rank: Participant

      I have a task of group deletion for specific domain. The script is ready and give the required output, i.e if the group is non existent, the output is relayed into false. However, if for some reason the deletion of groups fails, it still gives as deleted. I want to parse the error output too, so that I can determine what exactly is being deleted and what exactly error out. Request if anyone can guide me out.

      $groups= get-content "C:\temp\list.txt"
      $data = @()
      $result = @()
      $domain="test.net"
      
      
      $result= foreach ($group in $groups) 
      {
             
              try
              {
              Get-ADGroup $group -server $domain
              Get-ADGroup $group -server $domain |remove-adobject -confirm:$false -Verbose
              }
              Catch {out-null}
                    
          
       $result
       If ($result) {
       $data += [PSCustomObject]@{
          Name     = $group
          Status= $group+" "+ "deleted" 
          }
      }
      
       else{ 
        $data += [PSCustomObject]@{
         Name     = $group
          Status  = "Doesnt exist in AD"
          }
      }
      
      }
      
      $data | Export-Csv c:\TEMP\deletionconfirmation.csv -NoTypeInformation
      
      }
    • #216327
      Participant
      Topics: 12
      Replies: 1622
      Points: 2,560
      Helping Hand
      Rank: Community Hero

      The first issue is you have you are not using ErrorAction to Stop the processing and produce a terminating error. Next you are checking $result but not setting result. Try taking a look at this code. Another thing is you are assuming the only reason a failure would occur is a group doesn’t exist, so you should capture a failure and get the exception from your try\catch:

      $results = foreach ( $group in $groups ) {     
          try {
              Get-ADGroup $group -Server $domain -ErrorAction Stop |
              Remove-ADObject -Confirm:$false -Verbose -ErrorAction Stop
      
              [PSCustomObject]@{
                  Name   = $group
                  Status = "Success" 
              }
      
          }
          catch {
              [PSCustomObject]@{
                  Name     = $group
                  Status  = ("Failed: {0}" -f $_) 
              }        
      
          }
      }
      
      $results
      
      • This reply was modified 1 month, 3 weeks ago by Rob Simmers.
    • #216351
      Participant
      Topics: 12
      Replies: 21
      Points: 135
      Rank: Participant

      The first issue is you have you are not using ErrorAction to Stop the processing and produce a terminating error. Next you are checking $result but not setting result. Try taking a look at this code. Another thing is you are assuming the only reason a failure would occur is a group doesn’t exist, so you should capture a failure and get the exception from your try\catch:

      PowerShell
      22 lines

      <textarea class=”ace_text-input” wrap=”off” autocorrect=”off” autocapitalize=”off” spellcheck=”false” style=”opacity: 0; height: 18px; width: 7px; left: 170px; top: 270px;”></textarea>

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      $results = foreach ( $group in $groups ) {
      try {
      Get-ADGroup $group Server $domain ErrorAction Stop |
      Remove-ADObject Confirm:$false Verbose ErrorAction Stop
      [PSCustomObject]@{
      Name = $group
      Status = “Success”
      }
      }
      catch {
      [PSCustomObject]@{
      Name = $group
      Status = (“Failed: {0}” -f $_)
      }
      }
      }
      $results
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      Thanks a million, Rob. Exactly what I want.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.