Parse the error output into a variable

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

}

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

[quote quote=216327][/quote]

[quote quote=216327]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
[/quote]

Thanks a million, Rob. Exactly what I want.