<?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; Base Class Libraries</title>
	<atom:link href="http://blog.usepowershell.com/category/net-framework/base-class-libraries/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>Deep Dive: Error Handling &#8211; Error Types (part 1)</title>
		<link>http://blog.usepowershell.com/2009/07/deep-dive-error-handling-error-types-part-1/</link>
		<comments>http://blog.usepowershell.com/2009/07/deep-dive-error-handling-error-types-part-1/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 12:00:00 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Base Class Libraries]]></category>
		<category><![CDATA[Deep Dive]]></category>
		<category><![CDATA[Error Handling]]></category>
		<category><![CDATA[PowerShell Version 1]]></category>
		<category><![CDATA[PowerShell Version 2]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/2009/07/deep-dive-error-handling-error-types-part-1/</guid>
		<description><![CDATA[I started looking a little deeper at error handling in PowerShell after this StackOverflow question. PowerShell has two kinds of errors – terminating errors and non-terminating errors. Terminating errors are the errors that can stop command execution cold.  Non-terminating errors provided an additional challenge, as you need to be notified of failed operations and continue [...]]]></description>
			<content:encoded><![CDATA[<p>I started looking a little deeper at error handling in PowerShell after <a href="http://stackoverflow.com/questions/1142211/try-catch-does-not-seem-to-have-an-effect" target="_blank">this StackOverflow question</a>.</p>
<p>PowerShell has two kinds of errors – terminating errors and non-terminating errors.</p>
<p>Terminating errors are the errors that can stop command execution cold.  Non-terminating errors provided an additional challenge, as you need to be notified of failed operations and continue with pipeline operations.  To deal with this issue and to provide additional output options, PowerShell employs the concept of streams.  There are three additional streams (other than the primary pipeline stream) available in PowerShell – Verbose, Warning, and Error (and .  We’ll be concerning ourselves with the output from the Error stream.</p>
<p>There are some <a href="http://technet.microsoft.com/en-us/library/dd347675.aspx" target="_blank">automatic variables</a> in the shell that deal with errors as well.  $ErrorActionPreference is a variable that describes how PowerShell will treat non-terminating errors.  $ErrorActionPreference has four options: “Continue” (the default), “SilentlyContinue”, “Inquire”, and “Stop”.  $error is an array of all the errors that have occurred in the current session up to the number specified in $MaximumErrorCount.  $? is a boolean value that is $true if the previous operation succeeded and $false if it did not.</p>
<p>PowerShell does have some built in flexibility to turn non-terminating errors into terminating errors.  Based on our setting of $ErrorActionPreference, PowerShell will treat a non-terminating error differently.  If $ErrorActionPreference is set to &#8216;”Stop”, PowerShell will treat the errors from that scope and all sub scopes (unless explicitly overridden) as terminating errors.  If “Inquire” is chosen as the $ErrorActionPreference, then PowerShell will prompt the user at the console upon each error to ask whether it should continue (treat as non-terminating) or halt (treat as terminating), as well as providing an option to drop into a nested prompt.</p>
<p>$ErrorActionPreference is a global preference that can be set, but the PowerShell runtime also allows more granular control by providing <a href="http://technet.microsoft.com/en-us/library/dd315352.aspx" target="_blank">common parameters</a> for -ErrorAction and -ErrorVariable (shorthand -EA and –EV) which allow you to determine per cmdlet (and in V2 per advanced function) how errors should be handled.  The –ErrorAction parameter takes the same options as $ErrorActionPreference.</p>
<p>So what happens when you encounter an error?</p>
<p>If you hit a terminating error, your script, function, or command will stop.  The error will be logged to the $error variable and written to the error stream.  The $? will be set to false.  You can use the <a href="http://technet.microsoft.com/en-us/library/dd347548.aspx" target="_blank">trap construct</a> (or the <a href="http://technet.microsoft.com/en-us/library/dd315350.aspx" target="_blank">try/catch/finally construct</a> in V2) to deal with terminating errors and we’ll cover that further in the fourth post in this series.</p>
<p>If you hit a non-terminating error, the result will depend on the $ErrorActionPreference in the scope or the –ErrorAction parameter.  If the $ErrorActionPreference or –ErrorAction parameter is set to ‘Continue’, the error will be written to the error stream, added to the $error array, and the $? will be set to $false.  ‘SilentlyContinue’ will not write an error to the Error stream, but $error and $? are updated.  Finally, ‘Inquire’ provides you an option at each error as to whether to treat it as terminating or non-terminating.  If treated as a non-terminating error, the error will be treated like the $ErrorActionPreference or –ErrorAction parameter is ‘Continue’.</p>
<p>Now, I’ve mentioned that some terminating and non-terminating errors both write to the Error stream, so where does that actually get reported back to the user?  If an error has been written to the error stream, it will be written to the output stream (which may surface differently based on the PowerShell host) separately from the pipeline output unless it is redirected.  This means that the exception objects, which are how the errors are represented, are not saved to a variable if you are assigning the pipeline output to a variable.</p>
<p>For example:</p>
<p>$results = Get-WMIObject Win32_Bios –ComputerName localhost, ComputerThatDoesNotExist<br />
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)<br />
At line:1 char:22<br />
+ $results = Get-WmiObject  &lt;&lt;&lt;&lt; Win32_Bios -ComputerName localhost, ComputerThatDoesNotExist</p>
<p>creates this output to the screen, but $results will only hold the results of the successful WMI query.  This is where the –ErrorVariable comes in.  You can specify a variable to hold the exceptions generated by a particular cmdlet (or in V2 advanced function).  Specifying a variable does not remove an item from being written to the error stream and displayed as output.</p>
<p>In a console session, you can redirect the error stream with the redirection operators ( 2&gt; or 2&gt;&gt;) or you can merge the error stream with the output stream (2&gt;&amp;1) and write the output to a file, the pipeline, or assign it to a variable.</p>
<p>Up next:</p>
<ul>
<li>Error Objects</li>
<li>Tracing Errors</li>
<li>Trapping Errors</li>
<li>Reporting Errors</li>
</ul>
<p>Other great <a href="http://support.microsoft.com/kb/968929" target="_blank">PowerShell</a> Error Handling posts:</p>
<ul>
<li><a href="http://huddledmasses.org/trap-exception-in-powershell/">http://huddledmasses.org/trap-exception-in-powershell/</a></li>
<li><a href="http://blogs.msdn.com/powershell/archive/2006/11/03/erroraction-and-errorvariable.aspx">http://blogs.msdn.com/powershell/archive/2006/11/03/erroraction-and-errorvariable.aspx</a></li>
<li><a href="http://tfl09.blogspot.com/2009/01/error-handling-with-powershell.html">http://tfl09.blogspot.com/2009/01/error-handling-with-powershell.html</a></li>
<li><a href="http://weblogs.asp.net/adweigert/archive/2007/10/10/powershell-try-catch-finally-comes-to-life.aspx">http://weblogs.asp.net/adweigert/archive/2007/10/10/powershell-try-catch-finally-comes-to-life.aspx</a></li>
<li><a href="http://powershell.com/cs/blogs/ebook/archive/2009/03/30/chapter-11-finding-and-avoiding-errors.aspx">http://powershell.com/cs/blogs/ebook/archive/2009/03/30/chapter-11-finding-and-avoiding-errors.aspx</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/07/deep-dive-error-handling-error-types-part-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Exploring the .NET Framework with PowerShell – Namespaces (Part 5)</title>
		<link>http://blog.usepowershell.com/2009/04/exploring-the-net-framework-with-powershell-namespaces-part-5/</link>
		<comments>http://blog.usepowershell.com/2009/04/exploring-the-net-framework-with-powershell-namespaces-part-5/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 12:00:00 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Base Class Libraries]]></category>
		<category><![CDATA[Introduction]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/?p=115</guid>
		<description><![CDATA[One concept that is central to the understanding of the layout of classes in the Base Class Libraries is the Namespace.  Namespaces are a method of providing context for classes (and other constructs like Enums and Structs), allowing developers to group related classes and not worry about name collisions with development in other areas. In [...]]]></description>
			<content:encoded><![CDATA[<p>One concept that is central to the understanding of the layout of classes in the <a href="http://blog.usepowershell.com/2009/02/exploring-the-net-framework-with-powershell-terminology-part-1/" target="_blank">Base Class Libraries</a> is the Namespace.  Namespaces are a method of providing context for classes (and other constructs like Enums and Structs), allowing developers to group related classes and not worry about name collisions with development in other areas.</p>
<p>In the Base Class Libraries, the root namespace is the System namespace.  The System namespace defines a large amount of the base types, like <a href="http://msdn.microsoft.com/en-us/library/system.object.aspx" target="_blank">Object</a> (which all classes derive from), <a href="http://msdn.microsoft.com/en-us/library/system.string.aspx" target="_blank">String</a>, <a href="http://msdn.microsoft.com/en-us/library/system.datetime.aspx" target="_blank">DateTime</a>, <a href="http://msdn.microsoft.com/en-us/library/system.boolean.aspx" target="_blank">Boolean</a>, and <a href="http://msdn.microsoft.com/en-us/library/system.aspx" target="_blank">numerous others</a>.  When you specify types, <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx" target="_blank">PowerShell</a> can infer the “System” part.</p>
<p>Example:</p>
<blockquote><p>PS C:\scripts\PowerShell&gt; $random = <a href="http://technet.microsoft.com/en-us/library/d203c4ab-2d57-4103-a04e-d7869d06931e" target="_blank">New-Object</a> –TypeName <a href="http://msdn.microsoft.com/en-us/library/system.random.aspx" target="_blank">Random</a></p></blockquote>
<p>is the same as</p>
<blockquote><p>PS C:\scripts\PowerShell&gt; $random = <a href="http://technet.microsoft.com/en-us/library/d203c4ab-2d57-4103-a04e-d7869d06931e" target="_blank">New-Object</a> –TypeName <a href="http://msdn.microsoft.com/en-us/library/system.random.aspx" target="_blank">System.Random</a></p></blockquote>
<p>Namespaces are hierarchical, except in naming convention. The representation of a file in PowerShell is a great example.  A file object is represented in PowerShell as a <a href="http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx" target="_blank">System.IO.FileInfo</a> object.  The <a href="http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx" target="_blank">FileInfo</a> class lives in the <a href="http://msdn.microsoft.com/en-us/library/system.io.aspx" target="_blank">System.IO</a> namespace, which is a separate namespace than the <a href="http://msdn.microsoft.com/en-us/library/system.aspx" target="_blank">System</a> namespace.  There is a hierarchical impression implied, but that is solely through naming convention.</p>
<p>The CLR (Common Language Runtime) does not care about namespaces, only the fully qualified name of the class, so why use namespaces at all?</p>
<p>Namespaces are a convenience for developers, as languages like C# and VB.NET have language constructs that allow the developer to not have to use the fully qualified type name every time that is needed to be referenced.  C# provides the “<a href="http://msdn.microsoft.com/en-us/library/aa664766(VS.71).aspx" target="_blank">using</a>” syntax and VB.NET has the “<a href="http://msdn.microsoft.com/en-us/library/zt9tafza(VS.80).aspx" target="_blank">imports</a>” syntax.</p>
<p>How does this apply to PowerShell?</p>
<p>PowerShell does not currently have a “using” or “imports” syntax or language feature, but there are a couple of workarounds.  You can use variables holding the namespace and string concatenation to save some typing</p>
<blockquote><p>PS C:\scripts\PowerShell&gt; $netinfo = &#8216;System.Net.NetworkInformation&#8217;<br />
PS C:\scripts\PowerShell&gt; $ping = New-Object &#8220;$netinfo.ping&#8221;<br />
PS C:\scripts\PowerShell&gt; $ping.gettype()<br />
IsPublic IsSerial Name           BaseType<br />
&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;-                &#8212;&#8212;&#8211;<br />
True     False    Ping            System.ComponentModel.Component</p></blockquote>
<p>This doesn’t work with type accelerators though (at least in V1)..  V2 CTP3 has some other options and <a href="http://www.nivot.org/2008/12/25/ListOfTypeAcceleratorsForPowerShellCTP3.aspx" target="_blank">Oisin Grehan has a great discussion on how to find the type accelerators and add your own.</a></p>
<p>Another option would be to create a function to wrap the creation of objects for particular namespaces.</p>
<p>Use-Namespace is a function I came up with to create a function to wrap New-Object for specific namespaces.</p>
<p>The useage of Use-Namespace is<br />
PS C:\scripts\PowerShell&gt;Use-Namespace System.IO, System.Net<br />
PS C:\scripts\PowerShell&gt;$memorystream = New-System.IOObject MemoryStream<br />
PS C:\scripts\PowerShell&gt;$memorystream.GetType()<br />
PS C:\scripts\PowerShell&gt; $memorystream.gettype()<br />
IsPublic IsSerial  Name                    BaseType<br />
&#8212;&#8212;&#8211; &#8212;&#8212;&#8211;   &#8212;-                         &#8212;&#8212;&#8211;<br />
True     True     MemoryStream      System.IO.Stream</p>
<p>And the function is here..</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> Use<span style="color: pink;">-</span>Namespace<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #0000FF;">param</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Namespace</span><span style="color: #000000;">&#41;</span>
  BEGIN
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Namespace</span>.length <span style="color: #FF0000;">-gt</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      <span style="color: #800080;">$Namespace</span> <span style="color: pink;">|</span> Use<span style="color: pink;">-</span>Namespace
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
  PROCESS
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span> <span style="color: #FF0000;">-ne</span> <span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      <span style="color: #800080;">$NS</span> <span style="color: pink;">=</span> <span style="color: #000080;">$_</span>
      <span style="color: #800080;">$NSObject</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$NS&quot;</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;Object&quot;</span>
      <span style="color: #800080;">$function</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #800000;">&quot;
param (<span style="color: #008080; font-weight: bold;">`$</span>Class, [string[]]<span style="color: #008080; font-weight: bold;">`$</span>ArgumentList)
if (<span style="color: #008080; font-weight: bold;">`$</span>ArgumentList.length -gt 0)
{
return New-Object -TypeName &quot;</span><span style="color: #800080;">$NS</span>.`$Class<span style="color: #800000;">&quot; -ArgumentList <span style="color: #008080; font-weight: bold;">`$</span>ArgumentList
}
else
{
return New-Object -TypeName &quot;</span><span style="color: #800080;">$NS</span>.`$Class<span style="color: #800000;">&quot;
}
&quot;</span><span style="color: pink;">@</span>
    <span style="color: #008080; font-weight: bold;">Set-Item</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;function:global:New-$NSObject&quot;</span> <span style="color: #008080; font-style: italic;">-Value</span> <span style="color: #800080;">$function</span> <span style="color: #008080; font-style: italic;">-force</span>
    <span style="color: #008080; font-weight: bold;">Write-Debug</span> <span style="color: #800000;">&quot;Created function:global:New-$NSObject &quot;</span>
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>PSMDTAG:FAQ CLR<br />
PSMDTAG:FAQ Namespace<br />
PSMDTAG:FAQ .NET Framework</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/04/exploring-the-net-framework-with-powershell-namespaces-part-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tip: Sneaky Storage &#8211; What&#8217;s in your AppDomain?</title>
		<link>http://blog.usepowershell.com/2009/03/tip-sneaky-storage-whats-in-your-appdomain/</link>
		<comments>http://blog.usepowershell.com/2009/03/tip-sneaky-storage-whats-in-your-appdomain/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 14:49:54 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Base Class Libraries]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/2009/03/tip-sneaky-storage-whats-in-your-appdomain/</guid>
		<description><![CDATA[Since PowerShell is built on .NET, there is a AppDomain (I’ll go into more detail in a later post) which has a lot of information about the .NET environment (what assemblies are loaded, etc..). One feature that the AppDomain has is to store globally accessible name/value pairs.&#160; Normally, variables in PowerShell should handle most of [...]]]></description>
			<content:encoded><![CDATA[<p>Since PowerShell is built on .NET, there is a <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.aspx" target="_blank">AppDomain</a> (I’ll go into more detail in a later post) which has a lot of information about the .NET environment (what assemblies are loaded, etc..).</p>
<p>One feature that the AppDomain has is to store globally accessible name/value pairs.&#160; Normally, variables in PowerShell should handle most of your “in-process” storage needs, but for the times that they don’t, you have your AppDomain.</p>
<p>To access the AppDomain object, you can use the static property <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.currentdomain.aspx" target="_blank">CurrentDomain</a> on the <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.aspx" target="_blank">System.AppDomain</a> class. </p>
<p><strong>(NOTE: Though I usually refer use the fully qualified namespace, PowerShell does allow you to skip the “System” portion of the namespace.&#160; I’m using the full namespace for clarity on the blog, but when typing on the command line, it is easier to skip that.)</strong></p>
<p>PS C:\&gt; [system.appdomain]::CurrentDomain</p>
<p>To save a name/value pair to your AppDomain, you can use the <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.setdata.aspx" target="_blank">SetData</a> method.</p>
<p>PS C:\&gt; [system.appdomain]::CurrentDomain.SetData(&#8216;ThisIsMyNameOrKey&#8217;, (&#8216;This&#8217;,'Can&#8217;,'Be&#8217;,'Any&#8217;,'Object&#8217;,'Or&#8217;,'Collection&#8217;) )</p>
<p>Any object or collection can be saved in the AppDomain, and you are not limited to just one.</p>
<p>Accessing those values is just as easy, using the <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.getdata.aspx" target="_blank">GetData</a> method.</p>
<p>PS C:\&gt; [system.appdomain]::CurrentDomain.GetData(&#8216;ThisIsMyNameOrKey&#8217;)   <br />This    <br />Can    <br />Be    <br />Any    <br />Object    <br />Or    <br />Collection</p>
<p>And there we have our collection back.</p>
<p><a href="http://blogs.msdn.com/powershell/archive/2009/03/01/powershell-folksonomy.aspx">PSMDTAG</a>:.Net AppDomain</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/03/tip-sneaky-storage-whats-in-your-appdomain/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Exploring the .NET Framework with PowerShell – Static Members (Part 4)</title>
		<link>http://blog.usepowershell.com/2009/03/exploring-the-net-framework-with-powershell-static-members-part-4/</link>
		<comments>http://blog.usepowershell.com/2009/03/exploring-the-net-framework-with-powershell-static-members-part-4/#comments</comments>
		<pubDate>Fri, 20 Mar 2009 13:02:54 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Base Class Libraries]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Introduction]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/2009/03/exploring-the-net-framework-with-powershell-static-members-part-4/</guid>
		<description><![CDATA[In our Exploring the .NET Framework series, we’ve covered some terminology, creating instances of objects and calling methods.  In today’s installment, we are going to look at using static members &#8211; methods and properties.  Static methods are methods that a class (or type) make available without needing to create an instance of the class.  Similarly, [...]]]></description>
			<content:encoded><![CDATA[<p>In our <a href="http://blog.usepowershell.com/category/net-framework/introduction/" target="_blank">Exploring the .NET Framework series</a>, we’ve covered some terminology, creating instances of objects and calling methods.  In today’s installment, we are going to look at using static members &#8211; methods and properties.  Static methods are methods that a class (or type) make available without needing to create an instance of the class.  Similarly, static properties are properties that you can access without needing an instance of the class.</p>
<p>Many classes provide static members supply functionality where it doesn’t make sense to need an object. </p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/system.math.aspx" target="_blank">System.Math</a> class contains a couple of static properties, PI and E. </p>
<p>PS C:\scripts\PowerShell&gt; [math]::E<br />
2.71828182845905<br />
PS C:\scripts\PowerShell&gt; [math]::PI<br />
3.14159265358979</p>
<p>Constant values are not the only type of property that you’ll find.  The <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.aspx" target="_blank">System.AppDomain</a> class has a static property, <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.currentdomain.aspx" target="_blank">CurrentDomain</a>, which is an AppDomain instance referring to the current application domain.</p>
<p>PS C:\scripts\PowerShell&gt; [AppDomain]::CurrentDomain.gettype()</p>
<p>IsPublic  IsSerial Name         BaseType<br />
&#8212;&#8212;&#8211;    &#8212;&#8212;&#8211;    &#8212;-            &#8212;&#8212;&#8211;<br />
True     False    AppDomain System.MarshalByRefObject</p>
<p>System.Math also contains a number of static methods that provide a great deal of basic mathematical function.</p>
<p>PS C:\scripts\PowerShell&gt;[Math] | get-member –static –membertype method | select name</p>
<p>Name<br />
&#8212;-<br />
Abs<br />
Acos<br />
Asin<br />
Atan<br />
Atan2<br />
BigMul<br />
Ceiling<br />
Cos<br />
Cosh<br />
DivRem<br />
Equals<br />
Exp<br />
Floor<br />
IEEERemainder<br />
Log<br />
Log10<br />
Max<br />
Min<br />
Pow<br />
ReferenceEquals<br />
Round<br />
Sign<br />
Sin<br />
Sinh<br />
Sqrt<br />
Tan<br />
Tanh<br />
Truncate</p>
<p>I wrote a little helper function to get the static method definitions (<a href="http://poshcode.org/968" target="_blank">Get-StaticMethodDefinition</a>), which you can grab from <a href="http://poshcode.org/" target="_blank">PoshCode.org</a>.</p>
<p>(V2 CTP3 &#8211; We can use the trick of using the method name without parenthesis following to get additional information on each of the methods that we are interested in.)</p>
<p>One type of static method that I would like to highlight is the factory method.  A factory method is a common way of controlling object creation.  At its most basic level, a factory pattern is a method that creates objects of a specified type. </p>
<p>An example of this is the <a href="http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx" target="_blank">System.Net.WebRequest’s</a> Create method.</p>
<p>PS C:\scripts\PowerShell&gt; $web = [net.webrequest]::create(&#8216;<a href="http://www.google.com">http://www.google.com</a>&#8216;)<br />
PS C:\scripts\PowerShell&gt; $web.gettype()</p>
<p>IsPublic IsSerial Name                                     BaseType<br />
&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;-                                     &#8212;&#8212;&#8211;<br />
True     True     HttpWebRequest                           System.Net.WebReq&#8230;</p>
<p>The factory method Create used the argument (‘http://www.google.com’) to determine the type of <a href="http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx" target="_blank">WebRequest</a> object to create.  In this case, it created an <a href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx" target="_blank">HttpWebRequest</a> object, which is a more specialized version of the WebRequest for HTTP requests. </p>
<p>Let’s see how it would respond if we passed the Create method a URI with FTP.</p>
<p>PS C:\scripts\PowerShell&gt; $web = [net.webrequest]::create(&#8216;<a href="ftp://ftp.google.com'">ftp://ftp.google.com&#8217;</a>)<br />
PS C:\scripts\PowerShell&gt; $web.gettype()</p>
<p>IsPublic IsSerial Name                                     BaseType<br />
&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;-                                     &#8212;&#8212;&#8211;<br />
True     False    FtpWebRequest                            System.Net.WebReq&#8230;</p>
<p>As we can see, the WebRequest class created an <a href="http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx" target="_blank">FtpWebRequest</a> object.</p>
<p>Factory methods provide developers a way to more flexibly support different operations by allowing the static method to determine which object to return.</p>
<p><strong>UPDATE:  <a href="http://huddledmasses.org/" target="_self">Joel &#8220;Jaykul&#8221; Bennet</a> added a <a href="http://poshcode.org/969" target="_blank">V2 advanced function to convert static methods to functions</a> to PoShCode.org  and pointed out <a href="http://www.nivot.org/2007/08/13/CreatingFunctionsFromANETClasssStaticMethods.aspx" target="_blank">Oisin Grehan&#8217;s blog post about creating functions from static methods (V1 compatible)</a>.</strong></p>
<p>I’ve been really happy to receive some of the feedback (positive and constructive criticism) on this series.  There is a lot of the <a href="http://msdn.microsoft.com/en-us/netframework/default.aspx" target="_blank">.NET Framework</a> to cover, and I’m open to suggestions as to what you would like to know.  Please post a comment on the blog or email me at Steve at UsePowerShell.Com.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/03/exploring-the-net-framework-with-powershell-static-members-part-4/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How To Send E-Mail From PowerShell</title>
		<link>http://blog.usepowershell.com/2009/03/how-to-send-e-mail-from-powershell/</link>
		<comments>http://blog.usepowershell.com/2009/03/how-to-send-e-mail-from-powershell/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 15:30:00 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Base Class Libraries]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Version 1]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/?p=60</guid>
		<description><![CDATA[In my Exploring the .NET Framework series, I introduced the System.Net.Mail.MailMessage class.&#160; Being able to create a MailMessage object is all well and good, but if you can’t send it, it’s really not helpful. To send an email from PowerShell using the .NET Framework, you can use the System.Net.Mail.SMTPClient class. First, you need to create [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://blog.usepowershell.com/category/net-framework/introduction/" target="_blank">Exploring the .NET Framework series</a>, I introduced the <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx#" target="_blank">System.Net.Mail.MailMessage</a> class.&#160; Being able to create a MailMessage object is all well and good, but if you can’t send it, it’s really not helpful.</p>
<p>To send an email from <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx" target="_blank">PowerShell</a> using the .NET Framework, you can use the <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx#" target="_blank">System.Net.Mail.SMTPClient</a> class.</p>
<p>First, you need to create your MailMessage</p>
<p>PS C:\scripts\PowerShell&gt; $message = <a href="http://technet.microsoft.com/en-us/library/d203c4ab-2d57-4103-a04e-d7869d06931e" target="_blank">New-Object</a> System.Net.Mail.MailMessage –ArgumentList <a href="mailto:steve@usepowershell.com">steve@usepowershell.com</a>, <a href="mailto:nobody@adomain.com">nobody@adomain.com</a>, ‘This is a test’, ‘I’m going to send an email via PowerShell’</p>
<p>If you need to add an attachment, you can add one with the <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.attachment.aspx" target="_blank">System.Net.Mail.Attachment</a> class.</p>
<p>To create the attachment, we’ll create an use an overload of the Attachment class that allows us to specify a filename and a MIME type (the MIME types are available in several enumerations:&#160; <a href="http://msdn.microsoft.com/en-us/library/system.net.mime.mediatypenames.application.aspx" target="_blank">System.Net.Mime.MediaTypeNames.Application</a>, <a href="http://msdn.microsoft.com/en-us/library/system.net.mime.mediatypenames.image.aspx" target="_blank">System.Net.Mime.MediaTypeNames.Image</a>, and <a href="http://msdn.microsoft.com/en-us/library/system.net.mime.mediatypenames.text.aspx" target="_blank">System.Net.Mime.MediaTypeNames.Text</a> – I’ve had a bit of trouble accessing those enumerations via PowerShell, but you can specify the name of the enumeration value in a string).</p>
<p>PS C:\scripts\PowerShell&gt; $attachment = <a href="http://technet.microsoft.com/en-us/library/d203c4ab-2d57-4103-a04e-d7869d06931e" target="_blank">New-Object</a> System.Net.Mail.Attachment –ArgumentList &#8216;c:\docs\test.xls’, ‘Application/Octet’</p>
<p>PS C:\scripts\PowerShell&gt; $message.Attachments.Add($attachment)</p>
<p>After we have our email and optional attachment, we’ll create the SMTP client and have it configured to use the SMTP server of our choice.</p>
<p>The constructor of the SMTPClient class that we will use is</p>
<p>SmtpClient(    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; String host,     <br />)</p>
<p>PS C:\scripts\PowerShell&gt; $smtp = New-Object System.Net.Mail.SMTPClient –ArgumentList 10.10.10.15</p>
<p>All that is left is sending our email..</p>
<p>PS C:\scripts\PowerShell&gt; $smtp.Send($message)</p>
<p>Have fun!</p>
<p>NOTE:</p>
<p>There is a shortcut to creating the MailMessage object like we did.. there is an overload of Send where you can pass in four strings (from, to, subject, and message).&#160; The SMTPClient handles the creation of the MailMessage in the background.</p>
<p>A faster method would be</p>
<p>PS C:\scripts\PowerShell&gt; $smtp = New-Object System.Net.Mail.SMTPClient –ArgumentList 10.10.10.15</p>
<p>PS C:\scripts\PowerShell&gt; $smtp.Send(‘<a href="mailto:steve@usepowershell.com">steve@usepowershell.com</a>’, ‘<a href="mailto:nobody@adomain.com">nobody@adomain.com</a>’, ‘This is a test’, ‘I’m going to send an email via PowerShell’)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/03/how-to-send-e-mail-from-powershell/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Exploring the .NET Framework with PowerShell &#8211; Constructors (Part 3)</title>
		<link>http://blog.usepowershell.com/2009/03/exploring-the-net-framework-with-powershell-constructors-part-3/</link>
		<comments>http://blog.usepowershell.com/2009/03/exploring-the-net-framework-with-powershell-constructors-part-3/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 13:30:00 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Base Class Libraries]]></category>
		<category><![CDATA[Introduction]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Version 1]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/?p=9</guid>
		<description><![CDATA[In part 2(a &#38; b) of this series, we talked about methods and looked at ways to view their overloads, or ways to call them.&#160; We also looked at the objects returned from a method call.&#160; In this post, we are going to explore a special kind of method called the constructor. A constructor is [...]]]></description>
			<content:encoded><![CDATA[<p>In part 2(<a href="http://blog.usepowershell.com/2009/02/exploring-the-net-framework-with-powershell-calling-a-method-part-2a/" target="_blank">a</a> &amp; <a href="http://blog.usepowershell.com/2009/02/exploring-the-net-framework-with-powershell-calling-a-method-part-2b/" target="_blank">b</a>) of this series, we talked about methods and looked at ways to view their overloads, or ways to call them.&#160; We also looked at the objects returned from a method call.&#160; In this post, we are going to explore a special kind of method called the constructor.</p>
<p>A constructor is a method whose job is to create the object that you want to work with.&#160; When I created the Ping object</p>
<p>PS C:\scripts\PowerShell&gt; $ping = New-Object System.Net.NetworkInformation.Ping</p>
<p>the <a href="http://technet.microsoft.com/en-us/library/d203c4ab-2d57-4103-a04e-d7869d06931e" target="_blank">New-Object</a> cmdlet calls the <a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.ping.aspx" target="_blank">constructor for Ping</a>.</p>
<p>Like other methods, constructors can require parameters and can have multiple overloads.&#160; </p>
<p>Let’s look at <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx#" target="_blank">System.Net.Mail.MailMessage</a>, which is a object that represents an email message.</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.mailmessage.aspx" target="_blank">MailMessage constructor</a> contains several overloads.&#160; The first method listed is MailMessage(), which is the default constructor.&#160; This method does not require any arguments and returns an empty MailMessage object.&#160; In <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx" target="_blank">PowerShell</a>, we would call this method via <a href="http://technet.microsoft.com/en-us/library/d203c4ab-2d57-4103-a04e-d7869d06931e" target="_blank">New-Object</a>, just like with Ping class.</p>
<p>MailMessage()</p>
<p> PS C:\scripts\PowerShell&gt; $message = New-Object System.Net.Mail.MailMessage</p>
<p>The first overload requires two <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx" target="_blank">MailAddress</a> objects, the first of which is the “from” address and the second is the “to” address.&#160; </p>
<p>MailMessage(   <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; MailAddress from,    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; MailAddress to,    <br />)</p>
<p>Creating an object in PowerShell from a constructor that requires parameters can be done in two ways.&#160; The first is to use the method call notation.</p>
<p> PS C:\scripts\PowerShell&gt; $message = New-Object System.Net.Mail.MailMessage($MailFrom, $MailTo)</p>
<p>The second is to use the ArgumentList parameter.</p>
<p> PS C:\scripts\PowerShell&gt; $message = New-Object System.Net.Mail.MailMessage –ArgumentList $MailFrom, $MailTo</p>
<p>If you happen to have <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx" target="_blank">MailAddress</a> objects floating around, this might work for you.&#160; A more common scenario would be to use the next overload, which takes two strings.</p>
<p>MailMessage(   <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; String from,    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; String to,    <br />)</p>
<p>This overload requires two strings, one for the “to” and one for the “from” address.&#160; </p>
<p>The final overload for MailMessage is</p>
<p>MailMessage(   <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; String from,    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; String to,    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; String subject,    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; String body,    <br />)</p>
<p>This overload allows you to pass in four strings and get back basic MailMessage object that is ready to be sent.</p>
<p>Wouldn’t it be great to have a function, where we could provide a type name and get a listing of all the constructors, right from the command line (not needing to go to the MSDN documentation every time).&#160; Jeffrey Snover has beaten me to the punch and provided a function to find the constructors, appropriately called <a href="http://blogs.msdn.com/powershell/archive/2008/09/01/get-constructor-fun.aspx" target="_blank">Get-Constructor</a>.&#160; Add that function to the your toolbox, and exploring the .NET Framework becomes a lot easier.</p>
<p>Next up, we’ll look at static methods.&#160; Static methods are methods that can be called without having to create a instance of an object.</p>
<p>One particular type of static method that we’ll dig in to is the factory method, which is another way to create instances of objects.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/03/exploring-the-net-framework-with-powershell-constructors-part-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Exploring the .NET Framework with PowerShell &#8211; Calling a Method (Part 2b)</title>
		<link>http://blog.usepowershell.com/2009/02/exploring-the-net-framework-with-powershell-calling-a-method-part-2b/</link>
		<comments>http://blog.usepowershell.com/2009/02/exploring-the-net-framework-with-powershell-calling-a-method-part-2b/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 14:00:00 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Base Class Libraries]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Introduction]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/?p=42</guid>
		<description><![CDATA[Continuing from where I left off last week, we were created an instance of the System.Net.NetworkInformation.Ping class and looked at the different ways (overloads) that we can call the Send method.&#160; Now that we’ve seen how we can create Ping objects, let’s take advantage of options for creating some custom ping packets and examine the [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing from where <a href="http://blog.usepowershell.com/2009/02/exploring-the-net-framework-with-powershell-calling-a-method-part-2a/" target="_blank">I left off last week</a>, we were created an instance of the <a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx" target="_blank">System.Net.NetworkInformation.Ping</a> class and looked at the different ways (overloads) that we can call the Send method.&#160; Now that we’ve seen how we can create Ping objects, let’s take advantage of options for creating some custom ping packets and examine the return object.</p>
<p>PS C:\&gt; $encoder = new-object System.Text.ASCIIEncoding</p>
<p>PS C:\&gt; $bytes = $encoder.Getbytes(&#8216;Hello From Steve&#8217;)</p>
<p>PS C:\&gt; $response = $ping.Send(&#8216;google.com&#8217;, 100, $bytes)</p>
<p>Let’s take a look at the response we get back.</p>
<p>PS C:\&gt; $response | Get-Member</p>
<p>&#160;&#160; TypeName: System.Net.NetworkInformation.PingReply</p>
<p>Name&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; MemberType&#160;&#160;&#160;&#160; Definition    <br />&#8212;-&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#8212;&#8212;&#8212;-&#160;&#160;&#160;&#160; &#8212;&#8212;&#8212;-     <br />Equals&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Method&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.Boolean Equals(Object obj)     <br />GetHashCode&#160;&#160;&#160;&#160;&#160;&#160;&#160; Method&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.Int32 GetHashCode()     <br />GetType&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Method&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.Type GetType()     <br />get_Address&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Method&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.Net.IPAddress get_Address()     <br />get_Buffer&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Method&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.Byte[] get_Buffer()     <br />get_Options&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Method&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.Net.NetworkInformation.PingOptions&#8230;     <br />get_RoundtripTime Method&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.Int64 get_RoundtripTime()     <br />get_Status&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Method&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.Net.NetworkInformation.IPStatus ge&#8230;     <br />ToString&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Method&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.String ToString()     <br />Address&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Property&#160;&#160;&#160;&#160;&#160;&#160; System.Net.IPAddress Address {get;}     <br />Buffer&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Property&#160;&#160;&#160;&#160;&#160;&#160; System.Byte[] Buffer {get;}     <br />Options&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Property&#160;&#160;&#160;&#160;&#160;&#160; System.Net.NetworkInformation.PingOptions&#8230;     <br />RoundtripTime&#160;&#160;&#160;&#160;&#160;&#160; Property&#160;&#160;&#160;&#160;&#160;&#160; System.Int64 RoundtripTime {get;}     <br />Status&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Property&#160;&#160;&#160;&#160;&#160;&#160; System.Net.NetworkInformation.IPStatus St&#8230;     <br />BufferSize&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ScriptProperty System.Object BufferSize {get=$this.Buffe&#8230;     <br />OptionsString&#160;&#160;&#160;&#160;&#160;&#160;&#160; ScriptProperty System.Object OptionsString {get=&#8217;TTL={0}&#8230;</p>
<p>Get-Member shows us that this is a <a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.pingreply.aspx" target="_blank">PingReply</a> object in the <a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.aspx" target="_blank">System.Net.NetworkInformation</a> namespace.</p>
<p>Looking at the <a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.pingreply.aspx" target="_blank">PingReply</a> object that was returned, we have several methods that look strangely similar to the properties, but with a “get_” prefixed to it.&#160; When you see a method that matches a Property, but has “get_” or “set_”, that is a method exposed by the <a href="http://msdn.microsoft.com/en-us/netframework/default.aspx" target="_blank">.NET Framework</a> that provides the functionality for the properties.&#160; When you access the Buffer property, you are actually calling the get_Buffer method.&#160; Properties are, for the most part, easier to work with and you can effectively ignore those methods.&#160; Properties also contain a clue in their description, if you look at the end of the Buffer property’s description, you see “{get;}” which indicates that there is a way to retrieve that property.&#160; Properties can also show “{set;}” which indicates that you can update that property.</p>
<p>In <a href="http://www.microsoft.com/DOWNLOADS/details.aspx?FamilyID=c913aeab-d7b4-4bb1-a958-ee6d7fe307bc&amp;displaylang=en" target="_blank">PowerShell V2 CTP3</a> those “getters” and “setters” (as those methods are commonly called) are hidden by default.&#160; If you are running CTP3 and want to see the “getters” and “setters”, you can call Get-Member with the Force parameter.</p>
<p>PS C:\&gt; $response | Get-Member –Force</p>
<p>Back to the properties of our return object.&#160; The properties names are pretty self-explanatory, but to confirm we got back the “special” packet we crafted, let’s take a look at the Buffer property and convert that back to text.</p>
<p>PS C:\&gt; $encoder.GetString($response.buffer)    <br />Hello From Steve</p>
<p>Now for the meat of the response, the RoundtripTime and Status.&#160; I commonly use the Roundtrip time to watch for latency on my network, and since it is exposed so nicely, it is easy to log.</p>
<p>PS C:\&gt; $response.RoundtripTime    <br />74</p>
<p>And of course, we are interested in the Status.</p>
<p>PS C:\&gt; $response.Status    <br />Success</p>
<p>Now, we can see from the Get-Member return above, that Status is not just a string that says, “Success”, it is a <a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipstatus.aspx" target="_blank">IPStatus</a> enumeration (more on enumerations coming up, but long story short, there are a number of predefined values that an <a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipstatus.aspx" target="_blank">IPStatus</a> object can hold)(also found in the <a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.aspx" target="_blank">System.Net.NetworkInformation</a> namespace).&#160; </p>
<p>I’m normally checking the IPStatus for a Success value like</p>
<p>PS C:\&gt; $success = [System.Net.NetworkInformation.IPStatus]::Success</p>
<p>PS C:\&gt; $response.status -eq $success    <br />True</p>
<p>I could compare the string results, but then I’m stepping back to parsing text results.&#160; By using the <a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipstatus.aspx" target="_blank">IPStatus</a> enumeration values, I can get be reasonably sure that my value are correct and I don’t have any text matching issues.</p>
<p>In the next post in this series, we’ll take a look at a special type of method, the constructor.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/02/exploring-the-net-framework-with-powershell-calling-a-method-part-2b/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Exploring the .NET Framework with PowerShell &#8211; Calling a Method (Part 2a)</title>
		<link>http://blog.usepowershell.com/2009/02/exploring-the-net-framework-with-powershell-calling-a-method-part-2a/</link>
		<comments>http://blog.usepowershell.com/2009/02/exploring-the-net-framework-with-powershell-calling-a-method-part-2a/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 04:06:38 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Base Class Libraries]]></category>
		<category><![CDATA[Introduction]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/?p=38</guid>
		<description><![CDATA[Last week, I defined a number of terms that we’ll be exposed to as we delve into how and why PowerShell is an object oriented shell and how to use it to explore .NET Framework which it is built upon. Now, let’s take a look at how some of these terms surface themselves in PowerShell. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.usepowershell.com/2009/02/exploring-the-net-framework-with-powershell-terminology-part-1/" target="_blank">Last week, I defined a number of terms</a> that we’ll be exposed to as we delve into how and why <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx" target="_blank">PowerShell</a> is an object oriented shell and how to use it to explore .NET Framework which it is built upon.</p>
<p>Now, let’s take a look at how some of these terms surface themselves in PowerShell.</p>
<p>One of the most common tasks you might encounter is needing to ping a computer.  There is ping.exe, which works and has been a common guest in my console sessions for years.  You can also get ping information via WMI which provides you with some interesting information.</p>
<p>PS C:\scripts\PowerShell&gt; Get-WmiObject Win32_PingStatus -filter &#8220;Address=&#8217;google.com&#8217;&#8221;</p>
<p>My preference is to use a class from the .NET Framework called <a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx" target="_blank">Ping</a>.  <a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx" target="_blank">Ping</a> lives in the <a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.aspx" target="_blank">System.Net.NetworkInformation</a> namespace.</p>
<p>To access this in PowerShell, I’ll create an instance of the class and refer to it with a variable, $ping.</p>
<p>PS C:\scripts\PowerShell&gt; $ping = New-Object System.Net.NetworkInformation.Ping</p>
<p>By typing the method name without the parenthesis immediately after it, PowerShell will give us information about the method.</p>
<p>PS C:\scripts\PowerShell&gt; $ping.send</p>
<p>MemberType          : Method<br />
OverloadDefinitions : {System.Net.NetworkInformation.PingReply Send(String ho<br />
                      stNameOrAddress), System.Net.NetworkInformation.PingRep<br />
                      ly Send(String hostNameOrAddress, Int32 timeout), Syste<br />
                      m.Net.NetworkInformation.PingReply Send(IPAddress addre<br />
                      ss), System.Net.NetworkInformation.PingReply Send(IPAdd<br />
                      ress address, Int32 timeout)&#8230;}<br />
TypeNameOfValue     : System.Management.Automation.PSMethod<br />
Value               : System.Net.NetworkInformation.PingReply Send(String hos<br />
                      tNameOrAddress), System.Net.NetworkInformation.PingRepl<br />
                      y Send(String hostNameOrAddress, Int32 timeout), System<br />
                      .Net.NetworkInformation.PingReply Send(IPAddress addres<br />
                      s), System.Net.NetworkInformation.PingReply Send(IPAddr<br />
                      ess address, Int32 timeout), System.Net.NetworkInformat<br />
                      ion.PingReply Send(String hostNameOrAddress, Int32 time<br />
                      out, Byte[] buffer), System.Net.NetworkInformation.Ping<br />
                      Reply Send(IPAddress address, Int32 timeout, Byte[] buf<br />
                      fer), System.Net.NetworkInformation.PingReply Send(Stri<br />
                      ng hostNameOrAddress, Int32 timeout, Byte[] buffer, Pin<br />
                      gOptions options), System.Net.NetworkInformation.PingRe<br />
                      ply Send(IPAddress address, Int32 timeout, Byte[] buffe<br />
                      r, PingOptions options)<br />
Name                : Send<br />
IsInstance          : True</p>
<p>What I am most interested in is the Overload Definitions, which is the explanation of how I can use this method.</p>
<p>PS C:\scripts\PowerShell&gt; $ping.send.overloaddefinitions</p>
<p>System.Net.NetworkInformation.PingReply Send(String hostNameOrAddress)<br />
System.Net.NetworkInformation.PingReply Send(String hostNameOrAddress, Int32 timeout)<br />
System.Net.NetworkInformation.PingReply Send(IPAddress address)<br />
System.Net.NetworkInformation.PingReply Send(IPAddress address, Int32 timeout)<br />
System.Net.NetworkInformation.PingReply Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer)<br />
System.Net.NetworkInformation.PingReply Send(IPAddress address, Int32 timeout, Byte[] buffer)<br />
System.Net.NetworkInformation.PingReply Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)<br />
System.Net.NetworkInformation.PingReply Send(IPAddress address, Int32 timeout, Byte[] buffer, PingOptions options)</p>
<p>As we can see from the above output $ping.<a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.send.aspx" target="_blank">Send</a> has several different ways that we can call it and it also shows the object that we will get in return, an object of the type <a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.pingreply.aspx" target="_blank">System.Net.NetworkInformation.PingReply</a>.</p>
<p>The first overload for $ping.<a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.send.aspx" target="_blank">Send</a> is to supply it an object of the type <a href="http://msdn.microsoft.com/en-us/library/system.string.aspx" target="_blank">String</a> (remember, in PowerShell, even text is an object) which represents the hostname or IP address of the host we wish to ping.</p>
<p>PS C:\scripts\PowerShell&gt; $ping.send(‘google.com’)</p>
<p>Status        : Success<br />
Address       : 74.125.45.100<br />
RoundtripTime : 29 ms<br />
BufferSize    : 32<br />
Options       : TTL=243, DontFragment=False</p>
<p>Here, we called $ping.<a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.send.aspx" target="_blank">Send</a> with a argument of the String ‘google.com’ and received back an object of the type PingReply.  We can see the textual representation of it’s properties.  In my next post, we’ll dig into the return object a bit more and see how we can use its properties to our advantage.</p>
<p>We still have a number of other ways that we can call $ping.<a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.send.aspx" target="_blank">Send</a>.  Our second option is to provide a <a href="http://msdn.microsoft.com/en-us/library/system.string.aspx" target="_blank">String</a> describing the hostname or IP address (as before), but also allows us to specify a timeout by providing an <a href="http://msdn.microsoft.com/en-us/library/system.int32.aspx" target="_blank">Int32</a>.  (Just a quick note, if you specify a really low timeout value, the reply still may be received.  It is more of an option to provide a longer than normal, five seconds, timeout period.)</p>
<p>We also see overloads that take an object of the <a href="http://msdn.microsoft.com/en-us/library/system.net.ipaddress.aspx" target="_blank">IPAddress</a> type to describe the target of the Ping.</p>
<p>Now, things get a bit more interesting.  The next argument we see is of the type <a href="http://msdn.microsoft.com/en-us/library/system.byte(VS.80).aspx" target="_blank">Byte</a>[], which means an <a href="http://msdn.microsoft.com/en-us/library/system.array.aspx" target="_blank">Array</a> of <a href="http://msdn.microsoft.com/en-us/library/system.byte(VS.80).aspx" target="_blank">Byte</a> objects.  The quick explanation of an <a href="http://msdn.microsoft.com/en-us/library/system.array.aspx" target="_blank">Array</a> is a collection of objects (not 100 percent accurate, but sufficient for right now).  This allows us to specify exactly what data will be sent with the ICMP echo message.</p>
<p>The final argument is to supply an object of the <a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.pingoptions.aspx" target="_blank">PingOptions</a> type.  This allows us to specify an <a href="http://msdn.microsoft.com/en-us/library/system.int32.aspx" target="_blank">Int32</a> as the TTL (Time To Live) and a <a href="http://msdn.microsoft.com/en-us/library/system.boolean.aspx" target="_blank">Boolean</a> as Don’t Fragment option.</p>
<p>I’m starting to run a bit long here, so I’m going to continue walking through this example in my next post.  We’ll create an object of type <a href="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx" target="_blank">Ping</a> with some custom options and then dig in to the object that is returned.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/02/exploring-the-net-framework-with-powershell-calling-a-method-part-2a/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Exploring the .NET Framework With PowerShell &#8211; Terminology (Part 1)</title>
		<link>http://blog.usepowershell.com/2009/02/exploring-the-net-framework-with-powershell-terminology-part-1/</link>
		<comments>http://blog.usepowershell.com/2009/02/exploring-the-net-framework-with-powershell-terminology-part-1/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 18:34:08 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Base Class Libraries]]></category>
		<category><![CDATA[Introduction]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/?p=12</guid>
		<description><![CDATA[PowerShell opens up the .NET Framework for a large new group of consumers, IT Pro’s (or systems administrators).  Microsoft developers have been working for years with the .NET Framework and now that rich sea of resources is available from our command line. One of the first hurdles that many IT Pro’s come across when trying [...]]]></description>
			<content:encoded><![CDATA[<p>PowerShell opens up the <a href="http://msdn.microsoft.com/en-us/netframework/default.aspx" target="_blank">.NET Framework</a> for a large new group of consumers, IT Pro’s (or systems administrators).  Microsoft developers have been working for years with the .NET Framework and now that rich sea of resources is available from our command line.</p>
<p>One of the first hurdles that many IT Pro’s come across when trying to see where the <a href="http://msdn.microsoft.com/en-us/netframework/default.aspx" target="_blank">.NET Framework</a> fits into their workflow is the terminology. </p>
<p>Before we start digging into the guts of the <a href="http://msdn.microsoft.com/en-us/netframework/default.aspx" target="_blank">.NET Framework</a>, let’s cover some definitions:</p>
<ul>
<li><strong>.NET Framework</strong> – The .NET Framework is a software framework that contains the Common Language Runtime and  Base Class Libraries that provide much of the functionality.  There are several versions of the .NET Framework.  PowerShell is compatible with versions 2.0, 3.0, and 3.5. </li>
<li><strong>Common Language Runtime</strong> (<strong>CLR</strong>) – The Common Language Runtime is the execution environment for .NET applications.  The CLR provides memory management, exception handling, thread management, and security infrastructure.  The CLR is the engine that runs .NET applications.</li>
<li><strong>Base Class Libraries</strong> (<strong>BCL</strong>) – The Base Class Libraries are sets of common functions accessible by any .NET language, including PowerShell.  The BCL is organized into namespaces based on functionality.  For example, the Webclient class is in the System.Net namespace.  The BCL includes functions for working with files, networking, database interaction, graphics, and much more.</li>
<li><strong>Namespace</strong> – A namespace allows CLR functions to be organized and distinguished.  Using a namespace helps prevent naming conflicts without having to have extra long names.  For example, the class System.Net.NetworkInformation.Ping is in the System.Net.NetworkInformation namespace.</li>
<li><strong>Class</strong> – A class is an abstract definition of some functionality.  The class is the definition that the CLR uses when objects are created.  Classes are also referred to as <strong>Types</strong>.  Classes can also have static methods, which are functions that can be used without having to create an instance of the class.</li>
<li><strong>Object</strong> – An object is the <strong>instance</strong> of a class.  An object is a distinct creation in memory that follows the blueprint described in the class.  For example, System.Net.WebClient is a class.  To create an instance of that class in PowerShell, I would: $webclient = New-Object System.Net.WebClient.  The result would be an object of the type System.Net.Webclient which is pointed to by the variable $webclient.</li>
<li><strong>Property</strong> – A property is data that is associated with an object or class.  Properties themselves are objects.  In our example, the System.Net.Webclient object referred to by $webclient has a property Credentials, which is an object of the type System.Net.NetworkCredentials.</li>
<li><strong>Method</strong> – Methods are actions that the object (or type in the case of static methods) can perform.  Methods can have a return type and can require different sets of parameters, allowing you different ways to call them.</li>
<li><strong>Event</strong> – Events are notices that an object can expose, allowing others to run code in response to a change of state of some sort.</li>
<li><strong>Exception</strong> – Exceptions are the error handling mechanism in the CLR.  Exceptions are strongly typed, so you can handle expected errors in your code differently depending on the problem.</li>
</ul>
<p>Now that we’ve got some definitions covered, we’ll start using PowerShell to expose the capabilities that the framework provides us and dig further into these definitions.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/02/exploring-the-net-framework-with-powershell-terminology-part-1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
