Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 554
  • Last Modified:

Need help writing a loop in a Powershell script

Hi guys,

I am teaching myself Powershell 3.0 while I am hacking together a script that will take a CSV file of names and emails and reformat its contents into a new file that CSVDE.exe can import into AD as new contacts for a distribution list.

I have successfully learnt how to import a CSV into a variable, export it to a new CSV file as well as confirmed that I can create a CSV that CSVDE can import into AD.

Where I am stuck is understanding how loops work and getting the individual vaules from an array properly. I can only seem to return all values in a column or only one value. Because I dont yet understand which method of creating a loop I should be using and how.

I have been using a "foreach" loop thinking, perhaps incorrectly, that it was best suited to my needs by looking at other examples online and in help files.

My source file has two columns of data only.
Column A is 'name' and column B is 'email'
I want to take the name and email from each line and reformat it into a new file with these 3 columns and properties

DW,objectClass,email

so to demonstrate how that should look with my data it should look like below.

"CN=(NAME from CSV),OU=All Suppliers,OU=Company,OU=Domain Users,DC=domain,DC=local",contact,(EMAIL from CSV)

How do I read each line from the souce CSV manipulate it it looks like the above using Powershell?

Thanks in advance,
AC
0
defecta
Asked:
defecta
  • 5
  • 3
1 Solution
 
SubsunCommented:
You don't need a foreach loop to convert the csv file. You can do it by creating custom properties.. Try..
Import-Csv C:\temp\input.csv | Select @{N="DN";E={"CN=$($_.Name),OU=All Suppliers,OU=Company,OU=Domain Users,DC=domain,DC=local"}},@{N="objectClass";E={"contact"}},email | Export-Csv C:\Temp\result.csv -NTI

Open in new window

Ref :
http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/19/create-custom-objects-in-your-powershell-script.aspx

Let me know if you have any questions..

BTB, here is a nice tutorial about loop processing in PowerShell
http://www.powershellpro.com/powershell-tutorial-introduction/logic-using-loops/
0
 
defectaAuthor Commented:
Wow, thats amazing! Thank you so much!

I am thinking I could also use this method to take an extra step out of my script too.

The original file I start with has many columns I dont need that I have been using PS to create a new file with just the data I need but this is much better!

Thanks again!

AC
0
 
defectaAuthor Commented:
Quick question though.

This creates a CSV with double quotes around every attribute which CSVDE doesn't like.

It only needs to be around the DN attribute.

I tried to remove them from the code but it breaks. Is there a switch to prevent them from being created in the output or something?


EDIT: Nevermind I think its something else because the attribute name didn't match and I had to change it. Working on it.

Cheers,
AC
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SubsunCommented:
Export-csv output will have the quotes. If you want to exclude it then you need to use a different method to create the output file..
0
 
defectaAuthor Commented:
hmm ok, thanks for that.

Do you have any suggestions on alternatives?

I have just done some testing and I dont think the quotes are an issue as a manually created test file works with the quotes.

But I do have an issue with names field containing commas. I am learning how to remove/replace them now.
0
 
SubsunCommented:
Try..
$file = "C:\Temp\result.csv"
Add-Content $file "DN,objectClass,email"
Import-Csv C:\Temp\input.csv | % {
  Add-Content $file "`"CN=$($_.Name),OU=All Suppliers,OU=Company,OU=Domain Users,DC=domain,DC=local`",contact,$($_.email)"
}

Open in new window

In future, Please create new request for any additional requirements which is out of the current question scope..
0
 
defectaAuthor Commented:
Sure, sorry I was actually happy to work this out myself.

Unless I am mistaken, the above script creates the file without all the additional quotes, does it not?

BTW I found the problem. The 3rd attribute is meant to be 'mail' but it was 'email'. :)
0
 
defectaAuthor Commented:
So i have finally figured out how to get it all working perfectly.

Here is my final code that cleans up commas from the names before creating the result.CSV.

Import-Csv C:\temp\input.csv | 
where {$_.EmailAddress -ne "" -and $_."Is Enabled" -eq "yes"} | 
Select @{N="DN";E={"CN=$($_."name" -replace ",", """ ),OU=All Suppliers,OU=Domain Users,DC=domain,DC=local"}},@{N="objectClass";E={"contact"}},@{N="mail";E={"$($_.EmailAddress)"}} | 
Export-Csv C:\Temp\result.csv -NTI

Open in new window


Thanks for all your help Subsun.

Now if only I could get it piping directly to CSVDE?? :P (thats not a request for help Subsun. :D)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now