Array with null (?) elements

hello,
i try to search for some users in AD - these users should be found only in some OU's so i make 3 different searches (i couldn't find other way to do this).

the problem is that for every search that didn't return a user "something" is returned and my array of ad users variable gets one more element (that is messing the later cod)

if no user is found in any of these OU's, my $aduser array will still have 3 elements. element type is unknown as gettype() return an error (You cannot call a method on a null-valued expression).

$adusers  = @()
$adusers += Get-ADUser -Filter {msRTCSIP-UserEnabled -notlike "*" } -SearchBase 'OU=Consultants,DC=local,,DC=intra' -SearchScope onelevel
$adusers += Get-ADUser -Filter {msRTCSIP-UserEnabled -notlike "*" } -SearchBase 'OU=OFFICE USERS,DC=local,,DC=intra' -SearchScope onelevel
$adusers += Get-ADUser -Filter {msRTCSIP-UserEnabled -notlike "*" } -SearchBase 'OU=Consultants-Extern,DC=local,DC=intra' -SearchScope onelevel

can i do something about this and get rid of those null (?) element ?

thanks,
Marius

February 17th, 2015 7:51pm

Hi Marius,

try this:

$adusers = @()
Get-ADUser -Filter { msRTCSIP-UserEnabled -notlike "*" } -SearchBase 'OU=Consultants,DC=local,,DC=intra' -SearchScope onelevel | %{ $adusers += $_ }
Get-ADUser -Filter { msRTCSIP-UserEnabled -notlike "*" } -SearchBase 'OU=OFFICE USERS,DC=local,,DC=intra' -SearchScope onelevel | %{ $adusers += $_ }
Get-ADUser -Filter { msRTCSIP-UserEnabled -notlike "*" } -SearchBase 'OU=Consultants-Extern,DC=local,DC=intra' -SearchScope onelevel | %{ $adusers += $_ }

Cheers,
Fred

Ps.: You may want to look into those Searchbases :)

Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 7:54pm

thanks for the help.

i manage to find something else that seems to work:

[array]$ADUsers  = @()
$OrgUnits = @(
        'OU=Consultants,DC=local,DC=intra'
        'OU=OFFICE USERS,DC=local,DC=intra'
        'OU=Consultants-Extern,DC=local,DC=intra'
)

foreach($OU in $OrgUnits){
    $TempADUsers = Get-ADUser -Filter {msRTCSIP-UserEnabled -notlike "*" } -SearchBase $OU -SearchScope onelevel
    if($TempADUsers){ $ADUsers += $TempADUsers }
}

  • Marked as answer by octavmarius Friday, February 20, 2015 10:57 AM
February 18th, 2015 12:50am

Hi Marius,

Yes, that could work. If you use an old PowerShell version (v2) you may hit a snag though. Let's say the Consultants and Office Users work as intended. The Consultants-Extern however do not have an entry. This would cause Get-ADUser not to null $TempADUsers - thus it would again add the "OFFICE USERS" to the list a second time.

If you want to work with a list of OUs, rather try this:

$adusers = @()

$OrgUnits = @(
'OU=Consultants,DC=local,DC=intra',
'OU=OFFICE USERS,DC=local,DC=intra',
'OU=Consultants-Extern,DC=local,DC=intra'
)

foreach ($OU in $OrgUnits) { Get-ADUser -Filter { msRTCSIP-UserEnabled -notlike "*" } -SearchBase $OU -SearchScope onelevel | %{ $adusers += $_ } }

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 12:07pm

you are saying that the if will not protect me agaiinst a null ?

 if($TempADUsers){ $ADUsers += $TempADUsers }

February 20th, 2015 12:15pm

Oh, that if clause will protect you against null, no worries there.

The risk I mentioned (which only affects older systems, I do not know what you are running this on) is that if you find no users for one search query, it may add the users from the previous query again, because then $TempADUser would not be null but still contain the old values (This behavior for variables is fixed by now - I think for all Powershell versions 3+).

Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 12:24pm

ok - now i understand - thanks a lot for the explanations. (i'm using powershell 3)

Marius

February 20th, 2015 1:57pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics