Using Custom Attribute - PowerShell

Hello,

Hoping someone can help as I’m a bit lost on how to do what I need to do.

Basically we have a leaver script that pulls from a HR file but the only source of 100% truth is a column headed ‘clockNumber’ This is a custom attribute in AD so I’m a bit lost on how I can get the script to look at the ‘clockNumber’ column in the csv and find the AD account associated with that ‘clockNumber’ then list the properties to filter out the SAMAccountName and then place the SAMAccountName into a variable called $Username that the script would then use for the remainder of the leaver script.

I hae the script working currently using the SAMAccountName as the $Username but as I said it is not 100% accurate so there are some missed leavers.

Any help or advice would be appreciated.

Thank you.

10 Spice ups

What is the actual field? Is it a truly custom attribute you’ve added, or is it using an ExtensionAttribute, or something similar?

If it’s the latter, you can easily pull that data using PowerShell, and I believe you can even filter Get-ADUser by them.

6 Spice ups

You should be able to retrieve it like any other attribute.

get-aduser -filter "clockNumber -eq '123'" -properties clockNumber 

The caveat is, some attributes are hidden / protected, so you might have to run powershell as admin or a specific user to be able to retrieve them, but that depends on how exactly that attribute was added to the AD schema / user object and the security settings.

6 Spice ups

Are you asking about Get-Date? Like Josh J said we need more information to help. Neally is also on point if that is what you are looking for. To chime off Neally you can also use this method. The ? is Where-Object if you are newer to PowerShell.

Get-ADUser -Properties * | ?{$_.ClockNumber -eq "123"} $Time = Get-Date Set-ADUser -Identity $Username -extensionAttribute1 $Time 
5 Spice ups

Hi Josh,

It’s an actual custom attribute and not an extension attribute. Below is the commands I have but it errors (error below too). It seems to be taking the heading but I only want the samaccount name.

$ClockNumber = $User.ClockNo (This pulls the clock number from the csv)

$GetUser = Get-ADUser -Filter {clockNumber -eq $ClockNumber} (This get’s the user associated with that clock number)

$Username = $GetUser | select samAccountName (This is the sername variable that the script then uses)

The username then shows with the sam account name heading which AD obvilsy doesn’t recognice. How can I put just the samaccountname into the variable?

Thanks

5 Spice ups

You need to add the -ExpandProperty to accomplish that.

$ClockNumber = $User.ClockNo $GetUser = Get-ADUser -Filter {clockNumber -eq $ClockNumber} $Username = $GetUser | select -ExpandProperty samAccountName 
5 Spice ups

You have to tell PowerShell to get the attribute, like in the sample I showed.

get-aduser -filter "clockNumber -eq '$clockNumber'" -properties clockNumber 

ClockNumber is not a default attribute, so you have to specifically tell it to get it.

4 Spice ups

You’d use dot notation or the -expandproperty parameter.

$ADUser = get-aduser -filter "clockNumber -eq '$clockNumber'" $ADUser.samaccountname $ADUser | select-object -expandproperty samaccountname 

And then you can assign that a new variable as needed.

$ADUser = get-aduser -filter "clockNumber -eq '$clockNumber'" $sam = $ADUser.samaccountname $sam 
5 Spice ups

Another piece of advice is use Visual Studio Code to debug your script. It will show the value of the variable. So if one is blank/error you know that is the problem one.

Thank you Neally I forgot the -Properties as well like in my first script example.

4 Spice ups

Yeah, don’t do that in a production script.
You tell it to get ALL AD users and ALL their properties.
If you have a large AD that’s a yuge waste and takes a long time.

5 Spice ups

That is very true Neally :slight_smile:

Feathers if they don’t normally show an attribute with a Get-ADUser use this method with -Properties.

Get-ADUser -Identity $GetUser -Properties clockNumber,extensionAttribute1 

This is just an example so you don’t use more lines. To be honest your $GetUser and $Username could probably just be one line. Not sure how the rest of your script is written is why I say probably.

Hope that helps :slight_smile:

3 Spice ups