Solved

Need help writing a loop in a Powershell script

Posted on 2013-12-03
8
552 Views
Last Modified: 2013-12-03
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
Comment
Question by:defecta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39694114
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
 

Author Closing Comment

by:defecta
ID: 39694146
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
 

Author Comment

by:defecta
ID: 39694165
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 40

Expert Comment

by:Subsun
ID: 39694274
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
 

Author Comment

by:defecta
ID: 39694294
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
 
LVL 40

Expert Comment

by:Subsun
ID: 39694345
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
 

Author Comment

by:defecta
ID: 39694378
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
 

Author Comment

by:defecta
ID: 39694433
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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question