{"id":1521,"date":"2009-06-01T08:33:16","date_gmt":"2009-06-01T16:33:16","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2009\/06\/01\/updated-powershell-treeform\/"},"modified":"2009-06-01T08:33:16","modified_gmt":"2009-06-01T16:33:16","slug":"updated-powershell-treeform","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2009\/06\/01\/updated-powershell-treeform\/","title":{"rendered":"Updated PowerShell TreeForm"},"content":{"rendered":"<p>Not too long ago I wrote a blog post about a Windows Form PowerShell script to <a href=\"http:\/\/www.sapien.com\/blog\/2009\/04\/06\/up-another-powershell-tree\/\" target=\"_blank\">display a folder tree<\/a>. I got some feedback about an error when running the script. The problem was that I was casting a variable in one of the functions to a specific type. Normally this is considered a best practice. But in this situation the [System.Windows.Forms.TreeNode] class depends on the parent class being loaded in your PowerShell session. Even though I thought I had tested thoroughly on PowerShell v1 and v2 I must have missed something and the [System.WIndows.Forms] class was getting loaded into my shell prior to running the script, which is why I never saw the error.&nbsp; Anyway, I&#8217;ve since modified the problematic function to remove the type casting. Since I can trust that a treenode is getting passed to the function I can live without.<\/p>\n<p>While I was at it, I also added a debug feature.&nbsp; Throughout the script I&#8217;ve added numerous <strong>Write-Debug<\/strong> expressions so I can follow what the script is doing at any given time. If you use -debug as a parameter, the Debug pipeline is enabled by setting the $debugPreference to Continue.<\/p>\n<div style=\"border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px\">\n<div style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">Param ([string]$path=$env:temp,[<span style=\"color: #0000ff\">switch<\/span>]$debug)<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">&nbsp;<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\"><span style=\"color: #0000ff\">if<\/span> ($debug) {<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">  $debugPreference=<span style=\"color: #006080\">\"Continue\"<\/span><\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\"> }<\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\"> <\/pre>\n<pre style=\"border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px\">Write-Debug <span style=\"color: #006080\">\"Starting script in debug mode for $($path)\"<\/span><\/pre>\n<\/div>\n<\/div>\n<p>The rest of the script is essentially the same. I&#8217;ve updated the download link on the original post.&nbsp; Or you can download the revised file <a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2009\/06\/treedemo.zip\" target=\"_blank\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Not too long ago I wrote a blog post about a Windows Form PowerShell script to display a folder tree. I got some feedback about an error when running the script. The problem was that I was casting a variable in one of the functions to a specific type. Normally this is considered a best practice. But in this situation the [System.Windows.Forms.TreeNode] class depends on the parent class being loaded in your PowerShell session. Even though I thought I had tested thoroughly on PowerShell v1 and v2 I must have missed something and the [System.WIndows.Forms] class was getting loaded into my shell prior to running the script, which is why I never saw the error.  Anyway, I&#8217;ve since modified the problematic function to remove the type casting.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[25],"tags":[259,28,314,57,327],"class_list":["post-1521","post","type-post","status-publish","format-standard","hentry","category-windows-powershell","tag-debug","tag-powershell","tag-primalforms","tag-scripting","tag-winform"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=1521"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/1521\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=1521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=1521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=1521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}