{"id":6031,"date":"2008-06-03T08:37:00","date_gmt":"2008-06-03T08:37:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2008\/06\/03\/v2-custom-enums\/"},"modified":"2019-02-18T13:15:51","modified_gmt":"2019-02-18T20:15:51","slug":"v2-custom-enums","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/v2-custom-enums\/","title":{"rendered":"V2:  Custom Enums"},"content":{"rendered":"<p>Once again MOW proves what a clever guy he is.&nbsp; Check out his blog entry <a class=\"\" href=\"http:\/\/thepowershellguy.com\/blogs\/posh\/archive\/2008\/06\/02\/powershell-v2-ctp2-making-custom-enums-using-add-type.aspx\">PowerShell V2 CTP2: making Custom Enums using Add-Type<\/a>. He shows how you can make your own enums using a very simple function he wrote (Add-Enum) which leverages our new Add-Type cmdlet.&nbsp; Wonderful stuff.<\/p>\n<p>Add-Type is one of those huge game-changing features that we&#8217;ve added to V2 and it will take all of us a while to understand the full range of scenarios that it enables.&nbsp; The things that we had in focus for this were:<\/p>\n<ul>\n<li>Access to WIN32 APIs.&nbsp; Check out <a href=\"http:\/\/pinvoke.net\/\">http:\/\/pinvoke.net<\/a> .&nbsp; It has C# and VB.Net code snippets to access WIN32 APIs.&nbsp; Now you can just go there, cut and paste that code into your PS script using Add-Type and your script can now access WIN32 APIs.&nbsp; With this, I think we now provide complete access to all the major (and many of the minor) API sets you could want to use:&nbsp; .Net, COM, WMI, ADSI, XML, ADO, Win32.&nbsp; If we are missing anything, please let us know.<\/li>\n<li>Performance.&nbsp; There will be times when you need absolute performance for an operation (e.g. you need to chug through a couple terabytes of log files) and you want all the nice features of PowerShell but you need the inner core of operations to be maximally efficient.&nbsp; We didn&#8217;t want to make you throw everything away and re-write it in .NET.&nbsp; Now you can have portions of your processing in C#\/VB.NET and fly like the wind.<\/li>\n<li>Simple mechanism to load assemblies (read MOWs blog).<\/li>\n<\/ul>\n<p>We had a couple others but these were the biggies.&nbsp; <\/p>\n<p>There is an important aspect to Add-Type that you need to get in focus.&nbsp; Let me illustrate it with the following example using MOW&#8217;s cool Add-Enum function:<\/p>\n<p>PS&gt; <strong>Add-Enum Fruit &#8220;Apple&#8221;,&#8221;Bannana&#8221;,&#8221;Strawberry&#8221;<br \/><\/strong>PS&gt;<strong> [fruit]&#8221;Apple&#8221;<\/strong><br \/>Apple<br \/>PS&gt;<strong> [fruit]&#8221;Banana&#8221;<\/strong><br \/>Cannot convert value &#8220;Banana&#8221; to type &#8220;Fruit&#8221; due to invalid enumeration values. Specify<br \/>one of the following enumeration values and try again. The possible enumeration values ar<br \/>e &#8220;Apple, Bannana, Strawberry&#8221;.<br \/>At line:1 char:8<br \/>+ [fruit] &lt;&lt;&lt;&lt; &#8220;Banana&#8221;<br \/>PS&gt; <strong>Add-Enum Fruit &#8220;Apple&#8221;,&#8221;Banana&#8221;,&#8221;Strawberry&#8221;<\/strong><br \/>Add-Type : Cannot add type. The type name &#8216;Fruit&#8217; already exists.<br \/>At line:20 char:11<br \/>+&nbsp;&nbsp; Add-Type &lt;&lt;&lt;&lt;&nbsp; $code<\/p>\n<p>Notice that I misspelled Banana when I first defined the Enum.&nbsp; Having done that, I can&#8217;t now go in and&nbsp;fix the Enum.&nbsp; That is because this is a now a proper .NET type and it is not dynamic.&nbsp; If you want to change this, you need to start a new session to do so.<\/p>\n<p>That is an important semantic that you should have in focus when working with Add-Type.&nbsp; It is an awesome tool but you need to adjust your workflow to accommodate this.&nbsp; e.g. do yor work in a throw-away worker process until you get it right.&nbsp; Note also that&nbsp; you need a worker PROCESS and not a worker RUNSPACE.&nbsp; When you add the type, you are adding it to the PROCESS not the runspace.<\/p>\n<p>Add-Type is an awesome new tool but you need to understand its semantics to master it.&nbsp; Try it out &#8211; you are going to love it.&nbsp; And just like MOW did, SHARE SHARE SHARE.&nbsp;&nbsp;When you share&nbsp;like MOW, you make&nbsp;us all smarter faster.&nbsp; <\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows Management Partner Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once again MOW proves what a clever guy he is.&nbsp; Check out his blog entry PowerShell V2 CTP2: making Custom Enums using Add-Type. He shows how you can make your own enums using a very simple function he wrote (Add-Enum) which leverages our new Add-Type cmdlet.&nbsp; Wonderful stuff. Add-Type is one of those huge game-changing [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[96],"class_list":["post-6031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-add-type"],"acf":[],"blog_post_summary":"<p>Once again MOW proves what a clever guy he is.&nbsp; Check out his blog entry PowerShell V2 CTP2: making Custom Enums using Add-Type. He shows how you can make your own enums using a very simple function he wrote (Add-Enum) which leverages our new Add-Type cmdlet.&nbsp; Wonderful stuff. Add-Type is one of those huge game-changing [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=6031"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6031\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=6031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=6031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=6031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}