none
Exporting CSV objects RRS feed

  • Question

  • What I'm trying to accomplish is import a csv, sort through it and create only new users. That's done, the next step is exporting a csv of the newly created users and passwords. I've tried a couple things and it won't give me an error but nothing is added to the csv. Here is an example, and all information may not be in here but what is needed to get my point across is. My csv has 12 headers and I only need the three listed below, but even if I can get all of them that would be fine too.

    $TestCSV = Import-Csv C:\Scripts\Add_AD_Users.csv | ForEach-Object{
    $selected = Select-Object $_.SamAccountName, $_.EmailAddress, $_.Password | export-csv -path C:\Scripts\Add_AD_Users.csv -append
    }


    Friday, April 1, 2016 8:32 PM

Answers

  • But after trial and error and research I still don't have an answer I figure I might as well see what someone else can do.

    The problem is that you're expecting someone to take the time to figure out everything that you want to do. We can help answer specific questions, but it's really beyond the scope of this forum to try to figure out the meaning of a very vague specification (based on guesswork from multiple posts) and redesign a working solution for you.

    Instead, we would recommend starting by learning the fundamentals of PowerShell. Guesswork is only going to be frustrating for you. Learning the fundamentals will be more time consuming at first but it will be far less frustrating in the long run. It will also put you in a good position to understand the answers you have already been given.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, April 5, 2016 2:49 PM
    Moderator

All replies

  • It looks like you are trying too hard. Use Import-Csv to import the CSV, then use Select-Object to select the properties you want. At the end of the pipeline, export to a new CSV file.


    PS C:\> Import-Csv firstCSVfile.csv |
      Select-Object SamAccountName,EmailAddress,Password |
      Export-Csv newCSVfile.csv -NoTypeInformation
    


    -- Bill Stewart [Bill_Stewart]

    Friday, April 1, 2016 9:10 PM
    Moderator
  • I should've posted more of the script but, what is happening is I'm creating a user in AD for users that aren't already created and I need to export the username email and password of only the users I created. That's why the foreach is in there because I can make it work in other areas but not for that.
    Monday, April 4, 2016 6:36 PM
  • Unfortunately, it won't be possible for me to help answer questions about code that I have never seen.

    -- Bill Stewart [Bill_Stewart]

    Monday, April 4, 2016 6:38 PM
    Moderator

  • $TestCSV = Import-Csv C:\Scripts\Add_AD_Users.csv 
    $TestCSV | ForEach-Object{
               if(Get-Aduser -Filter "SamAccountName -eq '$($_.SamAccountName)'"){
                 Write-Host "$($_.SamAccountName) User Exists!"
               }
               elseif($_.Description -gt 4){
                        Write-Host "$($_.SamAccountName) Doesn't Exist! Creating AD User name and Password as well as Google email and password."
                        New-ADUser -Name $_.Name -DisplayName $_.DisplayName -GivenName $_.GivenName -Surname $_.Surname -UserPrincipalName $_.UserPrincipalName -SamAccountName $_.SamAccountName -HomeDrive $_.HomeDrive -HomeDirectory  $_.HomeDirectory -AccountPassword (ConvertTo-SecureString $_.Password -AsPlainText -force) -Enabled $True -ChangePasswordAtLogon $True -EmailAddress $_.EmailAddress -Description $_.Description -Path $_.Path -verbose
                        #.\gam.exe create user $_.EmailAddress firstname $_.GivenName lastname $_.Surname password $_.Password
                        Select-Object SamAccountName, EmailAddress, Password | Export-CSV -Append  -Path C:\Scripts\Created_Users.csv -NoTypeInformation

                    }
    Monday, April 4, 2016 6:40 PM
  • Please use the code insertion tool. What you posted is a jumble and is difficult to read.

    -- Bill Stewart [Bill_Stewart]

    Monday, April 4, 2016 6:45 PM
    Moderator
  • $TestCSV = Import-Csv C:\Scripts\Add_AD_Users.csv 
    $TestCSV | ForEach-Object{
               if(Get-Aduser -Filter "SamAccountName -eq '$($_.SamAccountName)'"){
                 Write-Host "$($_.SamAccountName) User Exists!"
               }
               elseif($_.Description -gt 4){
                        Write-Host "$($_.SamAccountName) Doesn't Exist! Creating AD User name and Password as well as Google email and password."
                        New-ADUser -Name $_.Name -DisplayName $_.DisplayName -GivenName $_.GivenName -Surname $_.Surname -UserPrincipalName $_.UserPrincipalName -SamAccountName $_.SamAccountName -HomeDrive $_.HomeDrive -HomeDirectory  $_.HomeDirectory -AccountPassword (ConvertTo-SecureString $_.Password -AsPlainText -force) -Enabled $True -ChangePasswordAtLogon $True -EmailAddress $_.EmailAddress -Description $_.Description -Path $_.Path -verbose
                        #.\gam.exe create user $_.EmailAddress firstname $_.GivenName lastname $_.Surname password $_.Password
                        Select-Object SamAccountName, EmailAddress, Password | Export-CSV -Append  -Path C:\Scripts\Created_Users.csv -NoTypeInformation
    
                    }

    Monday, April 4, 2016 6:48 PM
  • Your logic will likely fail under many circumstances.

    You should at least manage the else case to see what is being skipped.

    Select-Object will do nothing as it has no input object:

    $object | select-object ....

    If we expand an remove al useless code we will see what is happenin g:

    New-ADUser -Name $_.Name -DisplayName $_.DisplayName -GivenName $_.GivenName -Surname $_.Surname -UserPrincipalName $_.UserPrincipalName -SamAccountName $_.SamAccountName -HomeDrive $_.HomeDrive -HomeDirectory  $_.HomeDirectory -AccountPassword (ConvertTo-SecureString $_.Password -AsPlainText -force) -Enabled $True -ChangePasswordAtLogon $True -EmailAddress $_.EmailAddress -Description $_.Description -Path $_.Path -verbose
    
    #the following statements do nothing
    Select-Object SamAccountName, EmailAddress, Password | 
          Export-CSV -Append  -Path C:\Scripts\Created_Users.csv -NoTypeInformation
    

    Add "-PassThru" to New-AdUser and add the select as a pipeline element.


    \_(ツ)_/

    Monday, April 4, 2016 6:55 PM
  • No other solutions?
    Monday, April 4, 2016 7:45 PM
  • No other solutions?
    For what? You need to take jrv's advice and pass an object into Select-Object if you want any output.

    Monday, April 4, 2016 7:49 PM
  • Please don't misunderstand, but the advice you've been given thus far is intended to provoke you to learn more and grow in your understanding of how PowerShell works. It's not really the purpose of this forum to rewrite broken code and fix code that's been pasted together by guesswork.

    -- Bill Stewart [Bill_Stewart]

    Monday, April 4, 2016 8:07 PM
    Moderator
  • I gave you pretty explicit details on what is missing. Unfortunately you are not being very clear about what it is you need as an output.  The best we can do is to pint you in the right direction.

    If you take some time to learn the basics of PowerShell all of this will be much easier.  The "Sele3ct-Object" command I fundamental.  You need to learn how it works.  Start by using the help. If you still do not understand why it is not working then as a specific question.

    Using your example here is a simple demo of how it works:

    Import-Csv C:\Scripts\Add_AD_Users.csv |
    	ForEach-Object{
     		$samname=$_.SamAccountName
    Get-Aduser -Filter "SamAccountName -eq '$SamName'" | Select-Object SamAccountName, EmailAddress, Password | Export-CSV -Append -Path C:\Scripts\Created_Users.csv -NoTypeInformation


    \_(ツ)_/


    • Edited by jrv Monday, April 4, 2016 9:07 PM
    Monday, April 4, 2016 8:52 PM
  • You can expand this in steps to get what you want:

    Import-Csv C:\Scripts\Add_AD_Users.csv |
    	ForEach-Object{
    $samname=$_.SamAccountName if($user=Get-Aduser -Filter "SamAccountName -eq '$SamName'"){ $user }else{ Write-Host "User not found: $($_.SamAccountName)" -fore green } } | Select-Object SamAccountName, EmailAddress, Password | Export-CSV -Append -Path C:\Scripts\Created_Users.csv -NoTypeInformation

    You can just add a piece at a time until you get the results you are looking for.

    Note that "Password" will never be found.  It is not possible to retrieve a password.  That is basic Windows 101.


    \_(ツ)_/



    • Edited by jrv Monday, April 4, 2016 9:07 PM
    Monday, April 4, 2016 8:56 PM
  • Note that I also fixed the filter syntax so it works as expected.

    \_(ツ)_/

    Monday, April 4, 2016 9:08 PM
  • Now to make this work as I posted (using -passthru) we would just add this:

    Import-Csv C:\Scripts\Add_AD_Users.csv |
    	ForEach-Object{
    		$samname=$_.SamAccountName
    		if($user=Get-Aduser -Filter "SamAccountName -eq '$SamName'"){
    			$user
    		}else{
    			Write-Host "User not found: $($_.SamAccountName)" -fore green
    			New-AdUser ... blah blah ... -PassThru
    		}
    	} |
    	Select-Object SamAccountName, EmailAddress, Password

    Notice how, after you learn to use PowerShell correctly, the bits just layer in a piece at a time.  We only had to add one line to get all of the new users.  Other simple decorations can be made to tag the entries as new or old and other bits.

    There is no substitute in the technical world for study.  You must take the time to learn.  You cannot learn by osmosis. 


    \_(ツ)_/

    • Proposed as answer by jrv Tuesday, April 5, 2016 5:15 PM
    Monday, April 4, 2016 9:13 PM
  • I gave you pretty explicit details on what is missing. Unfortunately you are not being very clear about what it is you need as an output.  The best we can do is to pint you in the right direction.

    If you take some time to learn the basics of PowerShell all of this will be much easier.  The "Sele3ct-Object" command I fundamental.  You need to learn how it works.  Start by using the help. If you still do not understand why it is not working then as a specific question.

    Using your example here is a simple demo of how it works:

    Import-Csv C:\Scripts\Add_AD_Users.csv |
    	ForEach-Object{
     		$samname=$_.SamAccountName
    Get-Aduser -Filter "SamAccountName -eq '$SamName'" | Select-Object SamAccountName, EmailAddress, Password | Export-CSV -Append -Path C:\Scripts\Created_Users.csv -NoTypeInformation


    \_(ツ)_/


    next step is exporting a csv of the newly created users and passwords.

    What is not so clear about that?

    Tuesday, April 5, 2016 2:01 PM
  • next step is exporting a csv of the newly created users and passwords.

    What is not so clear about that?

    What are you actually asking? Are you confused about how to do that?

    Are you asking someone to do all the work for you?


    Tuesday, April 5, 2016 2:05 PM
  • But this still isn't what I'm asking for... I'd ask you to step down off your high horse so I can understand you better but you might break a leg falling that far. 

    I've clearly stated my problem twice and you come up with a solution that isn't what I've asked for. There have been multiple times you've mentioned that I need to learn powershell. Now what do you think I'm doing right now. You are such an arrogant person, you have no idea who I'am and half of your responses are constant berating. I don't need to be told to learn, I'm very much capable of figuring that out for myself. But after trial and error and research I still don't have an answer I figure I might as well see what someone else can do. I do not want your help. So please don't waste your time responding to anymore of this post. 

    Tuesday, April 5, 2016 2:19 PM
  • But after trial and error and research I still don't have an answer I figure I might as well see what someone else can do.

    The problem is that you're expecting someone to take the time to figure out everything that you want to do. We can help answer specific questions, but it's really beyond the scope of this forum to try to figure out the meaning of a very vague specification (based on guesswork from multiple posts) and redesign a working solution for you.

    Instead, we would recommend starting by learning the fundamentals of PowerShell. Guesswork is only going to be frustrating for you. Learning the fundamentals will be more time consuming at first but it will be far less frustrating in the long run. It will also put you in a good position to understand the answers you have already been given.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, April 5, 2016 2:49 PM
    Moderator
  • This is funny.  I posted the complete answer in  two ways.  Unfortunately you mcan't understand it if you cannot read PowerShell code.

    I believe that what bill is saying comes down to RTFM.  This is a good place to start.

    Here: http://www.amazon.com/Windows-PowerShell-3-0-Step/dp/0735663394/ref=sr_1_1?ie=UTF8&qid=1344869046&sr=8-1&keywords=powershell%203.0

    Of course there are many more but at least pick one and get through it a couple of times.  It will make things much easier.

    Good luck and happy Poshing.


    \_(ツ)_/

    Tuesday, April 5, 2016 5:18 PM