0

Hi I'm trying to create a script that will check if the list of AD Group I provided exist in AD but the script only returns the last object it found. Please disregard the previous sample code and refer to this one. The script seems to run but it is not returning all the ADGroup in the list even if they all exist as confirmed manually. It only returns the last object.

       function CheckAD

{


$AD= "AA","BB"

foreach ($group in $AD)
{

$R_AD= @()

$R_AD=Get-ADGroup -filter {GroupCategory -eq "Security" -and Name -eq $group} -SearchBase "OU=Application,OU=SOO,OU=Groups,OU=UN,DC=B3,DC=in"|select -ExpandProperty     name -Unique

if ($R_AD.count -gt 0)
{
$r = New-Object -TypeName psobject
$r|Add-Member -MemberType NoteProperty -Name ADGroup -Value $R_AD
}

} 

return($r)

}

$Test=CheckAD
2
  • Thanks for replying. Sorry for the confusion. I've posted a new code which is closer to the actual code that is used. Commented May 10, 2018 at 15:16
  • I would highly recommend using some indentation in your code to improve readability. Commented May 10, 2018 at 15:32

1 Answer 1

2
## Cleaned-up the formatting so I could read this
function CheckAD {
    ## Changed to a hash table; consider passing 
    ## these in as parameters
    $AD = @{"AA" = $false; "BB" = $false}

    foreach ($group in $AD.keys) {
        ## You don't need this. Removing.
        #$R_AD = @()

        ## In your post, you asked to know if the group existed.
        if ( $(Get-ADGroup -filter {GroupCategory -eq "Security" -and Name -eq $group} -SearchBase "OU=Application,OU=SOO,OU=Groups,OU=UN,DC=B3,DC=in") ) {
            $AD[$group] = $true 
        }

        ## You don't need this either
        #if ($R_AD.count -gt 0) {
        #    $r = New-Object -TypeName psobject
        #    $r|Add-Member -MemberType NoteProperty -Name ADGroup -Value $R_AD
        #}

    } 

    #return($r)
    return($AD)

}

$Test = CheckAD
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.