Blog
By Pete Zerger on 8/30/2010 2:59:38 AM • Rank (3247) • Views 3440
0

0

 I was recently working on some System Center-related PowerShell scripting and needed to pass multiple values from comma-separated list as inputs for a PowerShell script in which I needed to pass values into a couple of PowerShell functions. There is an example or two on the net with the beginnings of what I needed (HERE). I wanted to post this example for my own reference and for anyone else who might benefit.

In my case, I wanted to take a comma-separated file containing 3 values on each line (line 1 being the header). My intent in this case was to

1. Loop through each record in the csv file

2. Assign each value to the appropriate variable

3. Call two PowerShell functions, passing the output of the first function into the second function. (this step is not actually core to the task of csv input, but just part of my requirements in this case)

Sample comma-separated list (with header and three values)

Parent,Child,WQLQuery
Value1,value2,value3
Value1,value2,value3
Value1,value2,value3

PowerShell Syntax (Sample) to Read Values as Inputs

#Pass in comma-separated list 
$List = Import-Csv C:\scripts\list.txt 
 
ForEach ($entry in $list){ 
 
$Parent = $($entry.Parent)
$Child = $($entry.Child)
$WQL = $($entry.WQLQuery) 
 
#Echo values to verify result (test purposes only)
 
Write-host $Parent 
Write-host $Child
Write-host $WQL 
 
#Call functions and pass values
 
$Value1= Function1 $Parent Server 1 0
Function2 Server $Child $Value1 $WQL 
 
#------------
# Functions
#------------
 
Function1 {
 
<function logic>
 
}
 
 
Function2 {
 
<function logic>
 
}

More PowerShell Resources for System Center

We have a growing collection of PowerShell samples for System Center and general administration. You can find them in the Downloads section and many on this page - PowerShell Scripts for System Center (Master Collection)

Follow System Center Central (via Twitter and RSS)

Twitter_icon rss_big_default_300x300

Comments - Comment RSS


Who Viewed
Who Reviewed
Categories
Related Pages
Shortened URL
http://tinyurl.com/22vwo7k

Top Contributors
Featured Members
Pete Zerger
Points: 65622
Level: System Center Expert
Tommy Gunn
Points: 42748
Level: System Center Expert
Simon Skinner
Points: 40804
Level: System Center Expert
Stefan Koell
Points: 28999
Level: System Center Expert
Andreas Zuckerhut
Points: 27734
Level: System Center Expert