0

I have a script that imports a CSV, compares the list to AD and creates usernames and passwords. The scripts starts out like this:

$CSV = Import-CSV C:\Scripts\Add_AD_Users.csv

$CSV | ForEach-Object{
    if(user does exist){
        # skip it
    }   
    elseif(user doesn't exist){
        # create user and export password, email, and username to csv.
    }

In the elseif statement I would like to get the current ForEach-object "object" and export that to a CSV. Is that possible? The CSV contains 3 headers that I want to take $_.SamAccountName $_.EmailAddress $_.Password.

Is there a way to do this, so that I only have the newly created users? This can't be done with a -PassThru on Get-Aduser.

honk
  • 9,137
  • 11
  • 75
  • 83
ADK
  • 5
  • 5

2 Answers2

1

It's not super pretty but I would put this into a where clause and then, using Select-Object, output to CSV.

$CSV | 
    Where-Object{$result=try{Get-ADUser $_.samaccountname}catch{$false};!$result} |
    Select-Object SamAccountName, EmailAddress, Password | 
    Export-Csv -NoTypeInformation $path

Since you are not processing users that exist lets test using Where-Object so they do not continue down the pipe. Unfortunately you cannot use -ErorrAction which would have been nicer so we test the result from a try catch block instead.

From the looks of it your CSV file already has the columns SamAccountName, EmailAddress and Password so we just send those through the pipeline.

Community
  • 1
  • 1
Matt
  • 45,022
  • 8
  • 78
  • 119
0

The quickest way is to use a hashtable combined with a PSCustomObject creation accelerator.

$userToExport = [PSCustomObject]@{
    'sAMAccountName'=$_.SamAccountName
    'Email Address'=$_.EmailAddress
    'Password'=$_.Password
     # Each element in '' will be the column header, in order declared.
}

$userToExport | Export-CSV .\absolutefilepath.csv -Append -NoTypeInformation

-NoTypeInformation strips the object type away from the object so it doesn't print it out in your CSV. -Append will format the CSV if empty, and add the current added user to the csv, even if it was created before.

  • One problem I keep encountering with this is "Export-CSV : Cannot process argument because the value of argument "name" is not valid. Change the value of the "name" argument and run the operation again. At line:16 char:37 + $userToExport | Export-CSV -Append -Force -Path C:\Scripts\C ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Export-Csv], PSArgumentException + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.ExportCsvCommand" if the export is removed, it works – ADK Apr 05 '16 at 19:15
  • If these columns already exist then recreating the object seems like a waste. – Matt Apr 05 '16 at 19:37
  • @Matt not sure what you mean? The point of creating a custom object to export means you have control over what values go into the CSV, how the column headers are named, what order they are in, etc. Its the process of packaging a CSV "entry" then sending it off to be added to the CSV. Instead of looking into the input CSV and copying information over, you can validate an object is created then export it to the CSV. – Chris Kuperstein Apr 05 '16 at 19:46
  • You approach would be useful if the op needed to change the column names. Given that this output file would be sent to another process and there was no requirement for this change mentioned I am assuming you don't need to do it. – Matt Apr 05 '16 at 19:53
  • Now I figured it out. This way works the best for the scenario I'm using it. – ADK Apr 06 '16 at 13:58