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.
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.
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.
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?
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.
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.
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.