<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://powershell.com/cs/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results for 'app:weblogs' matching tags 'WMI', 'Windows PowerShell', and 'CSV and other delimited files'</title><link>http://powershell.com/cs/search/SearchResults.aspx?q=app:weblogs&amp;tag=WMI,Windows+PowerShell,CSV+and+other+delimited+files&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tags 'WMI', 'Windows PowerShell', and 'CSV and other delimited files'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 (Build: 30929.2835)</generator><item><title>Use a CSV File to Populate Parameters of PowerShell Cmdlets</title><link>http://powershell.com/cs/blogs/hey-scriptingguy/archive/2011/11/04/use-a-csv-file-to-populate-parameters-of-powershell-cmdlets.aspx</link><pubDate>Fri, 04 Nov 2011 05:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:13056</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Segoe;"&gt;&lt;strong&gt;Summary:&lt;/strong&gt; Learn how to use a CSV file to populate parameters of Windows PowerShell cmdlets.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Segoe;"&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;&lt;span style="font-family:Segoe;"&gt;&lt;img title="Hey, Scripting Guy! Question" border="0" alt="Hey, Scripting Guy! Question" align="left" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/q-sm.jpg" width="34" height="34" /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Hey, Scripting Guy! I have a problem. I would like to be able to read content from a comma-separated value (CSV) file that contains a list of WMI classes and servers that I would like to query. I need to be able to read the CSV file, and based upon the data contained in that CSV file, I need to create a query on the fly and execute that query. I am afraid it is too complicated, but if I can do this, it will make things a whole lot easier for us at work. Do you know any secret tricks to help us out of our dilemma? &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;&amp;mdash;SC&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;&lt;span style="font-family:Segoe;"&gt;&lt;img title="Hey, Scripting Guy! Answer" border="0" alt="Hey, Scripting Guy! Answer" align="left" src="http://img.microsoft.com/library/media/1033/technet/images/scriptcenter/qanda/a-sm.jpg" width="34" height="34" /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Hello SC, &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;Microsoft Scripting Guy Ed Wilson here. Getting to go to different places and talk to people about Windows PowerShell is a lot of fun. However, there is a downside&amp;mdash;expense reports. I wish there were a Windows PowerShell cmdlet called &lt;b&gt;Get-Expense&lt;/b&gt;, and another one called &lt;b&gt;New-ExpenseReport&lt;/b&gt;. If there were, I could use a command such as this one:&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;Get-Expense | New-ExpenseReport&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;That is the beauty of the Windows PowerShell pipeline. It looks at incoming properties, and seems to match them with similar items on the other side. Therefore, I can easily type the following:&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;Get-Process | Stop-Process&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;And the command works. This is a really cool example because the &lt;b&gt;Get-Process&lt;/b&gt; default property is &lt;b&gt;name&lt;/b&gt;&lt;i&gt; &lt;/i&gt;and the default property for &lt;b&gt;Stop-Process&lt;/b&gt; is &lt;b&gt;id&lt;/b&gt;&lt;i&gt;. &lt;/i&gt;The Windows PowerShell pipeline figures this stuff out. Cool. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;If a cmdlet or advanced function does not accept piped input, all is not lost because you can always use the &lt;b&gt;Foreach-Object&lt;/b&gt; cmdlet and manually map things inside that cmdlet&amp;rsquo;s scriptblock. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;The key here is that when importing a CSV file via the &lt;b&gt;Import-CSV&lt;/b&gt; cmdlet, each column becomes a property of an object. The other key here is that in a pipeline situation, &lt;b&gt;$_&lt;/b&gt; refers to the current item on the pipeline. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;Using this as background, I want to look at a sample CSV file such as you described. The first column contains a WMI class name, and the second column is the computer name to use. The sample CSV file is shown in the following figure.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/8585.hsg_2D00_11_2D00_4_2D00_11_2D00_01.png"&gt;&lt;img style="border:0px;" title="Image of sample CSV file" alt="Image of sample CSV file" src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/8585.hsg_2D00_11_2D00_4_2D00_11_2D00_01.png" width="426" height="380" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;The following steps are required to read a CSV file and create a WMI query.&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Segoe;"&gt;Use the &lt;b&gt;Import-CSV&lt;/b&gt; cmdlet to import the CSV file.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Segoe;"&gt;Use the &lt;b&gt;ForEach-Object&lt;/b&gt; cmdlet to walk through the imported data as it comes across the pipeline.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Segoe;"&gt;Inside the script block of the &lt;b&gt;Foreach-Object&lt;/b&gt; cmdlet, use the &lt;b&gt;Get-WMIObject&lt;/b&gt; cmdlet to query WMI.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Segoe;"&gt;Map the CSV column headings to the parameters of the &lt;b&gt;Get-WmiObject&lt;/b&gt; cmdlet.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Segoe;"&gt;Use the &lt;b&gt;$_&lt;/b&gt; variable to refer to the current items on the pipeline.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;The command to read a CSV file named WMICSVFile.csv that resides in the C:\fso directory and to query WMI is shown here (in the following command, &lt;b&gt;%&lt;/b&gt; is an alias for the &lt;b&gt;Foreach-Object&lt;/b&gt; cmdlet, and &lt;b&gt;gwmi&lt;/b&gt; is an alias for the &lt;b&gt;Get-WmiObject&lt;/b&gt; cmdlet):&lt;/span&gt;&lt;/p&gt;
&lt;p style="padding-left:30px;"&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;import-csv C:\fso\WMICSVFile.csv | % {gwmi $_.class -cn $_.cn}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;The command to read a CSV file and automatically query WMI along with the associated output is shown in the following figure.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;&lt;a href="http://blogs.technet.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/8508.hsg_2D00_11_2D00_4_2D00_11_2D00_02.png"&gt;&lt;img style="border:0px;" title="Image of command and associated output" alt="Image of command and associated output" src="http://blogs.technet.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-76-18/8508.hsg_2D00_11_2D00_4_2D00_11_2D00_02.png" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;SC, that is all there is to using a CSV file for input to other commands. This also concludes CSV Week. Join me tomorrow for the Weekend Scripter.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:small;"&gt;&lt;span style="font-family:Segoe;"&gt;I invite you to follow me on &lt;/span&gt;&lt;a href="http://bit.ly/scriptingguystwitter" target="_blank"&gt;&lt;span style="font-family:Segoe;"&gt;Twitter&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:Segoe;"&gt; and &lt;/span&gt;&lt;a href="http://bit.ly/scriptingguysfacebook"&gt;&lt;span style="font-family:Segoe;"&gt;Facebook&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:Segoe;"&gt;. If you have any questions, send email to me at &lt;/span&gt;&lt;a href="mailto:scripter@microsoft.com" target="_blank"&gt;&lt;span style="font-family:Segoe;"&gt;scripter@microsoft.com&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:Segoe;"&gt;, or post your questions on the &lt;/span&gt;&lt;a href="http://bit.ly/scriptingforum" target="_blank"&gt;&lt;span style="font-family:Segoe;"&gt;Official Scripting Guys Forum&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:Segoe;"&gt;. See you tomorrow. Until then, peace.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:small;"&gt;&lt;b&gt;Ed Wilson, Microsoft Scripting Guy&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:Segoe;font-size:small;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3462905" width="1" height="1" alt="" /&gt;</description></item></channel></rss>