export to csv - Powershell to CSV compare -


hello i'm trying use code found on here compare csv active directory. have csv file list of users. want check file , see if in "email" column spreadsheet matches email address ad. if does, want list email address , include canonical name ad can see ou user account in.

this i'm working with:

$path = "c:\scripts\import.csv" $outpath = "c:\scripts\export.csv" $csv = import-csv $path  import-module activedirectory   foreach ($line in $csv)  {     $user = get-aduser -ldapfilter "(&(objectclass=user)(mail=$($line.email)))"  -properties canonicalname     if ($user -ne $null) {"user exist in ou" + $line.email + $user.canonicalname}     else {"user not found in ad ou  - " + $line.email}  }  

i've been able modify suit needs i'm having trouble piping results out csv file. running script it's shown above outputs want screen i'd have in csv format. if like:

$results = foreach ($line in $csv)  

and use

$results | export-csv $outpath -notypeinformation  

i csv created includes string value, header length , numeric value each line. can use out-file send results txt file, includes same results displayed on screen, i'd csv, not txt file. believe need reference properties of csv file , ad in order build these export file i'm having trouble doing i'm not sure how build in status of whether user found or not.

any assistance appreciated.

update - final code final code went with. compares users in csv ad users in parent , child domain. uses email address field match users. grabs canonical name can see ou user in , if user not found, reports in canonical name field.

$path = "$env:userprofile\desktop\infile.csv" $outpath = "$env:userprofile\desktop\outfile.csv"  # importing csv file  import-module activedirectory $users = import-csv $path |   foreach-object {      write-progress -activity "comparing imported file active directory" -status "processing $($_.email)"   # comparing csv file domain.com users if (     $value1 = (get-aduser -ldapfilter "(&(objectclass=user)(mail=$($_.email)))" -server domain.com -properties canonicalname).canonicalname)      {$_ | add-member -membertype noteproperty -name canonicalname -value $value1 -passthru}  # comparing csv file child.domain.com users elseif (          $value2 = (get-aduser -ldapfilter "(&(objectclass=user)(mail=$($_.email)))" -server child.domain.com-properties canonicalname).canonicalname)     {$_ | add-member -membertype noteproperty -name canonicalname -value $value2 -passthru}  # writing output users not found in either domain else {$_ | add-member -membertype noteproperty -name canonicalname -value "email address not found in active directory" -passthru}  #exporting csv file           new-object -typename pscustomobject -property @{         email = $_.email         canonicalname = $_.canonicalname         lastname = $_."last name"         firstname = $_."first name"      }  } | select-object lastname, firstname, email, canonicalname | sort-object canonicalname | export-csv $outpath -notypeinformation 

i'm not sure resulting csv should like, code adds cannonicalname using calculated properties import.csv , saves export.csv.

$path = "c:\scripts\import.csv" $outpath = "c:\scripts\export.csv" import-module activedirectory  import-csv -path $path |     select-object -property *, @{         n = 'canonicalname'         e = {(get-aduser -ldapfilter "(&(objectclass=user)(mail=$($_.email)))" -properties canonicalname).canonicalname}     } | export-csv -path $outpath -notypeinformation 

update

this version create new csv file 3 columns: userexistinou, email , canonicalname if any:

import-csv -path $path | foreach-object {     $userexistinou = $false     if($canonicalname = (get-aduser -ldapfilter "(&(objectclass=user)(mail=$($_.email)))" -properties canonicalname).canonicalname)     {         $userexistinou = $true     }      new-object -typename pscustomobject -property @{         userexistinou = $userexistinou         email = $_.email         canonicalname = $canonicalname     } } | export-csv -path $outpath -notypeinformation 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -