powershell get-aduser output returned to an array

OK
So I am very new to Powershell and I have been looking for this answer for a few days with no luck. So here goes;

What I am trying to get is a list of AD Users and some of the property information; the script I have written is returning the data but not in the format that I need it to be in.

#retreives last authenticated Domain controller from environment variables
$DCname = $env:LOGONSERVER -replace “\\”, “”
$DCname

#retreives domain name
$TLD = $env:userdnsdomain
$TLD

# concatenates the above to create a FQDN for the DC
$Currentdc = $dcname+"."+$tld
$currentdc

$ADSearchbase = "ou=SBSUsers,ou=Users,ou=MyBusiness,dc=multipondamerica,dc=com"

$ADProperties = "Enabled","CanonicalName","Displayname","MemberOf","Givenname","Surname","EmployeeNumber","EmailAddress","Department","StreetAddress","OfficePhone","PostalCode","City","Office","Company","Title","SID"

$ADSelected   = "Enabled","CanonicalName","Displayname","MemberOf","Givenname","Surname","EmployeeNumber","EmailAddress","Department","StreetAddress","OfficePhone","PostalCode","City","Office","Company","Title","SID"

$arrUsers = Get-ADUser -Filter 'enabled -eq $true -and pobox -eq 12345' -server $CurrentDC -SearchBase $ADSearchbase -Properties $ADProperties | select $ADSelected

$arrUsers

OUT PUT:
Enabled        : True
CanonicalName  : Domain.com/MyBusiness/Users/SBSUsers/User1
Displayname    : User1
MemberOf       : {CN=Exchange View-Only Administrators,OU=Microsoft Exchange Security Groups,DC=Domain,DC=com, CN=Exchange Organization
                 Administrators,OU=Microsoft Exchange Security Groups,DC=Domain,DC=com,
                 CN=SQLServer2005MSSQLUser$SERVER$BKUPEXEC,CN=Users,DC=Domain,DC=com, CN=SMSMSE Admins,CN=Users,DC=Domain,DC=com...}
Givenname      : User1
Surname        :
EmployeeNumber :
EmailAddress   : user1@domain.com
Department     :
StreetAddress  :
OfficePhone    :
PostalCode     :
City           :
Office         :
Company        :
Title          :
SID            : S-1-5-21-1314609501-1516500090-731184889-1663

Enabled        : True
CanonicalName  : Domain.com/MyBusiness/Users/SBSUsers/User2
Displayname    : User2
MemberOf       : {CN=Exchange View-Only Administrators,OU=Microsoft Exchange Security Groups,DC=Domain,DC=com, CN=Exchange Organization
                 Administrators,OU=Microsoft Exchange Security Groups,DC=Domain,DC=com,
                 CN=SQLServer2005MSSQLUser$SERVER$BKUPEXEC,CN=Users,DC=Domain,DC=com, CN=SMSMSE Admins,CN=Users,DC=Domain,DC=com...}
Givenname      : User2
Surname        :
EmployeeNumber :
EmailAddress   : user2@domain.com
Department     :
StreetAddress  :
OfficePhone    :
PostalCode     :
City           :
Office         :
Company        :
Title          :
SID            : S-1-5-21-1314609501-1516500090-731184889-1664

So instead of returning an array with 42 elements, it returns 2 elements
The array looks like this:
1
@{Enabled=True; CanonicalName=Domain.com/MyBusiness/Users/SBSUsers/Act Admin; Displayname=Act Admin; MemberOf=Microsoft.ActiveDirectory.Management.ADPropertyV
alueCollection; Givenname=User1; Surname=User1; EmployeeNumber=; EmailAddress=User1@Domain.com; Department=; StreetAddress=; OfficePhone=; PostalCode=; City=; Office=; Company=; Title=;
SID=S-1-5-21-1314609501-1516500090-731184889-1663}
2
@{Enabled=True; CanonicalName=Domain.com/MyBusiness/Users/SBSUsers/User2; Displayname=User2; MemberOf=Microsoft.ActiveDirectory.Management.ADProperty
ValueCollection; Givenname=User2; Surname=User2; EmployeeNumber=; EmailAddress=User2@Domain.com; Department=; StreetAddress=; OfficePhone=; PostalCode=; City=; Office=; Company=; Title=;
 SID=S-1-5-21-1314609501-1516500090-731184889-1664}

So I want to have an array with each piece of data in it and not a log string of data for each user.

Any help would be great.
Thanks
RonPatton61Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
I don't see a problem with the code.  If it's only returning 2 users, then it's probably due to the filter that you have for the Get-ADUser command.  For troubleshooting, just try "-filter *".
Your first example output is correct, I don't know where/how you are getting the second output.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
$arrUsers[0] contains the first user's data as an object with properties. There is no "long string of data", in fact it is a hash array with property name / value pairs.
What do you want the result for? Depending on that different access methods make sense.
0
RonPatton61Author Commented:
Yes, all of the data is there, but I am looking for something different.
I will try and ask the question a different way.
I am looking to address each segment of the data like a multidimensional array;
$arrUsers[0] is True
$arrUsers[1] is Domain.com/MyBusiness/Users/SBSUsers/User1
$arrUsers[3] is User1
$arrUsers[4] is {CN=Exchange View-Only Administrators,OU=Microsoft Exchange Security ....
.....
$arrUsers[11] is S-1-5-21-1314609501-1516500090-731184889-1663
$arrUsers[12] is True
$arrUsers[13] is Domain.com/MyBusiness/Users/SBSUsers/User2
$arrUsers[14] is User2
....
$arrUsers[22] is S-1-5-21-1314609501-1516500090-731184889-1664

I want to address each selected object as an element.

Thanks for your help
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Doable, but you don't know what each value means. And what about the second user?
0
footechCommented:
I can't see any advantage doing it that way.  It's possible, but you lose most of the structure of the array as all elements will be different, and so any further processing is made much more difficult.

Can you describe what your goal is?  What you're describing doesn't sound like "the PowerShell way" to do things, and I think there's probably a better alternative.
0
RonPatton61Author Commented:
should be able to know by position in the array, if I am pulling 11 points of data then position 0-10 will be the first users information then the position 11-21 will be the second users information and 22-32 will be the third users information and so forth.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
$arrUsers = Get-ADUser -Filter 'enabled -eq $true -and pobox -eq 12345' -server $CurrentDC -SearchBase $ADSearchbase -Properties $ADProperties | % {
  foreach ($prop in $ADSelected) { $_.$p }
}

Open in new window

0
RonPatton61Author Commented:
The code above doesn't really return anything except the last element of SID. But I also don't really understand the % and the { $_.$p } doesn't make a lot of since either, but like I said this is new to me.

What I am doing is moving users from a SBS2008 domain to a new 2012 domain, exchange and data server so I need to gather all of the user information and then be able to finish the script where it will add the records to other servers as needed. Need to automate as much as I can, there are a lot of users. If I can gather all of the pertinent data to some type of an array where I know that position 0, 11, 22, 33, 44, etc, are the users names then I can gather that information and build the commands that I need.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
There was a typo. Here the complete code (note I have removed $ADProperties, as those were the same as $ADSelected):
#retreives last authenticated Domain controller from environment variables
$DCname = $env:LOGONSERVER -replace “\\”, “”
$DCname

#retreives domain name
$TLD = $env:userdnsdomain
$TLD

# concatenates the above to create a FQDN for the DC
$Currentdc = $dcname+"."+$tld
$currentdc

$ADSearchbase = "ou=SBSUsers,ou=Users,ou=MyBusiness,dc=multipondamerica,dc=com"

$ADSelected   = "Enabled","CanonicalName","Displayname","MemberOf","Givenname","Surname","EmployeeNumber","EmailAddress","Department","StreetAddress","OfficePhone","PostalCode","City","Office","Company","Title","SID"

$arrUsers = Get-ADUser -Filter 'enabled -eq $true -and pobox -eq 12345' -server $CurrentDC -SearchBase $ADSearchbase -Properties $ADSelected | % {
  foreach ($prop in $ADSelected) { $_.$prop }
}
$arrUsers

Open in new window

The trick is to go thru the list of properties you want to have, and send their corresponding values to the pipe. $arrUsers is hence an array consisting of an uncstructured "stream" of values.
% { ... }   is foreach-object
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Seth SimmonsSr. Systems AdministratorCommented:
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
http:#a40526920 answers the question as revised in http:#a40526745
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Recommendation: Accept http:#a40526920
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.