none
need help in comparing AD users with a list RRS feed

  • Question

  • Windows Server 2008 R2,

    PowerShell

    Major  Minor  Build  Revision
    -----  -----  -----  --------
    5      0      10586  122    

    i have a csv list (with header ID) of employee IDs. i'm trying to compare that list with my AD users to see if some of them are still existing in AD.

    Import-CSV 'd:\empstat.csv' | foreach-object {
      if (Get-ADuser -SearchBase 'ou=Main,dc=ACME,dc=com' -Filter 'samAccountName -like $_.ID') {
        echo $_.ID | Out-File d:\empstill.txt
      }
    }

    when i ran the above script, i get the below errors:

    Get-ADuser : Property: 'ID' not found in object of type: 'System.Management.Automation.PSCustomObject'.
    At C:\Users\15111\OneDrive\scripts\getEmployeeList.ps1:7 char:7
    +   if (Get-ADuser -SearchBase 'ou=Main,dc=ACME,dc=com' -F ...
    +       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    i think my problem is how to use a variable that contains the employee ID in this loop?

    Tuesday, May 24, 2016 8:20 AM

Answers

  • You need to do it like this:

    Import-CSV 'd:\empstat.csv' | 
         foreach-object {
             if (Get-ADuser -Filter "samAccountName -eq '$($_.ID)'") {
        echo $_.ID | Out-File d:\empstill.txt
      }
    }
    


    \_(ツ)_/

    • Marked as answer by Reno Mardo Tuesday, May 24, 2016 9:10 AM
    Tuesday, May 24, 2016 8:49 AM

All replies

  • You need to do it like this:

    Import-CSV 'd:\empstat.csv' | 
         foreach-object {
             if (Get-ADuser -Filter "samAccountName -eq '$($_.ID)'") {
        echo $_.ID | Out-File d:\empstill.txt
      }
    }
    


    \_(ツ)_/

    • Marked as answer by Reno Mardo Tuesday, May 24, 2016 9:10 AM
    Tuesday, May 24, 2016 8:49 AM
  • thanks! that did it.
    Tuesday, May 24, 2016 9:10 AM