Jump to content

Recommended Posts

Posted

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

Posted
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}
}

  • Thanks 1
Posted

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

Posted

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

Posted

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.

Posted

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

Posted

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

Posted (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 by pcstru

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...