none
Remove-ADGroup - exits loop RRS feed

  • Question

  • I am trying to write script to delete AD groups according to csv file and write simple success\failure report.

    Not all names from the CSV list actually exists in AD. Script runs perfectly till first "not found" error and there it leaves the loop.

    I tried ForEach-Object and ForEach options. And also implement "IF" inside of the loop, playing with ErrorActionPreference  and  but all the same, at the moment non-existent name is reached, script exits.

    Below is (one of my tries) snippet of my code.

    Try {	
    ForEach($GroupForDel in $CsvData.Name) {
    $GroupForDel = (Get-ADGroup -Identity $GroupForDel).distinguishedName
    If ($GroupForDel -eq $null)
    {
        $GroupForDel + " group cannot be found" | Add-Content  "$ReportFile"
    }
    Else
    {
        Set-ADObject $GroupForDel -ProtectedFromAccidentalDeletion $false
        Remove-ADGroup -Identity $GroupForDel -Confirm:$false
    }
    
    }
    }
    
    Catch {
        $_|Out-File  -Append -NoClobber -encoding UTF8 "$ReportFile"
        }


    • Edited by peTerko1 Thursday, September 28, 2017 10:06 AM
    Thursday, September 28, 2017 10:04 AM

Answers

  • Hi,

    simply put the try/catch inside the loop:

    ForEach ($GroupForDel in $CsvData.Name)
    {
    	try
    	{
    		$GroupForDel = (Get-ADGroup -Identity $GroupForDel -ErrorAction Stop).distinguishedName
    		If ($GroupForDel -eq $null)
    		{
    			$GroupForDel + " group cannot be found" | Add-Content  "$ReportFile"
    		}
    		Else
    		{
    			Set-ADObject $GroupForDel -ProtectedFromAccidentalDeletion $false
    			Remove-ADGroup -Identity $GroupForDel -Confirm:$false -WhatIf
    		}
    	}
    	Catch
    	{
    		$_ | Out-File -Append -NoClobber -encoding UTF8 "$ReportFile"
    	}
    }

    And all should be fine.

    Cheers,
    Fred


    There's no place like 127.0.0.1


    • Marked as answer by peTerko1 Thursday, September 28, 2017 11:38 AM
    • Edited by FWN Thursday, September 28, 2017 11:53 AM
    Thursday, September 28, 2017 10:09 AM

All replies

  • Hi,

    simply put the try/catch inside the loop:

    ForEach ($GroupForDel in $CsvData.Name)
    {
    	try
    	{
    		$GroupForDel = (Get-ADGroup -Identity $GroupForDel -ErrorAction Stop).distinguishedName
    		If ($GroupForDel -eq $null)
    		{
    			$GroupForDel + " group cannot be found" | Add-Content  "$ReportFile"
    		}
    		Else
    		{
    			Set-ADObject $GroupForDel -ProtectedFromAccidentalDeletion $false
    			Remove-ADGroup -Identity $GroupForDel -Confirm:$false -WhatIf
    		}
    	}
    	Catch
    	{
    		$_ | Out-File -Append -NoClobber -encoding UTF8 "$ReportFile"
    	}
    }

    And all should be fine.

    Cheers,
    Fred


    There's no place like 127.0.0.1


    • Marked as answer by peTerko1 Thursday, September 28, 2017 11:38 AM
    • Edited by FWN Thursday, September 28, 2017 11:53 AM
    Thursday, September 28, 2017 10:09 AM
  • Must sue "ErrorAction Stop" for try/catch to work.


    \_(ツ)_/

    Thursday, September 28, 2017 10:12 AM
  • @FWN
    Thank you very much.

    It helped, but the "SIMPLENESS" of the solution spoiled my day a bit ...



    @jrv

    thanks for the reply, but this was one of the first things I tried in every possible variation - per command (ErrorAction), globally (ErrorActionPreference). No effect.

    ::p


    • Edited by peTerko1 Thursday, September 28, 2017 12:07 PM
    Thursday, September 28, 2017 11:46 AM