{"id":442,"date":"2019-11-25T17:46:32","date_gmt":"2019-11-25T23:46:32","guid":{"rendered":"http:\/\/draith.com\/?p=442"},"modified":"2019-11-25T17:46:32","modified_gmt":"2019-11-25T23:46:32","slug":"2-great-experimental-features-in-core-7","status":"publish","type":"post","link":"https:\/\/draith.com\/?p=442","title":{"rendered":"2 Great Experimental Features in Core 7"},"content":{"rendered":"\n\n\n<p>In this blog post, I want to drill into a couple of very useful experimental features that are available in PowerShell 7 preview 6.&nbsp; The two features I want to dive into are \u2018PSNullConditionalOperators\u2019 and &nbsp;\u2018skip error check on web cmdlets\u2019.&nbsp; The former feature was available in preview5, and the latter was made available in preview 6.&nbsp; Both of these are (as of the writing of this blog) experimental features.&nbsp; In order to play around with these features, I suggest doing a \u2018Enable-ExperimentalFeature *\u2019 and restarting your PowerShell session.&nbsp; When you are done with your testing, you can always do a \u201cDisable-ExperimentalFeature\u201d to get your session back to normal.<\/p>\n<p>What exactly is \u2018PSNullConditionalOperators\u2019?&nbsp; This specifically refers to the \u2018&amp;&amp;\u2019 and \u2018||\u2019 operators, which deal with continuing or stopping a set of commands depending on the success or failure of the first command.&nbsp; The \u2018&amp;&amp;\u2019 operator will execute what is on it\u2019s right if the command on it\u2019s left succeeded, and conversely the \u2018||\u2019 operator will execute what is on it\u2019s right if the command on it\u2019s left failed.&nbsp; Probably the best way to picture this is with an example.<\/p>\n<p>&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powershell\">Write-Host \u201cThis is a good command\u201d &amp;&amp; Write-Host \u201cTherefore this command will run\u201d\n\nThis is a good command\n\nTherefore this command will run<\/code><\/pre>\n\n\n\n<p>The first write-host succeeded, and so the second write-host was run.&nbsp; Now, let\u2019s look at \u2018||\u2019.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powershell\">write-badcmdlet \"This is a bad command\" || Write-Host \u201cTherefore this command will run\u201d\nwrite-badcmdlet: The term 'write-badcmdlet' is not recognized as the name of a cmdlet, function, script file, or operable program.\n\nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.\n\nTherefore this command will run<\/code><\/pre>\n\n\n\n<p>Here we can see that the \u2018write-badcmdlet\u2019 cmdlet doesn\u2019t exist (if it does, we need to talk about your naming conventions), so the second command was run.&nbsp; This can replace 3 or 4 lines of code easily \u2013 you would normally have to do an if-else block to check the $? variable and see if it was true or false.&nbsp; You can also get fancy and chain these together for even more checks.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powershell\">write-badcmdlet \"This is a bad command\" || Write-Host \u201cTherefore this command will run\u201d &amp;&amp; \"And so will this one\"\n\n\n\n\nwrite-badcmdlet: The term 'write-badcmdlet' is not recognized as the name of a cmdlet, function, script file, or operable program.\n\nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.\n\nTherefore this command will run\n\nAnd so will this one<\/code><\/pre>\n\n\n\n<p>You can see how the 3<sup>rd<\/sup> command worked because the 2<sup>nd<\/sup> command ran successfully.&nbsp; Very handy, and dead simple to consolidate code.&nbsp; They even work with OS commands!&nbsp; Look at this example using notepad and the non-existent notepad2:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-powershell\">notepad &amp;&amp; \"Worked fine!\"\n\nWorked fine!\n\nnotepad2 &amp;&amp; \"Worked fine!\"\n\n\n\n\nnotepad2: The term 'notepad2' is not recognized as the name of a cmdlet, function, script file, or operable program.\n\nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.\n\nnotepad2 || \"Failed!\"\n\n\n\n\nnotepad2: The term 'notepad2' is not recognized as the name of a cmdlet, function, script file, or operable program.\n\nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.\n\nFailed!<\/code><\/pre>\n\n\n\n<p>\u00a0<\/p>\n<p>Now on to the second experimental feature \u2013 and one that I find amazingly handy.\u00a0 Often times when you are working with RestAPIs or really any sort of web request, you are dealing with invoke-restmethod and invoke-webrequest.\u00a0 One major pain point with these cmdlets is that if you get a non-200 error code back from the cmdlet, it returns an actual error object.\u00a0 Consider this:\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2019\/11\/shot1.png\" alt=\"\" class=\"wp-image-448\" width=\"1062\" height=\"450\" srcset=\"\/wp-content\/uploads\/2019\/11\/shot1.png 1062w, \/wp-content\/uploads\/2019\/11\/shot1-300x127.png 300w, \/wp-content\/uploads\/2019\/11\/shot1-1024x434.png 1024w, \/wp-content\/uploads\/2019\/11\/shot1-768x325.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p>\n\nThat url doesn\u2019t exist, but the way the error object is returned can be a massive pain!&nbsp; What if I want to know if it was a 404 vs a 500 vs a 403 and actually want to do something about it?&nbsp; Dealing with error objects is not the most intuitive, and I much prefer the way the new experimental feature \u2018-SkipHttpErrorCheck\u2019 handles this.&nbsp; With this switch, a returned error code from these cmdlets will not return as an error.&nbsp; Combine this with \u2018-StatusCodeVariable\u2019 and you can now act on the actual error without large try\/catch blocks.&nbsp; You can use switch blocks to route properly based on status code without diving into error objects.&nbsp; This is a very welcome feature!\n\n<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2019\/11\/shot2.png\" alt=\"\" class=\"wp-image-447\" width=\"892\" height=\"517\" srcset=\"\/wp-content\/uploads\/2019\/11\/shot2.png 892w, \/wp-content\/uploads\/2019\/11\/shot2-300x174.png 300w, \/wp-content\/uploads\/2019\/11\/shot2-768x445.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>That url doesn\u2019t exist, but the way the error object is returned can be a massive pain!&nbsp; What if I want to know if it was a 404 vs a 500 vs a 403 and actually want to do something about it?&nbsp; Dealing with error objects is not the most intuitive, and I much prefer &hellip; <a href=\"https:\/\/draith.com\/?p=442\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;2 Great Experimental Features in Core 7&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[16],"class_list":["post-442","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-powershell"],"_links":{"self":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=442"}],"version-history":[{"count":0,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/442\/revisions"}],"wp:attachment":[{"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}