<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Use PowerShell &#187; Tip</title>
	<atom:link href="http://blog.usepowershell.com/category/tip/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.usepowershell.com</link>
	<description>Real Admins Script</description>
	<lastBuildDate>Sat, 17 Jul 2010 14:38:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Adding Configuration To Remoting</title>
		<link>http://blog.usepowershell.com/2010/07/adding-configuration-to-remoting/</link>
		<comments>http://blog.usepowershell.com/2010/07/adding-configuration-to-remoting/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 14:25:57 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Version 2]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/2010/07/adding-configuration-to-remoting/</guid>
		<description><![CDATA[This is a pretty brief post that will need some further elaboration, but Twitter doesn’t provide the best mechanism for longer examples. There are two places where you can configure your remote sessions.. 1) On the remote server – which is covered in a number of places and probably the most discoverable place to put [...]]]></description>
			<content:encoded><![CDATA[<p>This is a pretty brief post that will need some further elaboration, but Twitter doesn’t provide the best mechanism for longer examples.</p>
<p>There are two places where you can configure your remote sessions.. </p>
<p>1) On the remote server – which is covered in a number of places and probably the most discoverable place to put configuration information..&#160; See the help for <a href="http://technet.microsoft.com/en-us/library/dd819508.aspx" target="_blank">about_Session_Configurations</a>.</p>
<p>2) Locally, when you start the PSSession.&#160; This is where we will dig deeper.</p>
<p>&#160;</p>
<p>One of the cool, lesser covered features in remoting is the ability to persist a remote connection to a module.&#160; After you establish a PSSession, you can use <a href="http://technet.microsoft.com/en-us/library/dd347679.aspx" target="_blank">Export-PSSession</a> to create a module that will hold the commands to create the proxies for the commands in that session, as well as the plumbing to recreate that session.</p>
<p><a href="http://technet.microsoft.com/en-us/library/dd347679.aspx" target="_blank">Export-PSSession</a> will create a script module, which you can modify to work how you would like..&#160; </p>
<p>In the module, one of the functions generated is the Get-PSImplicitRemotingSession function.&#160; When you call one of the commands from the imported session, it will get the saved PSSession.&#160; If the session has not been established yet, the Set-PsImplicitRemotingSession function will be called, creating the session.&#160; You can add your any commands you would like after that.. </p>
<p>For example I’m using Invoke-Command below to import a module once the session is connected.&#160; That script block can be anything you need to do to configure your remote session. (NOTE: The Set-PSImplicitRemotingSession is generated by the command, I’m only adding code after that and using the $script:PSSession object that is created.</p>
<pre class="PowerShellColorizedScript"><span style="color: #0000ff">Set-PSImplicitRemotingSession</span> <span style="color: #000000">`
</span>            <span style="color: #000080">-CreatedByModule</span> <span style="color: #ff4500">$true</span> <span style="color: #000000">`
</span>            <span style="color: #000080">-PSSession</span> <span style="color: #000000">(</span>
            <span style="color: #000000">$(</span>
                <span style="color: #a9a9a9">&amp;</span> <span style="color: #ff4500">$script:NewPSSession</span> <span style="color: #000000">`
</span>                    <span style="color: #000080">-ComputerName</span> <span style="color: #8b0000">'192.168.16.150'</span> <span style="color: #000000">`
</span>                    <span style="color: #000080">-ApplicationName</span> <span style="color: #8b0000">'/wsman'</span>    <span style="color: #000080">-ConfigurationName</span> <span style="color: #8b0000">'Microsoft.PowerShell'</span> <span style="color: #000000">`
</span>                    <span style="color: #000080">-SessionOption</span> <span style="color: #000000">(</span><span style="color: #0000ff">Get-PSImplicitRemotingSessionOption</span><span style="color: #000000">)</span> <span style="color: #000000">`
</span>                    <span style="color: #000080">-Credential</span> <span style="color: #000000">(</span> <span style="color: #ff4500">$host</span><span style="color: #a9a9a9">.</span><span style="color: #000000">UI</span><span style="color: #a9a9a9">.</span><span style="color: #000000">PromptForCredential</span><span style="color: #000000">(</span> <span style="color: #8b0000">'Windows PowerShell Credential Request'</span><span style="color: #a9a9a9">,</span> <span style="color: #8b0000">'Enter your credentials for http://192.168.16.150/wsman.'</span><span style="color: #a9a9a9">,</span> <span style="color: #8b0000">'phoenix\steve.murawski'</span><span style="color: #a9a9a9">,</span> <span style="color: #8b0000">'192.168.16.150'</span> <span style="color: #000000">)</span> <span style="color: #000000">)</span> <span style="color: #000000">`
</span>                     <span style="color: #000000">`
</span>                    <span style="color: #000080">-Authentication</span> <span style="color: #8a2be2">Default</span> <span style="color: #000000">`
</span>                   <span style="color: #000000">`
</span>            <span style="color: #000000">)</span>
            <span style="color: #000000">)</span>            

          <span style="color: #0000ff">Invoke-Command</span> <span style="color: #000000">{</span><span style="color: #0000ff">Import-Module</span> <span style="color: #8a2be2">TaskScheduler</span><span style="color: #000000">}</span> <span style="color: #000080">-Session</span> <span style="color: #ff4500">$script:PSSession</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2010/07/adding-configuration-to-remoting/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tip: Focus Your Scripts and Functions</title>
		<link>http://blog.usepowershell.com/2009/06/tip-focus-your-scripts-and-functions/</link>
		<comments>http://blog.usepowershell.com/2009/06/tip-focus-your-scripts-and-functions/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 12:30:00 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/2009/06/tip-focus-your-scripts-and-functions/</guid>
		<description><![CDATA[The PowerShell paradigm is the task based cmdlet.&#160; With cmdlets that surface a single function that handle a multitude of inputs, a PowerShell session or script can read back like a sentence.&#160; Get-Process –Name iexplore &#124; Where-Object {$_.WorkingSet –gt 50000000 } &#124; Stop-Process In the above (often overused, but illustrates the point well) example, Get-Process, [...]]]></description>
			<content:encoded><![CDATA[<p>The PowerShell paradigm is the task based cmdlet.&#160; With cmdlets that surface a single function that handle a multitude of inputs, a PowerShell session or script can read back like a sentence.&#160; </p>
<p>Get-Process –Name iexplore | Where-Object {$_.WorkingSet –gt 50000000 } | Stop-Process</p>
<p>In the above (often overused, but illustrates the point well) example, Get-Process, Where-Object, and Stop-Process all accurately describe the task that they perform.</p>
<p>We can take this same approach with our scripts and functions.&#160; By keeping your scripts and functions focused on performing discreet tasks, you keep with the composable nature of PowerShell and provide yourself the most flexibility in reusing the functions you create.</p>
<p>One of the most common “errors” I see in PowerShell scripts that are shared is the embedding of formatting in the output of the script.&#160; If your script generates output, it should be in the form of objects (custom or otherwise).&#160; (The only caveat here is if your script’s functionality is to handle the output of data, in which case it’s sole focus should be the handling that output.)</p>
<p>The PowerShell ecosystem provides a number of tools for formatting the output of your scripts, whether you want to display your scripts in the console window (<a href="http://technet.microsoft.com/en-us/library/dd347700.aspx" target="_blank">Format-List</a>, <a href="http://technet.microsoft.com/en-us/library/dd315387.aspx" target="_blank">Format-Wide</a>, <a href="http://technet.microsoft.com/en-us/library/dd315392.aspx" target="_blank">Format-Custom</a>, <a href="http://technet.microsoft.com/en-us/library/dd315255.aspx" target="_blank">Format-Table</a>), as a web page (<a href="http://technet.microsoft.com/en-us/library/dd347572.aspx" target="_blank">ConvertTo-HTML</a>), sent to a CSV file (<a href="http://technet.microsoft.com/en-us/library/dd347724.aspx" target="_blank">Export-CSV</a>) or XML (<a href="http://technet.microsoft.com/en-us/library/dd347657.aspx" target="_blank">Export-CLIXML</a>), in Version 2 to a sortable, groupable grid (<a href="http://technet.microsoft.com/en-us/library/dd315268.aspx" target="_blank">Out-GridView</a>), as well as a number of other options (<a href="http://huddledmasses.org/powerboots-output-graphs-to-images-from-powershell/" target="_blank">Visifire graphs via PowerBoots</a>, <a href="http://chadwickmiller.spaces.live.com/blog/cns!EA42395138308430!340.entry" target="_blank">Excel, PDF, or Image via the Reporting Services Redistributable</a>, or <a href="http://dougfinke.com/blog/index.php/2008/08/31/microsoft-research-netmap-and-powershell/" target="_blank">as a netmap using NodeXL</a>).</p>
<p>Take advantage of these, or write your own.&#160; If you write your own, it should work with custom objects, to keep with the composable nature of PowerShell.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/06/tip-focus-your-scripts-and-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tip: Keeping Your Scripts DRY.</title>
		<link>http://blog.usepowershell.com/2009/06/tip-keeping-your-scripts-dry/</link>
		<comments>http://blog.usepowershell.com/2009/06/tip-keeping-your-scripts-dry/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 12:00:00 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/2009/06/tip-keeping-your-scripts-dry/</guid>
		<description><![CDATA[DRY?&#160; Does this mean I can’t use PowerShell on a water-cooled PC? DRY is a principle that can be very familiar to the PowerShell aficionado with a development background.&#160; DRY means Don’t Repeat Yourself.&#160; Keeping your scripts DRY means that our scripts don’t contain repeated code.&#160; Copy/Paste is not your friend! Why should PowerShell scripters [...]]]></description>
			<content:encoded><![CDATA[<p>DRY?&#160; Does this mean I can’t use PowerShell on a water-cooled PC?</p>
<p>DRY is a principle that can be very familiar to the PowerShell aficionado with a development background.&#160; DRY means <strong>Don’t Repeat Yourself</strong>.&#160; Keeping your scripts DRY means that our scripts don’t contain repeated code.&#160; Copy/Paste is not your friend!</p>
<p>Why should PowerShell scripters care about keeping their PowerShell DRY?&#160; One major reason – script maintainability.</p>
<p>PowerShell has a huge advantage over scripting environments/shells in that the noun/verb structure lends itself to very readable scripts.&#160; If there is duplication in your code, that readability can give a PowerShellers a bit of overconfidence when reading/modifying scripts that are new to them or that have not been looked at in a while.</p>
<p>If you find yourself copying and pasting lines of PowerShell between different sections of your script, you could be setting yourself up for some interesting times when you need to make a change.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/06/tip-keeping-your-scripts-dry/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tip: Calculated Properties</title>
		<link>http://blog.usepowershell.com/2009/04/tip-calculated-properties/</link>
		<comments>http://blog.usepowershell.com/2009/04/tip-calculated-properties/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 11:36:59 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/?p=112</guid>
		<description><![CDATA[I really enjoy Jeffery Hicks&#8217;s Prof. PowerShell column.  In two of his recent columns (Perfect Properties and Label Me Perfect), Jeffery goes in to &#8220;calculated properties&#8221;.   A calculated property is a hashtable passed to the Select-Object or Format-* cmdlets as one of the properties to return. Here is an example from Label Me Perfect: [...]]]></description>
			<content:encoded><![CDATA[<p>I really enjoy <a href="http://blog.sapien.com/">Jeffery Hicks&#8217;s</a> <a href="http://mcpmag.com/columns/columnist.asp?columnistsid=81">Prof. PowerShell column</a>.  In two of his recent columns (<a href="http://mcpmag.com/columns/article.asp?editorialsid=3074" target="_blank">Perfect Properties</a> and <a href="http://mcpmag.com/columns/article.asp?editorialsid=3079" target="_blank">Label Me Perfect</a>), Jeffery goes in to &#8220;calculated properties&#8221;.  </p>
<p>A calculated property is a hashtable passed to the Select-Object or Format-* cmdlets as one of the properties to return.</p>
<p>Here is an example from <a href="http://mcpmag.com/columns/article.asp?editorialsid=3079" target="_self">Label Me Perfect</a>:</p>
<blockquote><p>PS C:\&gt; get-wmiobject Win32_logicaldisk -filter &#8220;drivetype=3&#8243; `<br />
-computer  &#8220;mycompany-dc01&#8243; | Format-table  DeviceID,VolumeName,`<br />
@{label=&#8221;Size(MB)&#8221;;Expression={$_.Size/1mb -as  [int]}},`<br />
@{label=&#8221;Free(MB)&#8221;;Expression={$_.FreeSpace/1MB -as  [int]}},`<br />
@{label=&#8221;PercentFree&#8221;;Expression={<br />
&#8220;{0:P2}&#8221; -f  (($_.FreeSpace/1MB)/($_.Size/1MB))}} -autosize</p></blockquote>
<p>That is a whopper to type at the command line or to try to read in a script.</p>
<p>In order to increase readability and resuse, you can assign those hash tables to a variable and use that variable as one of the parameters to Select-Object or one of the Format-* cmdlets.</p>
<blockquote><p>PS C:\&gt; $Size = @{label=&#8221;Size(MB)&#8221;;Expression={$_.Size/1mb -as [int]}}<br />
PS C:\&gt; $FreeMB = @{label=&#8221;Free(MB)&#8221;;Expression={$_.FreeSpace/1MB -as [int]}}<br />
PS C:\&gt; $PercentFree = @{label=&#8221;PercentFree&#8221;;Expression={&#8220;{0:P2}&#8221; -f (($_.FreeSpace/1MB)/($_.Size/1MB))}}</p>
<p>PS C:\&gt; get-wmiobject Win32_logicaldisk -filter &#8220;drivetype=3&#8243; `<br />
-computer &#8220;mycompany-dc01&#8243; | Format-table DeviceID,VolumeName, `<br />
$Size, $FreeMB, $PercentFree -autosize</p></blockquote>
<p>This makes it a bit easier to read, and also makes those hash tables available for further use in additional statements without needing to retype them.</p>
<p> </p>
<p>PSMDTAG: FAQ: Calculated Properties</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/04/tip-calculated-properties/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Tip: Free Up Some Memory</title>
		<link>http://blog.usepowershell.com/2009/03/tip-free-up-some-memory/</link>
		<comments>http://blog.usepowershell.com/2009/03/tip-free-up-some-memory/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 01:03:02 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/2009/03/tip-free-up-some-memory/</guid>
		<description><![CDATA[I’m often running too many applications on my workstation, and at least one session of&#160; PowerShell is almost always open.&#160; I’m regularly jumping back to a PowerShell prompt to try something out, query something from my database, or complete a task.&#160; If I don’t clear out the variables that reference objects I’m done with, the [...]]]></description>
			<content:encoded><![CDATA[<p>I’m often running too many applications on my workstation, and at least one session of&#160; <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx" target="_blank">PowerShell</a> is almost always open.&#160; I’m regularly jumping back to a PowerShell prompt to try something out, query something from my database, or complete a task.&#160; </p>
<p>If I don’t clear out the variables that reference objects I’m done with, the amount of memory used by my PowerShell session increases dramatically, which impacts some other applications that I run.&#160; (I know, memory is cheap.. buy more memory.&#160; I’ve got a limited budget and I have to prioritize.)</p>
<p>So I’ve written a couple of functions that can help me clear out the extraneous variables and free up some of that memory being used by PowerShell.</p>
<p>The first function (and I’m open to suggestions on the naming here..), <a href="http://poshcode.org/973" target="_blank">Set-SessionVariableList</a>, reads in all the variable names in the global scope and <a href="http://blog.usepowershell.com/2009/03/tip-sneaky-storage-whats-in-your-appdomain/" target="_blank">saves them to the AppDomain</a> (so I don’t lose them when I clear out variables).&#160; I recommend running this shortly after starting PowerShell or running it from your profile. </p>
<p>Later, when I’m feeling some memory pressure, I can run the second function <a href="http://poshcode.org/973" target="_blank">Remove-NewVariable</a>, which calls Remove-Variable for all variables whose names I did not save in the AppDomain.&#160; This removes the references to the objects they represented.&#160; </p>
<p><strong>Info Overload: Notice that I said that it removed the references to those objects.&#160; Those objects still exist in memory and remain there until the CLR “garbage collects” them, releasing that memory.&#160; The CLR has a process to reclaim that “used” memory when there is a need to reuse it or when it is impacting other performance.</strong></p>
<p>Even if you skipped the previous bold section (which is perfectly acceptable), you might have noticed that when you clear out a variable, your memory usage for PowerShell might remain high for a while.&#160; My function speeds up the natural “garbage collection” process by calling <a href="http://msdn.microsoft.com/en-us/library/system.gc.collect.aspx" target="_blank">[System.GC]::Collect()</a>, forcing the CLR to reclaim that memory.</p>
<p>There are a couple of “helper” functions included, Get-SessionVariableList, which will list out the variable names saved, in case you wanted to see which variables were going to be left and Add-SessionVariable, which adds more values to the current list.&#160; </p>
<p>&#160;</p>
<p> <script type="text/javascript" src="http://PoshCode.org/embed/973"></script>
<p>Feedback and improvements are always welcome!</p>
<p><a href="http://blogs.msdn.com/powershell/archive/2009/03/01/powershell-folksonomy.aspx" target="_blank">PSMDTAG</a>:FAQ Memory</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/03/tip-free-up-some-memory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tip: Passing Parameters Revisited</title>
		<link>http://blog.usepowershell.com/2009/03/tip-passing-parameters-revisited/</link>
		<comments>http://blog.usepowershell.com/2009/03/tip-passing-parameters-revisited/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 12:22:57 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Version 1]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/2009/03/tip-passing-parameters-revisited/</guid>
		<description><![CDATA[In Jeff Hicks’s Prof. PowerShell series, he continues with his tips on working with functions.  He uses the Check-Service function that was in the previous article and that I provided another option in this post.  Jeff describes how you can make a function act more like a cmdlet. The problem that you run into in [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://twitter.com/JeffHicks" target="_blank">Jeff Hicks</a>’s <a href="http://mcpmag.com/columns/columnist.asp?columnistsid=81" target="_blank">Prof. PowerShell series</a>, he continues with his <a href="http://mcpmag.com/columns/article.asp?editorialsid=3035" target="_blank">tips on working with functions</a>.  He uses the Check-Service function that was in the <a href="http://mcpmag.com/columns/article.asp?editorialsid=3028" target="_blank">previous article</a> and that I provided another option in <a href="http://blog.usepowershell.com/2009/03/tip-passing-parameters/" target="_blank">this post</a>.  Jeff describes how you can make a function act more like a cmdlet.</p>
<p>The problem that you run into in V1 of PoweShell is that it is easy to make your functions work with the pipeline</p>
<p><strong>Function Check-Service {<br />
  Param([string]$service=”spooler” )<br />
  PROCESS<br />
  {<br />
    $wmi=get-wmiobject win32_service -filter “name=’$service’” -computername $_ <br />
    if ($wmi.state -eq “running”)  {<br />
      write $True<br />
    }<br />
    else {<br />
      write $False<br />
    }<br />
  }<br />
}<br />
 <br />
Get-Content servers.txt | Check-Service</strong></p>
<p><em>OR</em> work well with parameters being passed in.</p>
<p><strong>Function Check-Service {<br />
  Param([string[]]$server=$env:computername, [string]$service=”spooler” ) <br />
    $wmi=get-wmiobject win32_service -filter “name=’$service’” -computername $server <br />
    if ($wmi.state -eq “running”)  {<br />
      write $True<br />
    }<br />
    else {<br />
      write $False<br />
    }<br />
} <br />
  <br />
Check-Service –Server Server01, Server02, Server03 </strong></p>
<p>Advanced functions in V2 of PowerShell can alleviate this problem (a topic for a whole new post), but a workaround I’ve found for V1 is to use the Begin block to take certain command line parameters and pass them back into the function via the pipeline.</p>
<p><strong>Function Check-Service {<br />
Param([string[]]$server=$null,[string]$service=”spooler” )<br />
  BEGIN<br />
  {<br />
    if ($server -ne $null)<br />
    {<br />
      $server | Check-Service –Service $service<br />
    }<br />
  }<br />
  PROCESS<br />
  {<br />
    if ($_ -ne $null)<br />
    {<br />
      $wmi=get-wmiobject win32_service -filter &#8220;name=&#8217;$service&#8217;&#8221; -computername $_<br />
      if ($wmi.state -eq “running”)  {<br />
        write $True<br />
      }<br />
      else {  write $False  }<br />
    }<br />
  }<br />
}</strong></p>
<p>This enables both of the above scenarios:</p>
<p>Get-Content servers.txt | Check-Service</p>
<p>Check-Service –Server Server01, Server02, Server03</p>
<p><a href="http://blogs.msdn.com/powershell/archive/2009/03/01/powershell-folksonomy.aspx" target="_blank">PSMDTAG</a>:FAQ param</p>
<p><a href="http://blogs.msdn.com/powershell/archive/2009/03/01/powershell-folksonomy.aspx" target="_blank">PSMDTAG</a>:FAQ pipeline</p>
<p><strong>Updated: (Missed a little recursion bug.. thanks Aleksandar!)</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/03/tip-passing-parameters-revisited/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Tip: Passing Parameters</title>
		<link>http://blog.usepowershell.com/2009/03/tip-passing-parameters/</link>
		<comments>http://blog.usepowershell.com/2009/03/tip-passing-parameters/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 15:50:24 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Version 1]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/2009/03/tip-passing-parameters/</guid>
		<description><![CDATA[Jeffery Hicks recently talked about the Param statement in his Prof. Powershell column for MCP Magazine (Keeping Your Options Open).  If you are new to PowerShell, I would definitely recommend reading the article, as provides a nice introduction to the Param statement.  Jeffrey recommended to cast your parameter as the type of object that you [...]]]></description>
			<content:encoded><![CDATA[<p>Jeffery Hicks <a href="http://mcpmag.com/columns/article.asp?editorialsid=3028" target="_blank">recently talked about the Param statement in his Prof. Powershell column for MCP Magazine</a> (Keeping Your Options Open).  If you are new to PowerShell, I would definitely recommend reading the article, as provides a nice introduction to the <strong>Param</strong> statement. </p>
<p>Jeffrey recommended to cast your parameter as the type of object that you are expecting, as way to catch errors.  I wholeheartedly agree.  That can cause trouble though, if you want to allow consumers of your script to pass in one or more values for that parameter.  In the spirit of the article’s title, I thought I would share a method I’ve come across to handle that situation.</p>
<p>Jeffrey gave is the below function.</p>
<p>Function Check-Service {<br />
  Param([string]$computername=$env:computername,<br />
    [string]$service=&#8221;spooler&#8221; )<br />
  $wmi=get-wmiobject win32_service -filter &#8220;name=&#8217;$service&#8217;&#8221; -computername $computername<br />
  if ($wmi.state -eq &#8220;running&#8221;)  {<br />
    write $True<br />
  }<br />
  else {<br />
    write $False<br />
  }<br />
}</p>
<p>Now, you can easily see where you might want to check for a service on multiple computers, but as this function stands, we would have to call it one time for each computer we want to check.</p>
<p>We can modify this script to take one or more computer names simply by changing the cast for the $computername variable from <strong>[string]</strong> to <strong>[string[]]</strong>.  That changes makes our variable hold an array of strings, rather than just one. </p>
<p>We don’t have to change the default value, as PowerShell handles the details of converting that value to an array. </p>
<p>Since the –ComputerName parameter for Get-WMIObject can take an array of computer names, we don’t have to change much else in the function. </p>
<p>I do add a foreach loop for the reporting end of the script, so we can see which machines have the service running.</p>
<p>And I end up with this (<strong>my changes in bold</strong>):</p>
<p>Function Check-Service</p>
<p>{</p>
<p>  Param( <strong>[string[]]$computername</strong>=$env:computername, [string]$service=&#8221;spooler&#8221; )</p>
<p>  $wmi=get-wmiobject win32_service -filter &#8220;name=&#8217;$service&#8217;&#8221; -computername $computername</p>
<p>  <strong>foreach ($one in $wmi)</strong></p>
<p><strong>  {</strong></p>
<p><strong>    if ($one.state -eq &#8220;running&#8221;) </strong></p>
<p><strong>    {</strong></p>
<p><strong>      write $one.SystemName,</strong> $true</p>
<p><strong>    }</strong></p>
<p><strong>    else </strong></p>
<p><strong>    {</strong></p>
<p><strong>      write $one.SystemName,</strong> $False</p>
<p><strong>    }</strong></p>
<p><strong>  }</strong></p>
<p>}</p>
<p> </p>
<p><a href="http://blogs.msdn.com/powershell/archive/2009/03/01/powershell-folksonomy.aspx" target="_blank">I almost forgot </a>- </p>
<p>PSMDTAG:FAQ param</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/03/tip-passing-parameters/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
