none
Script to Get-ADuser by employee number (error checking) RRS feed

  • Question

  • I have a script that gets the AD user by employee number and exports it to a csv, but it doesn't log anything if the employee number isn't found. It just skips over it completely without any error in the window or a log of it.

    Can someone please help me add something that adds a line (in order) saying something like "Employee number not found" in the same csv for each employee number that it couldn't find in AD? See below for the current script.

    $Users = Import-Csv C:\Powershell\EmployeeNumberTest.csv
    $CurrentDate = Get-Date
    $CurrentDate = $CurrentDate.ToString('MM-dd-yyyy')
    
    ForEach ($User in $Users) {
      $EmployeeNumber = $User.EmployeeNumber
      get-ADUser -Filter {EmployeeNumber -eq $EmployeeNumber} -Properties employeeNumber | Select SamAccountName,DistinguishedName,enabled,employeeNumber | Export-Csv -Path "C:\Powershell\EmployeeNumbers.$CurrentDate.csv" -Append
      }



    • Edited by FFwarriorz Wednesday, July 5, 2017 3:00 PM clarified request
    Wednesday, July 5, 2017 2:53 PM

Answers


  •  $Missing = New-Object psobject
     $Missing | Add-Member Noteproperty SamAccountname("No Match")
     $Missing | Add-Member Noteproperty DistinguishedName("No Match")
     $Missing | Add-Member Noteproperty enabled("No Match")
     $Missing | Add-Member Noteproperty employeeNumber (" ")

    $Users = Import-Csv C:\Powershell\EmployeeNumberTest.csv $CurrentDate = Get-Date $CurrentDate = $CurrentDate.ToString('MM-dd-yyyy') ForEach ($User in $Users) { $EmployeeNumber = $User.EmployeeNumber $user = get-ADUser -Filter {EmployeeNumber -eq $EmployeeNumber} -Properties employeeNumber | Select SamAccountName,DistinguishedName,enabled,employeeNumber

    If($user -ne $null){

    $user | Export-Csv -Path "C:\Powershell\EmployeeNumbers.$CurrentDate.csv" -Append

    }else{

    $Missing.employeeNumber = $EmployeeNumber

    $Missing | Export-Csv -Path "C:\Powershell\EmployeeNumbers.$CurrentDate.csv" -Append

    } }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


    • Edited by seanmcavinue Wednesday, July 5, 2017 4:06 PM
    • Marked as answer by FFwarriorz Wednesday, July 5, 2017 4:23 PM
    Wednesday, July 5, 2017 4:06 PM

All replies

  • What is the purpose of the script? Get a list of user accounts that have employee numbers in your list?

    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 5, 2017 3:43 PM
    Moderator
  • Try this:
    $UserList = Import-Csv C:\Powershell\EmployeeNumberTest.csv
    $CurrentDate = Get-Date
    $CurrentDate = $CurrentDate.ToString('MM-dd-yyyy')
    
    $Output = ForEach ($User in $UserList) {
      $AdData = get-ADUser -Filter {EmployeeNumber -eq $User.EmployeeNumber} -Properties employeeNumber | Select SamAccountName,DistinguishedName,enabled,employeeNumber 
          If($AdData){
              [PSCustomObject]@{
                EmployeeNumber = $User.EmployeeNumber
                SamAccountName = $AdData.SamAccountName
                DistinguishedName = $AdData.DistinguishedName
                enabled = $AdData.DistinguishedName
              }
          }
          Else{
              [PSCustomObject]@{
                EmployeeNumber = $User.EmployeeNumber
                SamAccountName = 'n/a'
                DistinguishedName = 'n/a'
                enabled = 'n/a'
              }
          }
      }
    $Output 
    $Output  | Export-Csv -Path "C:\Powershell\EmployeeNumbers.$CurrentDate.csv" -NoTypeInformation



    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Wednesday, July 5, 2017 3:47 PM

  •  $Missing = New-Object psobject
     $Missing | Add-Member Noteproperty SamAccountname("No Match")
     $Missing | Add-Member Noteproperty DistinguishedName("No Match")
     $Missing | Add-Member Noteproperty enabled("No Match")
     $Missing | Add-Member Noteproperty employeeNumber (" ")

    $Users = Import-Csv C:\Powershell\EmployeeNumberTest.csv $CurrentDate = Get-Date $CurrentDate = $CurrentDate.ToString('MM-dd-yyyy') ForEach ($User in $Users) { $EmployeeNumber = $User.EmployeeNumber $user = get-ADUser -Filter {EmployeeNumber -eq $EmployeeNumber} -Properties employeeNumber | Select SamAccountName,DistinguishedName,enabled,employeeNumber

    If($user -ne $null){

    $user | Export-Csv -Path "C:\Powershell\EmployeeNumbers.$CurrentDate.csv" -Append

    }else{

    $Missing.employeeNumber = $EmployeeNumber

    $Missing | Export-Csv -Path "C:\Powershell\EmployeeNumbers.$CurrentDate.csv" -Append

    } }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


    • Edited by seanmcavinue Wednesday, July 5, 2017 4:06 PM
    • Marked as answer by FFwarriorz Wednesday, July 5, 2017 4:23 PM
    Wednesday, July 5, 2017 4:06 PM
  • The purpose is that we have a list of employee numbers that we need to get SAM account names for. In the event that the employee number we have in the CSV doesn't exist in AD, I need it to put a line saying "Employee not found" instead of just skipping over it and not outputting anything into the resultant csv.
    Wednesday, July 5, 2017 4:20 PM
  • Thank you seanmcavinue, your solution worked perfectly.

    BOfH_666, yours did not work and gave me a multitude of errors. Not sure exactly what they meant but it had something to do with the PScustomObject. It also output the correct output file, but it didn't find any data for the legit employee numbers as well as the fake one I had in the input file. See below for the error.

    get-ADUser : Property: 'EmployeeNumber' not found in object of type: 'System.Management.Automation.PSCustomObject'.
    At line:6 char:13
    +   $AdData = get-ADUser -Filter {EmployeeNumber -eq $User.EmployeeNumb ...
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
     
    get-ADUser : Property: 'EmployeeNumber' not found in object of type: 'System.Management.Automation.PSCustomObject'.
    At line:6 char:13
    +   $AdData = get-ADUser -Filter {EmployeeNumber -eq $User.EmployeeNumb ...
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
     
    get-ADUser : Property: 'EmployeeNumber' not found in object of type: 'System.Management.Automation.PSCustomObject'.
    At line:6 char:13
    +   $AdData = get-ADUser -Filter {EmployeeNumber -eq $User.EmployeeNumb ...
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
     
    get-ADUser : Property: 'EmployeeNumber' not found in object of type: 'System.Management.Automation.PSCustomObject'.
    At line:6 char:13
    +   $AdData = get-ADUser -Filter {EmployeeNumber -eq $User.EmployeeNumb ...
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
     
    get-ADUser : Property: 'EmployeeNumber' not found in object of type: 'System.Management.Automation.PSCustomObject'.
    At line:6 char:13
    +   $AdData = get-ADUser -Filter {EmployeeNumber -eq $User.EmployeeNumb ...
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
     
    get-ADUser : Property: 'EmployeeNumber' not found in object of type: 'System.Management.Automation.PSCustomObject'.
    At line:6 char:13
    +   $AdData = get-ADUser -Filter {EmployeeNumber -eq $User.EmployeeNumb ...
    +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    Wednesday, July 5, 2017 4:28 PM
  • The purpose is that we have a list of employee numbers that we need to get SAM account names for. In the event that the employee number we have in the CSV doesn't exist in AD, I need it to put a line saying "Employee not found" instead of just skipping over it and not outputting anything into the resultant csv.

    I don't understand, since all AD user accounts have a sAMAccountName attribute. Do you mean this?

    "I have a list of employee numbers for employees that do not yet have AD accounts, and I need to know which employee numbers do not yet have AD accounts."

    If that is what you are asking, then here is a shorter approach:


    Import-Csv "C:\Powershell\EmployeeNumberTest.csv" | ForEach-Object {
      $user = Get-ADUser -LDAPFilter ("(employeeNumber={0})" -f $_.EmployeeNumber)
      if ( -not $user ) {
        $_
      }
    } | Export-Csv "C:\Powershell\users_without_accounts.csv" -NoTypeInformation
    


    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 5, 2017 4:32 PM
    Moderator
  • The purpose is that we have a list of employee numbers that we need to get SAM account names for. In the event that the employee number we have in the CSV doesn't exist in AD, I need it to put a line saying "Employee not found" instead of just skipping over it and not outputting anything into the resultant csv.

    I don't understand, since all AD user accounts have a sAMAccountName attribute. Do you mean this?

    "I have a list of employee numbers for employees that do not yet have AD accounts, and I need to know which employee numbers do not yet have AD accounts."

    If that is what you are asking, then here is a shorter approach:


    Import-Csv "C:\Powershell\EmployeeNumberTest.csv" | ForEach-Object {
      $user = Get-ADUser -LDAPFilter ("(employeeNumber={0})" -f $_.EmployeeNumber)
      if ( -not $user ) {
        $_
      }
    } | Export-Csv "C:\Powershell\users_without_accounts.csv" -NoTypeInformation
    


    -- Bill Stewart [Bill_Stewart]

    Hi Bill,

    No, we have a list of Employee Numbers that were exported from another system. I need to match them up to the respective AD accounts, and if one doesn't exist for an employee number in our list, then I need it to put a line saying it doesnt exist, in place of the SamAccountName. The post above from seanmcavinue worked and put "No Match" in the cell associated with the non-existent employee number.

    Wednesday, July 5, 2017 4:37 PM
  • I would recommend taking a look at my example anyway, as I am pretty sure you can get the result you want with a lot less code.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 5, 2017 4:40 PM
    Moderator
  • Here is a shorter example of what I think you want to accomplish:


    $dataPath = "C:\Powershell"
    $inputFilename = Join-Path $dataPath "InputFile.csv"
    $outputFilename = Join-Path $dataPath "OutputFile.csv"
    Import-Csv $inputFilename | ForEach-Object {
      $user = Get-ADUser -LDAPFilter ("(employeeNumber={0})" -f $_.EmployeeNumber)
      if ( -not $user ) {
        $_.sAMAccountName = "<missing>"
      }
      $_
    } | Export-Csv $outputFilename -NoTypeInformation
    


    -- Bill Stewart [Bill_Stewart]



    • Edited by Bill_StewartModerator Wednesday, July 5, 2017 5:43 PM
    • Proposed as answer by jrv Wednesday, July 5, 2017 6:02 PM
    Wednesday, July 5, 2017 5:06 PM
    Moderator