{"id":17854,"date":"2008-11-23T12:22:01","date_gmt":"2008-11-23T20:22:01","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/powershell\/?p=17854"},"modified":"2019-06-07T12:23:44","modified_gmt":"2019-06-07T20:23:44","slug":"poshboard-and-convertto-hashtable","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/poshboard-and-convertto-hashtable\/","title":{"rendered":"PoshBoard and ConvertTo-HashTable"},"content":{"rendered":"<p><P>I\u2019m currently watching an cool <A target=_blank href=\"https:\/\/vimeo.com\/1295215?pg=embed&amp;sec=1295215&amp;hd=1\" mce_href=\"https:\/\/vimeo.com\/1295215?pg=embed&amp;sec=1295215&amp;hd=1\">video of PoshBoard<\/A> a PowerShell based Web Portal by Pilosite available on <A target=_blank href=\"http:\/\/www.codeplex.com\/Poshboard\" mce_href=\"http:\/\/www.codeplex.com\/Poshboard\">CodePlex<\/A>.&nbsp;&nbsp; It looks great.&nbsp; <\/P>\n<P>In the middle of the video I noticed the following script he uses to populate one of the controls on the dashboard:<\/P>\n<P><FONT color=#0000ff size=2 face=con>$ht = @{ } <BR>$objDrives = gwmi \u2013class \u201cWin32_logicalDisk\u201d \u2013Namespace \u201croot\\cimv2\u201d |where {$_.DriveType -eq 3 } <BR>foreach ($ObjDisk in $objDrives) <BR>{ <BR>&nbsp;&nbsp;&nbsp; $size = $objDisk.size \/ 1024 <BR>&nbsp;&nbsp;&nbsp; $freespace = $objDisk.FreeSpace \/ 1024 <BR>&nbsp;&nbsp;&nbsp; $IntUsed = ($size &#8211; $freeSpace) * 1024 <BR>&nbsp;&nbsp;&nbsp; $ht.Add($objDisk.DeviceId, $IntUsed) <BR>}<\/FONT><\/P>\n<P>This is perfectly fine code but I noticed a couple opportunities for improvement:<\/P>\n<OL>\n<LI>You don\u2019t need to use quotes in:&nbsp; \u201cWin32_logicalDisk\u201d&nbsp; or \u201croot\\cimv2\u201d<\/LI>\n<LI>The default namespace for gwmi is root\\cimv2 so it it is redundant<\/LI>\n<LI>Gwmi supports \u2013FILTER which can be dramatically more efficient so it is worth learning it\u2019s different syntax and using it when you can<\/LI>\n<LI>CLASS can be a positional parameter so you don\u2019t need to say \u2013CLASS.<\/LI><\/OL>\n<P>Those observations allow us to transform<\/P>\n<P>&nbsp;<FONT color=#0000ff size=2 face=con>&nbsp;&nbsp;&nbsp; $objDrives = gwmi \u2013class \u201cWin32_logicalDisk\u201d \u2013Namespace \u201croot\\cimv2\u201d |where {$_.DriveType -eq 3 }<\/FONT><\/P>\n<P>into<\/P>\n<P>&nbsp;<FONT color=#0000ff size=2 face=con>&nbsp;&nbsp; $objDrives = gwmi Win32_logicalDisk \u2013Filter \u201cDriveType = 3\u201d<\/FONT> <BR><\/P>\n<P>Next I noticed that it was using a foreach loop to create a hashtable.&nbsp; I\u2019ve seen lots of code like this and in talking to a lot of new users, I\u2019ve come the the conclusion that there are a set of beginner users that have a difficult time getting their heads around control structures (e.g. if\/elseif\/else, for, foreach, while, do\/until).&nbsp; These folks seem to do just fine with commands and pipelines so I\u2019m on the lookout for ways to make control structures option.&nbsp; In the spirit, I wrote ConvertTo-HashTable.ps1 :<\/P>\n<P><FONT color=#0000ff size=2 face=con># ConvertTo-hashTable.ps1 <BR>#&nbsp;&nbsp;&nbsp; <BR>param( <BR>[string]&nbsp; <BR>$key, <\/FONT><\/P>\n<P><FONT color=#0000ff size=2 face=con>$value <BR>) <BR>Begin <BR>{ <BR>&nbsp;&nbsp;&nbsp; $hash = @{} <BR>&nbsp;&nbsp;&nbsp; $Script = $false <BR>&nbsp;&nbsp;&nbsp; if ($value -is [ScriptBlock]) <BR>&nbsp;&nbsp;&nbsp; { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $Script = $true <BR>&nbsp;&nbsp;&nbsp; } <BR>} <BR>Process <BR>{ <BR>&nbsp;&nbsp;&nbsp; $thisKey = $_.$Key <BR>&nbsp;&nbsp;&nbsp; if ($script) <BR>&nbsp;&nbsp;&nbsp; { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $hash.$thisKey = &amp; $Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; }else <BR>&nbsp;&nbsp;&nbsp; { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $hash.$thisKey = $_.$Value <BR>&nbsp;&nbsp;&nbsp; } <BR>} <BR>End <BR>{ <BR>&nbsp;&nbsp;&nbsp; Write-Output $hash <BR>}<\/FONT><\/P>\n<P>Once you have this (and you notice that you can get rid of the multiplying and dividing by 1024), we can transform this:<\/P>\n<P><FONT color=#0000ff size=2 face=Consolas>$ht = @{ } <BR>$objDrives = gwmi \u2013class \u201cWin32_logicalDisk\u201d \u2013Namespace \u201croot\\cimv2\u201d |where {$_.DriveType -eq 3 } <BR>foreach ($ObjDisk in $objDrives) <BR>{ <BR>&nbsp;&nbsp;&nbsp; $size = $objDisk.size \/ 1024 <BR>&nbsp;&nbsp;&nbsp; $freespace = $objDisk.FreeSpace \/ 1024 <BR>&nbsp;&nbsp;&nbsp; $IntUsed = ($size &#8211; $freeSpace) * 1024 <BR>&nbsp;&nbsp;&nbsp; $ht.Add($objDisk.DeviceId, $IntUsed) <BR>}<\/FONT><\/P>\n<P>into this:<\/P>\n<P><FONT color=#0000ff size=2 face=cons>$ht = gwmi Win32_logicalDisk \u2013Filter \u201cDriveType = 3\u201d | ConvertTo-HashTable DeviceId {$_.size &#8211; $_.freeSpace}<\/FONT><\/P>\n<P>That looks pretty readable to me.<\/P>\n<P>BTW &#8211; here is the video <\/P>\n<OBJECT width=400 height=225><PARAM NAME=\"allowfullscreen\" VALUE=\"true\"><PARAM NAME=\"allowscriptaccess\" VALUE=\"always\"><PARAM NAME=\"movie\" VALUE=\"https:\/\/vimeo.com\/moogaloop.swf?clip_id=1295215&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1\">\n<embed src=\"https:\/\/vimeo.com\/moogaloop.swf?clip_id=1295215&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" type=\"application\/x-shockwave-flash\" allowfullscreen=\"true\" allowscriptaccess=\"always\" width=\"400\" height=\"225\"><\/embed><\/OBJECT><BR><A href=\"https:\/\/vimeo.com\/1295215\">PowerShell Dashboard Web Portal Presentation<\/A> from <A href=\"https:\/\/vimeo.com\/user301430\">pilosite<\/A> on <A href=\"https:\/\/vimeo.com\/\">Vimeo<\/A>. \n<P>Enjoy!<\/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\" mce_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\" mce_href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/A><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019m currently watching an cool video of PoshBoard a PowerShell based Web Portal by Pilosite available on CodePlex.&nbsp;&nbsp; It looks great.&nbsp; In the middle of the video I noticed the following script he uses to populate one of the controls on the dashboard: $ht = @{ } $objDrives = gwmi \u2013class \u201cWin32_logicalDisk\u201d \u2013Namespace \u201croot\\cimv2\u201d |where [&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":[132,242],"class_list":["post-17854","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-convertto-hashtable","tag-poshboard"],"acf":[],"blog_post_summary":"<p>I\u2019m currently watching an cool video of PoshBoard a PowerShell based Web Portal by Pilosite available on CodePlex.&nbsp;&nbsp; It looks great.&nbsp; In the middle of the video I noticed the following script he uses to populate one of the controls on the dashboard: $ht = @{ } $objDrives = gwmi \u2013class \u201cWin32_logicalDisk\u201d \u2013Namespace \u201croot\\cimv2\u201d |where [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/17854","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=17854"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/17854\/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=17854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=17854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=17854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}