tech-man Posted October 14, 2015 Posted October 14, 2015 Hello I need to update all my student users on AD adding there Admission Number... can anyone help me out with a powershell script? I have a CSV in the following format: UPN, AdmissionNumber I probably want to populate the pager field. Thanks
halbaradkenafin Posted October 14, 2015 Posted October 14, 2015 Hello I need to update all my student users on AD adding there Admission Number... can anyone help me out with a powershell script? I have a CSV in the following format: UPN, AdmissionNumber I probably want to populate the pager field. Thanks Do they already have UPN in AD? If so then you should be able to do this: $UserDetails = Import-CSV C:\file.csv foreach ($User in $UserDetails) { Get-ADUser -Filter {Office -eq $User.UPN} | Set-ADUser -Replace @{Pager=$User.AdmissionNumber} } 1
tech-man Posted October 14, 2015 Author Posted October 14, 2015 Sorry I meant UserPrincipleName - [email protected]
halbaradkenafin Posted October 14, 2015 Posted October 14, 2015 Sorry I meant UserPrincipleName - [email protected] Same idea, just replace Office in the filter with UserPrincipalName and it should work 1
tech-man Posted October 14, 2015 Author Posted October 14, 2015 This is what I am seeing: $UserDetails = Import-CSV C:\user.csv foreach ($User in $UserDetails) { Get-ADUser -Filter {UserPrincipalName -eq $User.UPN} | Set-ADUser -Replace @{homePhone=$User.AdNo} } CSV: "UPN","Adno" "[email protected]","A12345" Error: Get-ADUser : Property: 'UPN' not found in object of type: 'System.Management.Automation.PSCustomObject'. At C:\users\user\Desktop\UpdateAdNo.ps1:4 char:1 + Get-ADUser -Filter {UserPrincipalName -eq $User.UPN} | Set-ADUser -Replace @{hom ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: ( [Get-ADUser], ArgumentException + FullyQualifiedErrorId : Property: 'UPN' not found in object of type: 'System.Managemen t.Automation.PSCustomObject'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser
halbaradkenafin Posted October 14, 2015 Posted October 14, 2015 Try adding in Write-host $UserDetails[0] after the Import-Csv section to see what Powershell is calling it, could be something strange is happening.
tech-man Posted October 14, 2015 Author Posted October 14, 2015 PS C:\users\user\Desktop> .\UpdateAdNo.ps1 @{[email protected]; Adno=A123456} Get-ADUser : Property: 'UPN' not found in object of type: 'System.Management.Automation.PSCustomObject'. At C:\users\user\Desktop\UpdateAdNo.ps1:5 char:1 + Get-ADUser -Filter {UserPrincipalName -eq $User.UPN} | Set-ADUser -Replace @{hom ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: ( [Get-ADUser], ArgumentException + FullyQualifiedErrorId : Property: 'UPN' not found in object of type: 'System.Managemen t.Automation.PSCustomObject'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser
halbaradkenafin Posted October 14, 2015 Posted October 14, 2015 That looks like it's not actually importing the csv correctly. Any chance you could send me a version of the csv? Just needs to be the same file but with some dummy data, only a few lines really. I'm curious if it's something to do with the formatting of the csv or possibly something else. When I run Import-Csv on a random csv I have lying around I get the following when output the first line of the results: SerialNumber Name Asset tag ------------ ---- --------- Numbers name tag So it looks like for some reason it's converting it to a hashtable rather than an actual object, or at least displaying that way which is pretty weird.
tech-man Posted October 14, 2015 Author Posted October 14, 2015 I did a bit of reading.... and changed the line to: Get-ADUser -Filter "UserPrincipalName -eq '$($User.UPN)'" | Set-ADUser -Replace @{homePhone=$User.AdNo} This worked for some reason.... powershell - Get-ADUser -Filter fails when comparing to an object's property - Stack Overflow
halbaradkenafin Posted October 14, 2015 Posted October 14, 2015 Ah, makes sense with that stack overflow link. Weird that the AD module does that but then again they use things like Properties as a parameter despite almost all the others using singular property.
tech-man Posted October 14, 2015 Author Posted October 14, 2015 I'm slowly getting my head more around PS - how do I get an export of all AD users with UserPrincipalName & homePhone? Thanks!
halbaradkenafin Posted October 14, 2015 Posted October 14, 2015 Get-aduser -filter * -properties homePhone Then pipe it to whatever you want, probably export-csv with the -NoTypeInformation parameter.
pcstru Posted October 14, 2015 Posted October 14, 2015 If you do : Get-ADUser -Properties * | get-member You can see all the attributes you can play with and hence to get what you want Get-ADUser -Properties UserPrincipalName, HomePhone | select-object UserPrincipalName, HomePhone | export-csv c:\tmp\tmp.csv
tech-man Posted October 14, 2015 Author Posted October 14, 2015 Thanks both for your help - @pcstru is it possible to set a base & subcontainers OU for lookup?
pcstru Posted October 14, 2015 Posted October 14, 2015 Apropos of this, we store admission number & staff ID in the employeeid attribute in AD (part of the extended attributes). Can be helpful when the account name is not obvious because (say) we have two John.Smiths in year 7. The following is a small script which helps reconciles a csv containing username and the required id, adding a column to the CSV which indicates whether the user was found and how. The CSV is generally generated from another powershell script which is pulling the info from our MIS, so our MIS and AD are kept in close step : # --------------------------------------------------------------------------- # Script : CheckCSVvsAD.ps1 # # : Script which will use a CSV file containing username and # Id (Admision Number or Staff ID) as input and check for # corresponding entries in AD. Script will add a column to # the data showing whether the AD data exists and how it was # found. CSV will be overwritten to add the column to the # existing set. # # : CSV Columns Required # # Username # Id # # --------------------------------------------------------------------------- Import-Module ActiveDirectory $csv_file = "c:\tmp\users.csv" $infile = import-csv $csv_file $i=0 foreach ($inobj in $infile) { $username = $($inobj.Username) $StudId = $($inobj.Id) $found = "" $error.clear() Get-ADUser $username -errorvariable $iserror | out-null if($?) { $found = "Exists (Account) : $username" } else { $found = "Not Found" $error.clear() $ADEmailObj = Get-ADUser -filter {employeeid -eq $StudId } -errorvariable $iserror if ($ADEmailObj -ne $null) { # Could be multiple accounts. #$ADEmailObj = Get-ADUser -filter {employeeid -eq $StudId } #-properties sAMAccountName | select sAMAccountName $ADEmail="" foreach ($eo in $ADEmailObj) { $ADEmail += $eo.sAMAccountName + "; " } write-host $ADEmail $found="Exists (Student ID) : $ADEmail " } else { $found = "Not Found" } } $inobj | Add-Member –membertype NoteProperty –name "ADExists" –value $found -force Write-Progress -Activity "Checking File " -status "$i of $($infile.count)" -percentComplete ($i / $infile.count*100) $i++ } $infile | Export-CSV $csv_file -notype
halbaradkenafin Posted October 14, 2015 Posted October 14, 2015 Thanks both for your help - @pcstru is it possible to set a base & subcontainers OU for lookup? Use the -SearchBase parameter, it needs a full path though so you'd do something like "OU=Targer,OU=Parent,DC=Domain,DC=Local"
pcstru Posted October 14, 2015 Posted October 14, 2015 (edited) Thanks both for your help - @pcstru is it possible to set a base & subcontainers OU for lookup? Sure, you want the searchbase parameter and pass in the domain and OU details Get-ADUser -filter * -Properties UserPrincipalName, HomePhone -SearchBase "OU=Students, DC=domain, DC=sch, DC=uk". If you have nested OU's the order needs to be from the top down, i.e. OU=Year7, OU=Students, DC= Edited October 14, 2015 by pcstru
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now