by TomKemp at 2013-04-24 04:24:30
I have been struggling with this for some time, so any help would be much appreciated please.
I have a script to test membership of AD groups and act accordingly. A user might be in one group matching the pattern, more than one or none. Therefore, I have defined the variable containing the result as an array – $global:BUFA. Then I test for the value of 'Count' for that array.
If the result is greater than one I use the first value, which I define as $global]. I would have said that from a Maths point of view, the 'first' member of a set with only one member is that single member. However, Powershell does not seem to accept $Variable for results with a count of 1 – so I just use the single value directly for those.
The problems arise when I have a count of more than 1 – i.e. the user is in two or more groups. Then I am trying to use:
[array]$global:BUFA = @()
if($global:BUFA.Count -gt 1)
$global:BUFA1 = $global:BUFA
The result is an error
Count : 2
Cannot index into a null array.
At C:\Users\tkema\MyScripts\UserMoves\UserMoveForm11.ps1:587 char:37
+ $global:BUFA1 = ($globalBUFA).name | Out- ...
+ CategoryInfo : InvalidOperation: (:) , RuntimeException
+ FullyQualifiedErrorId : NullArray
How can it be a null array if the count is greater than zero?
Even more strange is that is I try to use:
this works and prints the name of the first group in the list.
This suggests to me it has correctly identified and printed the value of the first item in the array.
Any thoughts please?
by ArtB0514 at 2013-04-24 06:42:43
Did you read your error message carefully?
[quote]$global:BUFA1 = ($globalBUFA).name | Out- ...[/quote]
You are missing the ":" in "$global].Name". Since there is no $globalBUFA array, you're getting the $null array error.
Just as an asside, why are you messing around in the $global namespace? Are you trying to make a variable visible outside your script? If so, it would be a better practice to work with parameters instead of namespaces.
by TomKemp at 2013-04-24 07:36:36
Sorry – that was a typo in my post. The actual script does have the : in it.
I think I may have resolved this problem now.
$A1 = ($global:BUFA.ToString()).split(" ")
$global:BUFA = ($A1).Substring(9)
This converts the value of the variable to a string, then splits it at the space (i.e. between the two group names),
then takes the first part of that split and removes the prefix from the remaining value, which corresponds to the Domain Name.
That gives me the name of the group, without the Domain prefix.
Early testing suggests that is working for users in one or more groups. Not tested those not in any groups yet.
Edit – Sorry, you are correct. I DID miss out the : in the original script. How annoying.