<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.technet.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Why Use .NET Framework Classes from Within PowerShell?</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/11/why-use-net-framework-classes-from-within-powershell.aspx</link><description>Microsoft Scripting Guy, Ed Wilson, talks about the need to use .NET Framework classes from within Windows PowerShell code.</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Why Use .NET Framework Classes from Within PowerShell?</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/11/why-use-net-framework-classes-from-within-powershell.aspx#3484757</link><pubDate>Tue, 06 Mar 2012 02:28:34 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3484757</guid><dc:creator>Brian T. Jackett</dc:creator><description>&lt;p&gt;To me there is a third option: performance.&lt;/p&gt;
&lt;p&gt;When I run both commands with measure-command the static .Net call is 2x-3x faster on my machine. &amp;nbsp;May not seem like much, but to a developer-minded person that could be the tipping of the scales in favor of using .Net vs. native PowerShell commands. &amp;nbsp;Many times when I&amp;#39;m teaching PowerShell workshops I show an example of adding numbers 1 to 10,000 to an array using native PowerShell array vs. .Net generic list. &amp;nbsp;The .Net way is typically 1000% faster than native PowerShell. &amp;nbsp;Raises interesting questions and discussions.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3484757" width="1" height="1"&gt;</description></item><item><title>re: Why Use .NET Framework Classes from Within PowerShell?</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/11/why-use-net-framework-classes-from-within-powershell.aspx#3480914</link><pubDate>Tue, 14 Feb 2012 18:48:02 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3480914</guid><dc:creator>jrv</dc:creator><description>&lt;p&gt;@JohnLan –&lt;/p&gt;
&lt;p&gt;I was not really considering off-point &amp;#39;noisy&amp;#39; issues such as job security.&lt;/p&gt;
&lt;p&gt;Issues of performance, understandability, portability and conformance to standards, both global and corporate, are what i believe are important areas of discussion. Perhaps there are other areas that should be included as well.&lt;/p&gt;
&lt;p&gt;Job security is about an individual employee and has no bearing here.&lt;/p&gt;
&lt;p&gt;We can re-interpret &amp;#39;look cool&amp;#39; to be the readability of a script. &amp;nbsp;To that point I offer the Verb-Noun nature of CmdLets as one good reason to use them over the Net Classes direct method.&lt;/p&gt;
&lt;p&gt;In simple everyday scripts I suggest whatever gets the job done without damage to the system s being acceptable. &amp;nbsp;For scripts that are to be shared a nit more discipline may be called for.&lt;/p&gt;
&lt;p&gt;I would suggest doing what we have always done in programming when calling a foreign API; encapsulate it.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;p&gt;function Get-WebFile([string]$fileurl]{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; $wc.DownloadFile($fileurl)&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;This can be included in a library module and will allow us to modify its behavior. &amp;nbsp;We can also replace it easily if newer functionality becomes available by just editing one location. &amp;nbsp;The designers of PowerShell have given us the tools to support nearly all standard modes of program design and support. &amp;nbsp;PowerShell is not a second class scripting language. It is a full first class language that supports command line ad-hoc tools and batch scripts along with the ability to be easily extended in almost any direction.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3480914" width="1" height="1"&gt;</description></item><item><title>re: Why Use .NET Framework Classes from Within PowerShell?</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/11/why-use-net-framework-classes-from-within-powershell.aspx#3480902</link><pubDate>Tue, 14 Feb 2012 18:04:01 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3480902</guid><dc:creator>JohnLan</dc:creator><description>&lt;p&gt;@jrv: I totally agree there are best practices, I also agree that one way is better, slightly or significantly, than the other from technical perspective; but I just don&amp;#39;t see how it would relate to the conclusion that someone tried to make his/her script look cool, or job security discussion etc.&lt;/p&gt;
&lt;p&gt;Why not just keep technical discussion to technical?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3480902" width="1" height="1"&gt;</description></item><item><title>re: Why Use .NET Framework Classes from Within PowerShell?</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/11/why-use-net-framework-classes-from-within-powershell.aspx#3480895</link><pubDate>Tue, 14 Feb 2012 17:44:31 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3480895</guid><dc:creator>jrv</dc:creator><description>&lt;p&gt;@JohnLan&lt;/p&gt;
&lt;p&gt;I will defend the blog. &amp;nbsp;It has begun some good discussion. &amp;nbsp;In programming and in all technical pursuits it is alwasy worthwhile to debate the pros and cons of any widely used methods. &amp;nbsp;I feel we can all learn something from this kind of discussion.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3480895" width="1" height="1"&gt;</description></item><item><title>re: Why Use .NET Framework Classes from Within PowerShell?</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/11/why-use-net-framework-classes-from-within-powershell.aspx#3480876</link><pubDate>Tue, 14 Feb 2012 16:23:42 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3480876</guid><dc:creator>JohnLan</dc:creator><description>&lt;p&gt;I see a lot judgemental statements in the posts - I myself really don&amp;#39;t see a big difference between using [datetime] static property or get-date cmdlet. It could be just a habit, it could be just whatever came up first in googling when a beginner didn&amp;#39;t know how to get current time, then he/she sticks to that method. Either way, big deal. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3480876" width="1" height="1"&gt;</description></item><item><title>re: Why Use .NET Framework Classes from Within PowerShell?</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/11/why-use-net-framework-classes-from-within-powershell.aspx#3480375</link><pubDate>Sun, 12 Feb 2012 03:28:33 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3480375</guid><dc:creator>Boe Prox</dc:creator><description>&lt;p&gt;This is a key line in the article: &amp;quot;Keep in mind, that as a best practice, I recommend using a native Windows PowerShell cmdlet when it exists—unless there are compelling reasons for not doing so.&amp;quot; In my mind this extends beyond just using static .Net methods but also using .Net classes to build objects that perform the same job as existing PowerShell cmdlets.&lt;/p&gt;
&lt;p&gt;Sometimes it is a lack of education when a .Net method is used instead of a cmdlet that can be helped just by advising the individual of the correct PowerShell cmdlet. A number of examples I have seen recently are folks using System.Net.Mail.MailMessage instead of Send-MailMessage and it was just a case of not knowing that this was available. A quick comment on their blog letting them know about this and now they are using the cmdlet in future scripts. Other examples I have seen are using .Net for &amp;quot;pinging&amp;quot; a machine when Test-Connection works.&lt;/p&gt;
&lt;p&gt;Other times it might be because of the background of the person writing the script where they have a heavy background as a .Net developer and just simply fall into the habit of what they use the most. It is just like a vbscripter who first steps into PowerShell and wants to continue to write code in a &amp;quot;vbscript way&amp;quot;. Sometimes it just takes a little bit of time to adjust to a PowerShell frame of mind when writing the code.&lt;/p&gt;
&lt;p&gt;In the case of dates, using a cmdlet is the preferred way to go. But in the case of downloading a webpage, at least in V2, using the .Net method (Net.WebClient) is the obvious best choice to work with until V3 goes RTM and then you can use something like Invoke-WebRequest to accomplish this. &amp;nbsp;When working with Active Directory, it all depends on what you have available in your environment at the time. If you have access to the AD module, then you use that, otherwise you make use of the .Net methods in the way of the type accelerators ([adsi],[adsisearcher]) to accomplish your goal.&lt;/p&gt;
&lt;p&gt;Just my 2 cents...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3480375" width="1" height="1"&gt;</description></item><item><title>re: Why Use .NET Framework Classes from Within PowerShell?</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/11/why-use-net-framework-classes-from-within-powershell.aspx#3480369</link><pubDate>Sun, 12 Feb 2012 01:55:06 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3480369</guid><dc:creator>jrv</dc:creator><description>&lt;p&gt;#rjcox - sorry I flomoxed that one. &amp;nbsp;Her is the correct version:&lt;/p&gt;
&lt;p&gt;PS&amp;gt;(Get-Date).Date&lt;/p&gt;
&lt;p&gt;Saturday, February 11, 2012 12:00:00 AM&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3480369" width="1" height="1"&gt;</description></item><item><title>re: Why Use .NET Framework Classes from Within PowerShell?</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/11/why-use-net-framework-classes-from-within-powershell.aspx#3480365</link><pubDate>Sat, 11 Feb 2012 22:05:11 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3480365</guid><dc:creator>K_Schulte</dc:creator><description>&lt;p&gt;Hi Ed, Hi @all&lt;/p&gt;
&lt;p&gt;to cut things short: If you can: make the best out of both worlds!&lt;/p&gt;
&lt;p&gt;There are no scary things involved!&lt;/p&gt;
&lt;p&gt;PS is based on the Net 2.0 framework, it won&amp;#39;t be here, without it!&lt;/p&gt;
&lt;p&gt;Use cmdlets if appropriate and well known!&lt;/p&gt;
&lt;p&gt;Use .Net 2.0 if you can and if nothing else is knwon to be available the PS way ...&lt;/p&gt;
&lt;p&gt;Klaus (Schulte)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3480365" width="1" height="1"&gt;</description></item><item><title>re: Why Use .NET Framework Classes from Within PowerShell?</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/11/why-use-net-framework-classes-from-within-powershell.aspx#3480364</link><pubDate>Sat, 11 Feb 2012 21:48:35 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3480364</guid><dc:creator>rjcox</dc:creator><description>&lt;p&gt;@jrv&lt;/p&gt;
&lt;p&gt;&amp;gt; Get-date &amp;nbsp; #[DateTime]::Today&lt;/p&gt;
&lt;p&gt;These are not the same, [DateTime]::Today includes a zero time (ie. midnight), Get-Date is the current time, so&lt;/p&gt;
&lt;p&gt; &amp;nbsp;$someDate -lt [DateTime]::Today&lt;/p&gt;
&lt;p&gt;is true if $someDate is yesterday or before.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3480364" width="1" height="1"&gt;</description></item><item><title>re: Why Use .NET Framework Classes from Within PowerShell?</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/11/why-use-net-framework-classes-from-within-powershell.aspx#3480352</link><pubDate>Sat, 11 Feb 2012 17:01:24 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3480352</guid><dc:creator>JeremyEngelWork</dc:creator><description>&lt;p&gt;I think there is another aspect we&amp;#39;re missing, which is not so negative - Innocence. I for one learned how to code with C++ and then C#, and I might use those without knowing there is a cmdlet equivalent. Of course, when I do discover the &amp;quot;PowerShell Way&amp;quot; I try to convert to using that, but I can&amp;#39;t know all things without first learning of it. That&amp;#39;s another reason why this site and the articles is so great and important! I learn every day.&lt;/p&gt;
&lt;p&gt;And as some of you pointed out .Net is native to PowerShell, so always using the cmdlets over raw .Net classes is nothing more than a subjective value statement. I agree that reading PowerShell cmdlets is easier, but I find instances where using the raw class is better for automation, such as when I build scripts to access AD. The AD cmdlets are great when you have the AD module installed, but most of my computers do not have that. Therefore, I tend not to use it in any of my formal scripts because I value portability across my systems over ease of reading. Conversely, I&amp;#39;ll use the AD module cmdlets for one-off queries, or if I know I&amp;#39;ll only run a script on computers with the AD module.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3480352" width="1" height="1"&gt;</description></item></channel></rss>