Reputation: 1
I have a simple script that looks like this
Get-ADUser 12345678 -Properties extensionAttribute11 | Select extensionAttribute11.
What this script does is get me the data which is associated to the extensionattribute11 value from AD, which in my case happens to map to a users card, from that card we get a hex number, which means that when a student scans their card over a card reader, they are automatically picked up in an attendance register.
One thing I cant do is get the script to do do this for a set of users. I can export a list of users into a CSV file but I dont know if that can be imported or if it can, would it work. Someone did suggest using an array and although I know what an array is, my knowledge of powershell is minimal.
In my head this should work Get-ADUser 12345678, 23456789, 12745423 -Properties extensionAttribute11 | Select extensionAttribute11, but obviously this doesnt.
Does anyone have any pointers? I would sort of like to crack this myself as its good to try and work things out but if someone has a ready made solution or something similar, that wouldnt hurt.
CHeers
Ed
Upvotes: 0
Views: 196
Reputation: 61028
If you can create a CSV file with relevant userdata from the students you want to get the cardnumbers for, something like this:
"SamAccountName", "PossibleOtherData" "jdoe", "something worth knowing" "jtrump", "no. 1 snooker player"
You can use that to do the following:
$users = Import-Csv -Path 'PathToYourCsvFile' | Select-Object -ExpandProperty SamAccountName
# or shorter: $users = (Import-Csv -Path 'PathToYourCsvFile').SamAccountName
$students = Get-ADUser -Filter "extensionAttribute11 -like '*'" -Properties DisplayName, extensionAttribute11 |
Where-Object { $users -contains $_.SamAccountName } |
Select-Object DisplayName, @{Name = 'CardNumber'; Expression = { $_.extensionAttribute11}}
# display on screen
$students
# write to new CSV file
$students | Export-Csv -Path 'PathToYourOutputCsvFile' -UseCulture -Encoding UTF8 -NoTypeInformation
Worth knowing is that the Get-ADUser
cmdlet will by default return objects with these properties: DistinguishedName
, Enabled
, GivenName
, Name
, ObjectClass
, ObjectGUID
, SamAccountName
, SID
, Surname
, UserPrincipalName
. All extra properties you need to ask for using the -Properties
parameter.
Also, you can make the code faster if you have a searchbase OU to look for the students. If all students are in a dedicated OU, you can add for instance this -SearchBase "OU=Students,OU=UserAccounts,DC=YourCompany,DC=org"
to the Get-ADUser
cmdlet
If you have located all your students inside a dedicated OU or Group, there should be no need to create a CSV file first and below should do it:
In case all your students can be found in the same OU:
# put the DistinguishedName of the OU here
$ouDN = "OU=Students,OU=UserAccounts,DC=YourCompany,DC=org"
$students = Get-ADUser -Filter "extensionAttribute11 -like '*'" -SearchBase $ouDN -Properties DisplayName, extensionAttribute11 |
Select-Object DisplayName, @{Name = 'CardNumber'; Expression = { $_.extensionAttribute11}}
# display on screen
$students
# write to new CSV file
$students | Export-Csv -Path 'PathToYourOutputCsvFile' -UseCulture -Encoding UTF8 -NoTypeInformation
If all students are members of an AD group like for instance "Students", you could do:
$students = Get-ADGroupMember -Identity "Students" -Filter "objectClass -eq 'user'" |
Get-ADUser -Properties DisplayName, extensionAttribute11 |
Where-Object { $_.extensionAttribute11 -like '*' }
Select-Object DisplayName, @{Name = 'CardNumber'; Expression = { $_.extensionAttribute11}}
# display on screen
$students
# write to new CSV file
$students | Export-Csv -Path 'PathToYourOutputCsvFile' -UseCulture -Encoding UTF8 -NoTypeInformation
Upvotes: 1