{"id":837,"date":"2020-02-12T21:53:25","date_gmt":"2020-02-13T03:53:25","guid":{"rendered":"http:\/\/zewwy.ca\/?p=837"},"modified":"2020-05-09T17:11:16","modified_gmt":"2020-05-09T22:11:16","slug":"even-more-powershell-fun","status":"publish","type":"post","link":"https:\/\/zewwy.ca\/index.php\/2020\/02\/12\/even-more-powershell-fun\/","title":{"rendered":"Even More PowerShell Fun"},"content":{"rendered":"<h1 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"The_Story\"><\/span><strong>The Story<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>It&#8217;s another day, and we all know what that means&#8230; yes, another blog post, and even more PowerShell! Can you feel all the power!?!?!<\/p>\n<p>This time it came down to the storage size of my Exchange servers C:\\ which turns out to be due to Logs. Logs are great, and best practice is to only clear them if you have a backup copy. Often is the case that logs can be truncated after a backup via VSS by many backup solutions however in my case I could and probably should get that validated with Veeam (as I can&#8217;t seem to get that working &#8216;out of the box&#8217;) at the moment. So instead I wanted to know what was &#8220;usually&#8221; done server side even if someone was not implementing a backup solution.<\/p>\n<p><a href=\"https:\/\/social.technet.microsoft.com\/wiki\/contents\/articles\/31117.exchange-201320162019-logging-clear-out-the-log-files.aspx\">Source: https:\/\/social.technet.microsoft.com\/wiki\/contents\/articles\/31117.exchange-201320162019-logging-clear-out-the-log-files.aspx<\/a><\/p>\n<p>Neat, but the script is just alright, good for them doing what they want and that&#8217;s running it as a scheduled task. Not my goal, but a great source and starting point&#8230; let&#8217;s have some fun and give this script some roids, much like my last one&#8230; <a href=\"https:\/\/github.com\/Zewwy\/Manage-ExchangeLogs\">I&#8217;ll give this a home on GitHub<\/a>.<\/p>\n<p>Things learned&#8230;<\/p>\n<ol>\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/samples\/working-with-registry-entries?view=powershell-7\">Working with the Registry<\/a><\/li>\n<li><a href=\"https:\/\/serverfault.com\/questions\/95431\/in-a-powershell-script-how-can-i-check-if-im-running-with-administrator-privil\">Determining if Elevated<\/a> (This is great and I may have a solution to the conundrum in my previous PowerShell post)<\/li>\n<li><a href=\"http:\/\/powershell-guru.com\/dont-do-that-5-specify-the-type-of-variable-with-read-host\/\">Getting a Number<\/a>, <a href=\"https:\/\/redmondmag.com\/articles\/2018\/09\/27\/validate-input-in-powershell-functions-2.aspx\">and validating it<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/35585486\/check-if-a-variable-property-is-a-single-value-or-is-an-object-array\">Validating Objects by Type<\/a><\/li>\n<li><a href=\"http:\/\/woshub.com\/powershell-get-folder-sizes\/\">Getting Folder Sizes<\/a><\/li>\n<\/ol>\n<p>Check out my script for all the fun coding bits. I&#8217;m a bit tired now as it&#8217;s getting late so not much blogging, all more coding. \ud83d\ude42<\/p>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"ErrorAction_Stop_Not_Stopping_Script\"><\/span>ErrorAction Stop Not Stopping Script<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Quick Educational note <a href=\"https:\/\/stackoverflow.com\/questions\/46603935\/error-action-not-stopping-script\">(Source)<\/a><\/p>\n<pre>$ETLLogKey2 = 'HKLM:\\SOFTWARE\\Microsoft\\Search Foundation for Exchange\\Diagnostics'\r\ntry{Get-ItemProperty -Path $ETLLogKey2 -ErrorAction Stop}\r\ncatch{Write-Host \"No Key\"}\r\nWrite-Host \"This should not hit\"<\/pre>\n<p>Produces:<\/p>\n<p><a href=\"https:\/\/i.imgur.com\/kZiDwga.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i.imgur.com\/kZiDwga.png\" alt=\"\" width=\"178\" height=\"45\" \/><\/a><\/p>\n<p>Well poop&#8230; The catch block was triggered but the script did not stop&#8230;<\/p>\n<p>Oddly, changing to Throw, which is ugly does make the script stop&#8230;<\/p>\n<pre>$ETLLogKey2 = 'HKLM:\\SOFTWARE\\Microsoft\\Search Foundation for Exchange\\Diagnostics'\r\ntry{Get-ItemProperty -Path $ETLLogKey2 -ErrorAction Stop}\r\ncatch{throw \"No Key\"}\r\nWrite-Host \"This should not hit\"<\/pre>\n<p><a href=\"https:\/\/i.imgur.com\/pj13iDv.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i.imgur.com\/pj13iDv.png\" alt=\"\" width=\"626\" height=\"119\" \/><\/a><\/p>\n<p>Nice it worked this time, but it&#8217;s ugly&#8230;<\/p>\n<p>Write-Error is just as ugly, but doesn&#8217;t stop the script?<\/p>\n<pre>$ETLLogKey2 = 'HKLM:\\SOFTWARE\\Microsoft\\Search Foundation for Exchange\\Diagnostics'\r\ntry{Get-ItemProperty -Path $ETLLogKey2 -ErrorAction Stop}\r\ncatch{Write-Error \"No Key\"}\r\nWrite-Host \"This should not hit\"<\/pre>\n<p>Produces:<\/p>\n<p><a href=\"https:\/\/i.imgur.com\/p6lCd5U.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i.imgur.com\/p6lCd5U.png\" alt=\"\" width=\"659\" height=\"130\" \/><\/a><\/p>\n<p>Yet if I follow Sages answer in the source, and do a script variable for the stop action it then works???!?!<\/p>\n<pre>$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop\r\n$ETLLogKey2 = 'HKLM:\\SOFTWARE\\Microsoft\\Search Foundation for Exchange\\Diagnostics'\r\ntry{Get-ItemProperty -Path $ETLLogKey2}\r\ncatch{Write-Error \"No Key\"}\r\nWrite-Host \"This should not hit\"<\/pre>\n<p><a href=\"https:\/\/i.imgur.com\/FE70hos.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i.imgur.com\/FE70hos.png\" alt=\"\" width=\"630\" height=\"114\" \/><\/a><\/p>\n<p>Those are really weird results, but all still ugly&#8230;  so it seems even though -ErrorAction Stop causes a non-terminating error to be treated as a terminating error, but depending on what you do in the catch block determines if there&#8217;s a break\/exit event being done. In my case to have things look nice and actually stop the script I have to do the follow.<\/p>\n<pre>$ETLLogKey2 = 'HKLM:\\SOFTWARE\\Microsoft\\Search Foundation for Exchange\\Diagnostics'\r\ntry{Get-ItemProperty -Path $ETLLogKey2 -ErrorAction Stop}\r\ncatch{Write-host \"No Key\";break}\r\nWrite-Host \"This should not hit\"<\/pre>\n<p>Which finally produced the output I wanted. (I could have also used exit in place of break)<\/p>\n<p><a href=\"https:\/\/i.imgur.com\/4tJNNMs.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i.imgur.com\/4tJNNMs.png\" alt=\"\" width=\"189\" height=\"37\" \/><\/a><\/p>\n<p>Finally!<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Validating_URLs\"><\/span>Validating URLs:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: center;\"><a href=\"https:\/\/community.idera.com\/database-tools\/powershell\/powertips\/b\/tips\/posts\/validating-a-url\">Source: <\/a><\/p>\n<p>Good but broken due to the match on the scheme extension:<\/p>\n<pre><span class=\"keyword\">function<\/span> isURI(<span class=\"var\">$address<\/span>) {\r\n\t(<span class=\"var\">$address<\/span> <span class=\"op\">-as<\/span> [<span class=\"namespace\">System.URI<\/span>]).<span class=\"method\">AbsoluteURI<\/span> <span class=\"op\">-ne<\/span> <span class=\"var\">$null<\/span>\r\n}\r\n\r\n<span class=\"keyword\">function<\/span> isURIWeb(<span class=\"var\">$address<\/span>) {\r\n\t<span class=\"var\">$uri<\/span> <span class=\"op\">=<\/span> <span class=\"var\">$address<\/span> <span class=\"op\">-as<\/span> [<span class=\"namespace\">System.URI<\/span>]\r\n\t<span class=\"var\">$uri<\/span>.<span class=\"method\">AbsoluteURI<\/span> <span class=\"op\">-ne<\/span> <span class=\"var\">$null<\/span> <span class=\"op\">-and<\/span> <span class=\"var\">$uri<\/span>.<span class=\"method\">Scheme<\/span> <span class=\"op\">-match<\/span> <span class=\"string\">'[http|https]'<\/span>\r\n}\r\n\r\n\r\nisURI(<span class=\"string\">'http:\/\/www.powershell.com'<\/span>)\r\nisURI(<span class=\"string\">'test'<\/span>)\r\nisURI(<span class=\"var\">$null<\/span>)\r\nisURI(<span class=\"string\">'zzz:\/\/zumsel.zum'<\/span>)\r\n\r\n<span class=\"string\">\"-\"<\/span> <span class=\"op\">*<\/span> 50\r\n\r\nisURIWeb(<span class=\"string\">'http:\/\/www.powershell.com'<\/span>)\r\nisURIWeb(<span class=\"string\">'test'<\/span>)\r\nisURIWeb(<span class=\"var\">$null<\/span>)\r\nisURIWeb(<span class=\"string\">'zzz:\/\/zumsel.zum'<\/span>)\r\nisURIWeb(<span class=\"string\">'hp:'<\/span>) #Return True<\/pre>\n<p>&nbsp;<\/p>\n<p>Better results with:<\/p>\n<pre><span class=\"keyword\">function<\/span> isURI(<span class=\"var\">$address<\/span>) {\r\n(<span class=\"var\">$address<\/span> <span class=\"op\">-as<\/span> [<span class=\"namespace\">System.URI<\/span>]).<span class=\"method\">AbsoluteURI<\/span> <span class=\"op\">-ne<\/span> <span class=\"var\">$null<\/span>\r\n}\r\n\r\n<span class=\"keyword\">function<\/span> isURIWeb(<span class=\"var\">$address<\/span>) {\r\n<span class=\"var\">$uri<\/span> <span class=\"op\">=<\/span> <span class=\"var\">$address<\/span> <span class=\"op\">-as<\/span> [<span class=\"namespace\">System.URI<\/span>]\r\n$uri.AbsoluteURI -ne $null -and $uri.Scheme -match \"http|https\"\r\n}\r\n\r\n\r\nisURI(<span class=\"string\">'http:\/\/www.powershell.com'<\/span>)\r\nisURI(<span class=\"string\">'test'<\/span>)\r\nisURI(<span class=\"var\">$null<\/span>)\r\nisURI(<span class=\"string\">'zzz:\/\/zumsel.zum'<\/span>)\r\n\r\n<span class=\"string\">\"-\"<\/span> <span class=\"op\">*<\/span> 50\r\n\r\nisURIWeb(<span class=\"string\">'http:\/\/www.powershell.com'<\/span>)\r\nisURIWeb(<span class=\"string\">'test'<\/span>)\r\nisURIWeb(<span class=\"var\">$null<\/span>)\r\nisURIWeb(<span class=\"string\">'zzz:\/\/zumsel.zum'<\/span>)\r\nisURIWeb(<span class=\"string\">'hp:'<\/span>) #Return True<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The Story It&#8217;s another day, and we all know what that means&#8230; yes, another blog post, and even more PowerShell! Can you feel all the power!?!?! This time it came down to the storage size of my Exchange servers C:\\ which turns out to be due to Logs. Logs are great, and best practice is &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/zewwy.ca\/index.php\/2020\/02\/12\/even-more-powershell-fun\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Even More PowerShell Fun&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"sfsi_plus_gutenberg_text_before_share":"","sfsi_plus_gutenberg_show_text_before_share":"","sfsi_plus_gutenberg_icon_type":"","sfsi_plus_gutenberg_icon_alignemt":"","sfsi_plus_gutenburg_max_per_row":"","footnotes":""},"categories":[11,36,8],"tags":[37,278,150],"class_list":["post-837","post","type-post","status-publish","format-standard","hentry","category-scripting","category-exchange","category-server-administration","tag-exchange","tag-logs","tag-powershell"],"_links":{"self":[{"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/posts\/837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/comments?post=837"}],"version-history":[{"count":4,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/posts\/837\/revisions"}],"predecessor-version":[{"id":907,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/posts\/837\/revisions\/907"}],"wp:attachment":[{"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/media?parent=837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/categories?post=837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/tags?post=837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}