Mark Minasi's Tech Forum
Register Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 30
Reply with quote  #1 
I have a CSV that contains multiple fields.  The only relevant fields are employeeNumber and managerNumber. I want to capture the sAMAccountName for both fields.

$employeeList = Import-CSV "c:\input.csv" | select employeeNumber, managerNumber

ForEach ($employee in $employeeList) {
$employeeSAM= Get-ADUser -identity $employee -Properties employeeNumber | select -ExpandProperty sAMAccountName
write-host "$employeeSAM"

$managerSAM= Get-ADUser -identity $employee -Properties managerNumber | select -ExpandProperty sAMAccountName
write-host "$employeeSAM"
}

The output looks like this:

@{sAMAccountName=#######} and then ISE says:

Get-ADUser : Object reference not set to an instance of an object.


I also tried $_.employee. ISE says:

Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is
null.

0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 30
Reply with quote  #2 
OK, this worked:

 Get-ADUser -Filter "employeeNumber -eq $($employee.employeeNumber)" -Properties sAMAccountName | select sAMAccountName


0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 30
Reply with quote  #3 
The script process employee correctly, but the second the line for manager is blank.  The script looks like this:

$employeeList = Import-CSV "c:\input.csv" | select employeeNumber, managerNumber

ForEach ($employee in $employeeList) {

$employeeSAM = Get-ADUser -Filter "employeeNumber -eq $($employee.employeeNumber)" -Properties sAMAccountName | select sAMAccountName | select -ExpandProperty sAMAccountName

$managerSAM = Get-ADUser -Filter "managerNumber -eq $($employee.employeeNumber)" -Properties sAMAccountName | select sAMAccountName | select -ExpandProperty sAMAccountName

Write-Host "$employeeSAM | $managerSAM"


}

0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 30
Reply with quote  #4 
The fields in the CSV are  employeeNumber and managerNumber.

The AD field is employeeNumber.

0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.