{"id":2149,"date":"2017-03-08T21:45:54","date_gmt":"2017-03-08T21:45:54","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=2149"},"modified":"2017-03-08T21:45:54","modified_gmt":"2017-03-08T21:45:54","slug":"windows-powershell-scripting","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/03\/08\/windows-powershell-scripting\/","title":{"rendered":"Windows powershell scripting"},"content":{"rendered":"<p>Powershell scripting is not too difficult to pick up and there is a wealth of examples on the internet to help solve any syntax related question.<\/p>\n<p>Simply call the appropriate Powershell &#8220;cmdlet&#8221; to perform the required task. \u00a0The return value can be manipulated directly without being forced to assign intermediate values to a temporary variable. \u00a0It is easy enough to manipulate or format the return value.<\/p>\n<pre>$datestr = (get-date).tostring('yyyyMMdd')\r\n<\/pre>\n<p>This example uses the get-date cmdlet to get the current date and format it into reasonably friendly way.<\/p>\n<h2>Functions<\/h2>\n<p>Just like any proper language it is possible to create functions to assist in breaking up program logic.<\/p>\n<p>Powershell has a number of primitives that can be used for creating functions or other program logic. \u00a0This list of supported primitive is actually fairly large but here is a smaller subset which covers the usual\u00a0suspects.INT<\/p>\n<ul>\n<li>BOOL<\/li>\n<li>FLOAT<\/li>\n<li>DOUBLE<\/li>\n<li>STRING<\/li>\n<\/ul>\n<p>Microsoft has taken a route less traveled with respect to how they define a function. \u00a0The parameter types are surrounded with square brackets. In addition, they seem to have gone back to &#8220;BASIC&#8221; and put dollar signs before each variable.<\/p>\n<pre># write out log info\r\nFunction My-Add-Function([float]$firstval,[float]$secondval) \r\n{\r\n\t$returnval = $firstval + $secondval\r\n\treturn $returnval\r\n}<\/pre>\n<p>Microsoft allows you to call your user defined functions in the way you would expect. \u00a0You simply call the function with the values being passed.<\/p>\n<pre>ie.\r\n\r\n\tmy-add-function $firstvalue $secondvalue<\/pre>\n<p>Yet it is also possible to actually pass in the parameters in any order if you simply give the name of each parameter as they are named in the function.<\/p>\n<pre>ie.\r\n\r\n\tmy-add-function -secondval $secondvalue -firstval $firstvalue\r\n<\/pre>\n<p>I don&#8217;t know of any other languages or scripts that allow you to call your own functions in this manner.\u00a0 It is interesting but I don&#8217;t see it as an important feature.<\/p>\n<p>It actually took me a while to see what was missing from the function declaration\u00a0&#8211; there is no return type. \u00a0This doesn&#8217;t mean you cannot return values from functions but how it is done is odd to say the least.<\/p>\n<pre># function GetDateNow returns todays date\r\nFunction GetDateNow \r\n{\r\n        $TimeStringNow=(get-date).tostring('yyyyMMdd')\r\n        return $TimeStringNow\r\n}<\/pre>\n<p>Basically all output that occurs in the function is returned. \u00a0Looking at this method you would think that the variable TimeStringNow is the only thing being returned and it is because of the return keyword. \u00a0This is not the case.<\/p>\n<pre># function GetDateNow returns todays date\r\nFunction GetDateNow \r\n{\r\n        $TimeStringNow=(get-date).tostring('yyyyMMdd')\r\n        $TimeStringNow\r\n}<\/pre>\n<p>This second function declaration is semantically identical to the first. \u00a0The return keyword simply exists so it is possible to exit a function at a specific point, it has no connection with the values being returned.<\/p>\n<p>The one part about the return values that I didn&#8217;t mention is that everything that is output in the function (no matter when) will be returned to the caller.<\/p>\n<p>I actually do not like how data is returned from a function but I cannot say that this is completely unique. \u00a0This is similar to how Unix shell scripting works if you call a function while capturing the output from that function to a return variable.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/bash\r\nfunction unixfunction \r\n{\r\necho &quot;here at point 1&quot;\r\necho &quot;here at point 2&quot;\r\necho &quot;here at point 3&quot;\r\necho returning 6\r\necho 6\r\n}\r\n\r\nretval=`unixfunction`\r\necho $retval\r\n<\/pre>\n<p>The output looks like this.<\/p>\n<pre>here at point 1 here at point 2 here at point 3 returning 6 6<\/pre>\n<p>I don&#8217;t really like Powershell scripting but that might just be an anti-Microsoft bias. Using Powershell is actually miles better than using batch scripts from all those years ago. It is really just a matter of taste, if you are a Windows aficionado this is not only a good choice but perhaps the best choice as it comes standard with Windows.<\/p>\n<p>Does it suck? \u00a0Well, I guess not. \u00a0If you look at this from the perspective of replacing the cmd.exe with Powershell you are probably trading up. \u00a0If you want more command line programs that you string together then in my opinion it isn&#8217;t really an improvement.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Powershell scripting is not too difficult to pick up and there is a wealth of examples on the internet to help solve any syntax related question. Simply call the appropriate Powershell &#8220;cmdlet&#8221; to perform the required task. \u00a0The return value &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/03\/08\/windows-powershell-scripting\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[39,71],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2149"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=2149"}],"version-history":[{"count":8,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2149\/revisions"}],"predecessor-version":[{"id":2186,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2149\/revisions\/2186"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=2149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=2149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=2149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}