Link to home
Start Free TrialLog in
Avatar of AlHal2
AlHal2Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Extracting values from Powershell array on ActiveDirectory Query

I have this Powershell script

$U=Get-ADUser -Filter * -SearchBase "OU=comp Users and Computers,DC=comp,DC=ads" -Properties Name,userprincipalname,distinguishedname | select Name,userprincipalname,distinguishedname

The results come back in a 1D array with lots of values separated by a semi colon eg
@{Name=test; userprincipalname=S71@comp.org.uk; distinguishedname=test,OU=Users,OU=FE,OU=Shops,OU=comp Users and Compute
rs,DC=comp,DC=ads}

How do I retrieve the values?
How do I deal with the fact that OU is repeated?
Avatar of oBdA
oBdA

The results come back in a 1D array with lots of values separated by a semi colon eg
That's only a display issue. This will happen when you send the output directly to Write-Host or a file.
This will give you readable output to the console - Format-* cmdlets should always be the last element in the pipeline:
$U = Get-ADUser -Filter * -SearchBase "OU=comp Users and Computers,DC=comp,DC=ads" -Properties Name,userprincipalname,distinguishedname | select Name,userprincipalname,distinguishedname
$U | Format-List

Open in new window

This will export the list to a csv file that can be opened in an editor or Excel or whatever:
$U = Get-ADUser -Filter * -SearchBase "OU=comp Users and Computers,DC=comp,DC=ads" -Properties Name,userprincipalname,distinguishedname | select Name,userprincipalname,distinguishedname
$U | Export-Csv -NoTypeInformation -Path C:\temp\ADUsers.csv

Open in new window

How do I retrieve the values?
You can address them directly, as in all arrays:
$U = Get-ADUser -Filter * -SearchBase "OU=comp Users and Computers,DC=comp,DC=ads" -Properties Name,userprincipalname,distinguishedname | select Name,userprincipalname,distinguishedname
$U[0]

Open in new window

Or use it in a ForEach:
ForEach ($row in $U) {
	Write-Host "Processing '$($row.userprincipalname)'"
}

Open in new window

Or use it in a Where-Filter:
$U | Where-Object {$_.UserPrincipalName -eq 'john.doe@domain.com'}

Open in new window

How do I deal with the fact that OU is repeated?
Please clarify what you mean with that.
If you're talking about the "CN=SomeUser,OU=...,OU=...," - that's just the format of a Distinguished Name. You have nested OUs, so you have multiple OU=<Name> elements.
Avatar of AlHal2

ASKER

Thanks for this.  I have a field in my Sharepoint list called directorate.  Using the above example, I want to set it to Shops.  Shops is one of the OU values.
values.  How do I do this?
Please clarify "one of the OU values".
Is "Shops" an OU or an AD user object?
Avatar of AlHal2

ASKER

Here is the full string with shops in bold.

@{Name=test; userprincipalname=S71@comp.org.uk; distinguishedname=test,OU=Users,OU=FE,OU=Shops,OU=comp Users and Compute
rs,DC=comp,DC=ads}

How do I find out if it is an OU or AD user object?

Looking at this link, shops is analagous to Finance in example 1.
Well, since you retrieved it Get-ADUser, the full object is a user object (or what's left of it, after the Select-Object).
But that still doesn't answer my question what you mean with "Shops".
If you want to set a field to "Shops", then just set that field to "Shops".
If "Shops" is related to the AD user objects you retrieved, you need to explain in detail what it is exactly you need extracted.
Currently, you're just asking how to retrieve an arbitrary name from an arbitrary string.
Avatar of AlHal2

ASKER

There will be one string like the above for each employee in the company.
There are 4 OU objects in the above string.  I want the 3rd which, in the above example is shops.  It represents the department in which they work.
Avatar of AlHal2

ASKER

I have syntax like this which I would like to adapt.

Set-PnPListItem -List FeaturedContentTest -Identity 36 -Values @{"Directorate"="www"}
I want to change www to the third OU object.
Also, I will need to replace -Identity 36 with another column in my Sharepoint list - called ClickerEmail.

I have the clicker's email address in my Sharepoint list and in my download from active directory.  The active directory download also has the directorate.  I want Powershell to do a lookup on the email address to update the list with the clicker's directorate.
Avatar of AlHal2

ASKER

I've got this far.

Set-PnPListItem -List FeaturedContentTest -Identity $listItem["ID"] -Values @{"Directorate"="eee"}

Just need to replace eee with the 3rd OU item, in this case shops.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlHal2

ASKER

Thanks.  I will look at this on Monday.
Avatar of AlHal2

ASKER

Each directorate has lots of departments, so the department name is no good.  The directorate is one of the OU values.  Where the primary OU is Users, the directorate is the 2nd OU.  Where the primary OU is users and computers, the directorate is the 3rd OU.
Please could you elaborate on this part of the code.

Where-Object {$_})[$ouIndex]}}

Open in new window

That splits the user's DN into its components, which turns it into an array. [$ouIndex] is the index of the element you want. The "Where-Object {$_}" removes the empty element from the first "CN="
($_.DistinguishedName -split '(?:^|,)(?:CN|DC|OU)=' | Where-Object {$_})[$ouIndex]

Open in new window

PS C:\> 'CN=User,OU=OU1,OU=OU2,OU=OU3,DC=domain,DC=com' -split '(?:^|,)(?:CN|DC|OU)=' | Where-Object {$_}
User
OU1
OU2
OU3
domain
com

Open in new window

Avatar of AlHal2

ASKER

Thanks very much.