AlHal2
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,dis tinguished name | select Name,userprincipalname,dis tinguished name
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=F E,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?
$U=Get-ADUser -Filter * -SearchBase "OU=comp Users and Computers,DC=comp,DC=ads" -Properties Name,userprincipalname,dis
The results come back in a 1D array with lots of values separated by a semi colon eg
@{Name=test; userprincipalname=S71@comp
rs,DC=comp,DC=ads}
How do I retrieve the values?
How do I deal with the fact that OU is repeated?
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?
values. How do I do this?
Please clarify "one of the OU values".
Is "Shops" an OU or an AD user object?
Is "Shops" an OU or an AD user object?
ASKER
Here is the full string with shops in bold.
@{Name=test; userprincipalname=S71@comp .org.uk; distinguishedname=test,OU= Users,OU=F E,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.
@{Name=test; userprincipalname=S71@comp
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.
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.
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.
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.
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.
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.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I will look at this on Monday.
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.
Please could you elaborate on this part of the code.
Where-Object {$_})[$ouIndex]}}
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]
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
ASKER
Thanks very much.
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:
Open in new window
This will export the list to a csv file that can be opened in an editor or Excel or whatever:Open in new window
How do I retrieve the values?You can address them directly, as in all arrays:
Open in new window
Or use it in a ForEach:Open in new window
Or use it in a Where-Filter: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=...