0

I have a script that has 2 parts that I have an issue with. Part #1 is a series of variables, that have users that meet some criteria using Get-ADUser. Part #2, I wanted to add the system arrays from part one into a list, so that in part #3 add the data is added to one big array along with a $tag so that in part #4 (not shown) everything can be processed at once.

The important thing is that all variables in part #1 end up with a tag in a single array, and that it's easy to add to when more variables are added to part #1. The thinking behind this is that people can add to part #1, refer to it in part #2, and don't have to repeat the code in parts #3 and #4.

THE PROBLEM:
On line 2 of Part #3 I can't split out the $collection array into the array and the tag, to add to the $data array.

#1
...
$O365_TransitionGroupMembers = $O365_TransitionGroupMembers | Select-Object -Unique

#2
$collection = `
(($VIPUser),"O365_VIP"),
(($O365_TransitionGroupMembers),"O365_Transition")
...

#3
$collection | ForEach-Object {
    $expression,$tag = $_.split(",")

    $data = ForEach ($User in $expression) {
        #Create new object for passing back the required information
        $Result = New-Object PSObject
        $Result | Add-Member -MemberType NoteProperty -name DistinguishedName -value NotSet
...
        $Result.Tag = $tag
...

EDIT:

Both $collection and the $vars in part 1 are system arrays, the part 1 $vars look like get-aduser. The $collection looks like:

14:11 C:\Users\d>$collection | fl

Length : 106 LongLength : 106 Rank : 1 SyncRoot : {CN=XXX\, XXX,OU=XXX,OU=XXX,OU=Domain Objects,DC=XXX,DC=XXX,DC=XXX, CN=XXX\, XXX,OU=XXX,OU=XXX,OU=Domain Objects,DC=XXX,DC=XXX,DC=XXX, CN=XXX,OU=Resources,OU=XXX,OU=Domain Objects,DC=XXX,DC=XXX,DC=XXX, CN=XXX\, XXX,OU=XXX,OU=XXX,OU=Domain Objects,DC=XXX,DC=XXX,DC=XXX...} IsReadOnly : False IsFixedSize : True IsSynchronized : False Count : 106

O365_VIP
Length : 600 LongLength : 600 Rank : 1 SyncRoot : {CN=XXX\, XXX C,OU=XXX,OU=Users,OU=ME-DXB-IIR,OU=Domain Objects,DC=XXX,DC=XXX,XXX=net, CN=XXX\, XXXv C,OU=XXX,OU=Users,OU=ME-DXB-IIR,OU=Domain Objects,DC=XXX,DC=XXX,DC=XXX, CN=XXX\, XXX C,OU=XXX,OU=Users,OU=ME-DXB-IIR,OU=Domain Objects,DC=XXX,DC=XXX,DC=XXX, CN=XXX\, XXXOU=Contractors,OU=Users,OU=ME-DXB-IIR,OU=Domain Objects,DC=emea,DC=CorpLAN,DC=net...} IsReadOnly : False IsFixedSize : True IsSynchronized : False Count : 600

O365_Transition

14:16 C:\Users\shielsd>$collection[0]
...
SamAccountName : XXX
SID : S-1-5-XXX-XXX-XXX-XXX-XXX
Surname : XXX
UserPrincipalName : XXX.XXX@XXX.XXX
DistinguishedName : CN=XXX\, XXX,OU=XXX,OU=XXXXXX,OU=Domain
Objects,DC=XXX,DC=XXX,DC=XXX
Enabled : True
ExtensionAttribute15 : XXX;XXX;O365_VIP
GivenName : XXX
Name : XXX, XXX
ObjectClass : user
ObjectGUID : XXX-XXX-XXX-XXX-XXX
SamAccountName : XXX
SID : S-1-5-21-XXX-XXX-XXX-XXX
Surname : XXX
UserPrincipalName : XXX.XXX@XXX.com

O365_VIP

The error I receive is:

Method invocation failed because [Microsoft.ActiveDirectory.Management.ADUser] does not contain a method named 'split'. At line:2 char:5 + $expression,$tag = $_.split(",") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (split:String) [], RuntimeException

idarryl
  • 749
  • 1
  • 11
  • 21
  • 2
    Can you show a sample of what `$collection` contains? You are splitting `$collection` on a comma, but the way you are assigning it, makes me think that the values are `line-separated` and not `comma-separated`. – Vivek Kumar Singh Jul 12 '18 at 12:30
  • 2
    we need to see what `$vipuser` is and the `$O365_TransitionGroupMembers` as well. What does collection look like when put to console would give better insight. – Matt Jul 12 '18 at 12:33
  • 1
    @Matt - Or should I rather say, the values of `$VIPUser` and `$O365_TransitionGroupMembers` will be of interest. But whatever the case may be, I think comma-separated split ain't gona work here. – Vivek Kumar Singh Jul 12 '18 at 12:33
  • Agreed (why i deleted the comment). I think the first is a scalar where the other is an array. This might be a jagged 2d array at first glance. – Matt Jul 12 '18 at 12:34

1 Answers1

0

You issues stem from how you are treating $collection which is likely exacerbated by whatever $O365_TransitionGroupMembers contains. Since it sounds like another collection.

Currently $collection is an array of arrays. Also known as a jagged array. There is nothing to split since that comma is being used as an array element delimiter.

Have a look at this example using your code and sample data that should prove a point.

$VIPUser = "Matt"
$O365_TransitionGroupMembers = "Tim","John"

$collection = (($VIPUser),"O365_VIP"),
(($O365_TransitionGroupMembers),"O365_Transition")

$collection | ForEach-Object{
    write-host ("This element contains {0} items and the second on is: {1}" -f $_.count, $_[1])
}

The actually data in your array elements does not contain commas (from what we see) so your split would only be populating $expression. Would also be flattening your array into a space delimited string.

Matt
  • 45,022
  • 8
  • 78
  • 119
  • > This element contains 2 items and the second on is: O365_VIP > This element contains 2 items and the second on is: O365_Transition – idarryl Jul 12 '18 at 13:31
  • Thanks for your answer, I'm not good enough to understand where to go from here, so may be I could ask this: how can build my $collection array so that I can call 2 'things' on each line/index of the for loop? the Split is just a means to an end, I just need to be able to say, ' for each line in the $collections array, do X with the array and do Y with the text – idarryl Jul 12 '18 at 14:05
  • I suggest reading the linked post that can help you deal with jagged arrays. With that you will know how to access your data structure in your loop. My answer also shows an example you should be able to extrapolate from. I can't tell you exactly what to do since I do not have your data. In my code the text is `$_[1]` and the first part can be accessed via `$_[0]` however you have multiple data types in there so there is no single solution. perhaps `ForEach ($User in $_[0])` or something similar. It depends what your end game is. – Matt Jul 12 '18 at 14:37