{"id":3972,"date":"2024-05-13T22:00:01","date_gmt":"2024-05-13T22:00:01","guid":{"rendered":"https:\/\/wiki.thomasandsofia.com\/?p=3972"},"modified":"2024-05-13T22:07:25","modified_gmt":"2024-05-13T22:07:25","slug":"powershell-functions-and-exceptions","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=3972","title":{"rendered":"Powershell: Functions and Exceptions"},"content":{"rendered":"<h1>Functions<\/h1>\n<p><strong>Define a function<\/strong><\/p>\n<pre>function myFunction { \r\n  param(\r\n    #define parameters\r\n    $MyParameter\r\n  )\r\n  # Do some stuff\r\n  Write-Host \"You set MyParameter to\" $MyParameter\r\n}\r\nmyFunction -MyParameter 10<\/pre>\n<p><em>Note: above function prints &#8216;You set MyParameter to 10&#8217;<\/em><\/p>\n<p><strong>Cmdlet Bindings<\/strong><\/p>\n<p>If the required parameter is not set when calling, the function <del>will<\/del> might fail. You can avoid this with the following:<\/p>\n<pre>function myFunction { \r\n  [CmdletBinding()]\r\n  param(\r\n    [Parameter(Mandatory)]\r\n    [int32]$MyParameter\r\n  )\r\n  Write-Host \"You set MyParameter to\" $MyParameter\r\n}\r\nmyFunction\r\n\r\n... run\r\nC:&gt; myFunction.ps1\r\ncmdlet myFunction at command pipeline position 1\r\nSupply values for the following parmeters:\r\nMyParameter: _ #&lt;- waiting for a value.<\/pre>\n<p><em>Note: On my system, I did not get errors if I did not pass in the desired parameter. Instead, the parameter is set to Null and the script executes as expected with $CountDown == NULL<\/em><\/p>\n<h1>Exceptions<\/h1>\n<p>Throw<\/p>\n<p>Command sends an error to the OS.<\/p>\n<pre>C:&gt; Throw \"It's a trap!\"\r\nIt's a trap!\r\nAt line:1 char:1\r\n+ throw \"It's a trap!\"\r\n+ ~~~~~~~~~~~~~~~~~~~~\r\n  + CategoryInfo : OperationStopped: (It's a trap!:String) [], RuntimeException\r\n  + FullyQualifiedErrorId : It's a trap!<\/pre>\n<p><strong>Try &#8211; Catch<\/strong><\/p>\n<p>How to handle errors. This should crash because we&#8217;re not setting any parameters<\/p>\n<pre>function SpaceX{\r\n  param(\r\n    $CountDown\r\n  )\r\n  While ($CountDown -ge 1){\r\n    Write-Host $CountDown \"Seconds to blastoff!\"\r\n  }\r\n  Write-Host \"Blastoff!!\"\r\n}\r\ntry { \r\n  SpaceX -ErrorAction Stop \r\n} catch {\r\n  Write-Output \"Launch Problem!\" Write-Output $_\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Functions Define a function function myFunction { param( #define parameters $MyParameter ) # Do some stuff Write-Host &#8220;You set MyParameter to&#8221; $MyParameter } myFunction -MyParameter 10 Note: above function prints &#8216;You set MyParameter to 10&#8217; Cmdlet Bindings If the required parameter is not set when calling, the function will might fail. You can avoid this ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=3972\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82,83],"tags":[],"class_list":["post-3972","post","type-post","status-publish","format-standard","hentry","category-powershell","category-programming"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/3972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3972"}],"version-history":[{"count":3,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/3972\/revisions"}],"predecessor-version":[{"id":3975,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/3972\/revisions\/3975"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}