Вопрос

I have a CSV file that I am treating as Master file. Not all of the headers from the CSV are part of a user in AD.

The master CSV has the following fields:

  • Last Name
  • First Name
  • Last 4 SSN
  • Month of Birth
  • Day of Birth
  • COMPANY Employee ID
  • Titles
  • Department Name
  • Phone Number

I need to compare the Company Employee ID field from the CSV against the employeeNumber attribute from AD. If they match, the following fields should be written to a new CSV:

  • Last Name
  • First Name
  • Last 4 SSN
  • Month of Birth
  • Day of Birth
  • COMPANY Employee ID
  • Titles
  • Department Name
  • Phone Number
  • Email Address

Last Name and First Name come from AD, Last 4 SSN, Month of Birth, Day of Birth and Phone Number come from the master CSV file.

COMPANY Employee ID, Titles, Department Name and Email Address in the output CSV should take the values of the AD attributes employeeNumber, title, department and mail respectively.

If they do not match, the record from the master CSV needs to be part of the new CSV with the same headers mentioned above.

@Kobhrl: I could not add the whole code in the comment. I have so far the following:

Function CheckAD
{
  $MasterFile,$CSV,$AD = @()
  DomainController = "companydc02.companycolo.pvt"
  $companyUsersOU = "OU=company,DC=companycolo,DC=pvt"
  $MasterFile = "C:\scripts\edir\edir_headers01.csv"
  $CSVADMismatch = "C:\scripts\edir\edir_headers01_1.csv"
  $CSV = Import-Csv $MasterFile
  ForEach ($User in $CSV)
  {
    $ClockNumber = $User."company Employee ID"
    Get-ADUser -SearchBase $companyUsersOU -Server $DomainController `
        -Filter '(employeeNumber -eq $ClockNumber)' | ForEach-Object {
      If ($User.$ClockNumber -eq $_.employeeNumber) {
        write-host "Last Name " $_.surname
      }
    }
  }
}

LATEST CODE but I am stuck:

Function CheckAD
{
 $MasterFile,$CSV,$AD,$Output = @()
 $DomainController = "companydc02.companycolo.pvt"
 $companyUsersOU = "OU=company,DC=companycolo,DC=pvt"
 $MasterFile = "C:\scripts\edir\edir_headers01.csv"
 $CSVADMismatch = "C:\scripts\edir\edir_headers01_1.csv"
 $CSV = Import-Csv $MasterFile
ForEach ($User in $CSV)
{
    $ClockNumber = $User."company Employee ID"
    #Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(employeeNumber -eq $ClockNumber)' -Properties * | `
    #Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(employeeNumber -eq $ClockNumber)' -Properties * | Select Surname, givenName, employeeNumber, department, mail
    Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(employeeNumber -eq $ClockNumber)' -Properties surname,givenName,employeeNumber,department,mail | `
    # If ($AD)
    # {
            # $Output += New-Object PSObject -Property @{
                # "Last Name"=$_.surname
                # "First Name"=$_.givenName
                # "Last 4 SSN"=$User."Last 4 SSN"
            # }
    # ForEach-Object { 
            # write-host "Last Name: " $_.surname
            # write-host "First Name: " $_.givenName
            # write-host "Last 4 SSN: " $User."Last 4 SSN"
            # write-host "Month of Birth: " $User."Month of Birth"
            # write-host "Day of Birth: " $User."Day of Birth"
            # write-host "company Employee ID: " $_.employeeNumber
            # write-host "Titles: " $User."Titles"
            # write-host "Department Name: " $_.department
            # write-host "Phone Number: " $User."Phone Number"
            # write-host "Email Address: " $_.mail              
            # write-host ""
    # }
    #$Ad | Sort-Object -Property sn, givenName | Select SamAccountName,Surname,GivenName,Name,Department,Title,TelephoneNumber,EmployeeNumber | Export-Csv "c:\scripts\ceridian\06-10-2013_ExportedADUsersWithClockNumber.csv" -NoTypeInformation
    Select @{n="Last Name";e={$_.surname}},@{n="First Name";e={$_.givenName}},@{n="Last 4 SSN";e={$User."Last 4 SSN"}},@{a="Month of Birth";b={$User."Month of Birth"}},@{c="Day of Birth";d={$User."Day of Birth"}},@{f="COMPANY Employee ID";g={$_.employeeNumber}},@{i="Titles";j={$User."Titles"}},@{k="Department Name";l={$_.department}},@{m="Phone Number";o={$User."Phone Number"}},@{p="Email Address";q={$_.mail}} | Export-CSV $CSVADMisMatch -NoTypeInformation
    #}
}
#$Output | Sort "Last Name" | Export-CSV $CSVADMisMatch -NoTypeInformation

}

Это было полезно?

Решение

I've done something similar at my job and basically did what you're doing and running a loop for each user. Pull the data from AD into an object, and then updating records in a custom object. So, something like this:

$DomainController = "companydc02.companycolo.pvt"
$companyUsersOU = "OU=company,DC=companycolo,DC=pvt"
$MasterFile = "C:\scripts\edir\edir_headers01.csv"
$CSVADMismatch = "C:\scripts\edir\edir_headers01_1.csv"
$CSV = Import-Csv $MasterFile
$Output = @()
ForEach($User in $CSV){
    Remove-Variable Current
    $Current = Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(employeeNumber -eq $ClockNumber)'
    If($Current){ #If AD match is found
        $Output += New-Object PSObject -Property @{
            "Last Name"=$Current.surname
            "First Name"=$Current.givenname
            "Last 4 SSN"=$User.'Last 4 SSN'
            "Other Fields"="Other Values"
        }
    }else{ #If no AD match
        $Output += New-Object PSObject -Property @{
            "Last Name"=$User.'Last Name'
            "First Name"=$User.'First Name'
            "Last 4 SSN"=$User.'Last 4 SSN'
            "Other Fields"="Other Values"
        }
    }
}
$Output|Export-Csv C:\Somefile.csv -NoTypeInformation

You will need to fill in various fields, but that imports your master file, loops through each user and looks them up in AD. If they are found it creates a new object from both the existing user and from AD, if they aren't found it just uses the values from the existing user. Then it adds that object to a master listing. Lastly I had it output that to a new CSV.

Edit: So, you didn't use my code, but you are saying you are getting errors with my code. What you have above doesn't resemble what I gave you at all (except parts that are commented out). If you want help, use what is given you. If you won't use what is given you don't ask for help.

What you have won't work because your hashtables are constructed wrong. The parts that are @{n="Name";e={Expression}} have some random letters thrown in there. n= is short for Name= and e= is short for Expression=, so what it should be saying is things like: Name of the object = "Last Name" Expression to determine the value = {$User.'Last Name'}

but you have random letters thrown in there like @{o='Last Name';p={$User.'Last Name'}} and Powershell has no idea what o= and p= is supposed to mean, so it throws errors.

Now, you have this as a function, and I'm not sure why. You aren't treating it like a function really. In the spirit of keeping it a function for some reason I've re-done what you have above to keep it as one. This sets it up as a function, and then calls that function, and outputs it to a CSV.

Function CheckAD{
    Param($MasterFile = "C:\scripts\edir\edir_headers01.csv",
    $DomainController = "companydc02.companycolo.pvt",
    $companyUsersOU = "OU=company,DC=companycolo,DC=pvt")
    $CSV = Import-Csv $MasterFile
    ForEach($User in $CSV)
    {
        $ClockNumber = $User."company Employee ID"
        Remove-Variable AD
        $AD = Get-ADUser -SearchBase $companyUsersOU -Server $DomainController -Filter '(employeeNumber -eq $ClockNumber)' -Properties surname,givenName,employeeNumber,department,mail
        New-Object PSObject -Property @{"Last Name"=if($AD){$AD.surname}else{$User.'Last Name'}
            "First Name"=if($AD){$AD.givenName}else{$User.'First Name'}
            "Last 4 SSN"=$User."Last 4 SSN"
            "Month of Birth"=$User."Month of Birth"
            "Day of Birth"=$User."Day of Birth"
            "COMPANY Employee ID"=if($AD){$AD.employeeNumber}else{$User.'Company Employee ID'}
            "Titles"=$User."Titles"
            "Department Name"=if($AD){$AD.department}else{$User.department}
            "Phone Number"=$User."Phone Number"
            "Email Address"=if($AD){$AD.mail}else{$User.'Email Address'}
        }
    }
}
$CSVADMismatch = "C:\scripts\edir\edir_headers01_1.csv"
CheckAD | Sort "Last Name" | Export-CSV $CSVADMisMatch -NoTypeInformation
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top