{"id":12759,"date":"2019-09-08T16:24:00","date_gmt":"2019-09-08T14:24:00","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/"},"modified":"2024-09-11T15:09:56","modified_gmt":"2024-09-11T13:09:56","slug":"scripting-is-not-just-a-series-of-commands","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/","title":{"rendered":"Scripting is not Just a Series of Commands"},"content":{"rendered":"<p>I saw, several times now, scripts were it is just a series of commands, sometime just created with copy\/paste and few changes.<br \/>\nThis is far from best practices in terms of readability, maintainability and debug (ability &#x1f642;).<br \/>\n<!--more--><\/p>\n<h3>My Best Practices<\/h3>\n<h4>Use functions (with types)<\/h4>\n<p>Beside adding, the most obvious re-usability, using functions also helps for code maintainability. For instance, if a bug is found and the fix is in a function used 10 times, you will have to fix it once and not 10 times. It also makes code readability better.<\/p>\n<h4>Manage errors<\/h4>\n<p>If the scripting language you are using support exception, catch exception and use them. It is important, like for example calling a <a href=\"https:\/\/www.dbi-services.com\/blog\/use-powershell-to-access-a-webservice\/\">WebService<\/a>, to catch known and unexpected exception. For example, if an error code 500 (Internal Server Error) is raised while querying a WebService, the script will not be able to proceed any further and thus error code and message could be logged and script stopped.<br \/>\nAlso, exception can make the code easier to maintain. Assuming that piece of code:<\/p>\n<pre class=\"brush: powershell; gutter: true; first-line: 1\">\n    Try {\n        If ( $input -like 'ADM_*' ){\n            # call function with parameters\n            FUNCTION_A \"ADM\"\n        } ElseIf ( $input -like 'SYS_*' ) {\n            FUNCTION_A \"SYS\"\n        } else\n            return $false\n        } # End If ADM user\n    } Catch {\n        # manage the exception as it failed\n    }\n<\/pre>\n<p>Instead of managing errors in each If and ElseIf part, multiplying the effort, it is easier to throw an exception in FUNCTION_A that will be caught at end of If&#8217;s. With this, you can quick go out of execution stack up to the first catch encountered.<\/p>\n<h4>Debugging<\/h4>\n<p>For example, in my PowerShell scripts, I usually add this function:<\/p>\n<pre class=\"brush: powershell; gutter: true; first-line: 1\">Function Write-Log ([string] $log) {\n    $date = Get-Date -Format \"dd-MMM-yyyy HH:mm:ss.fff\"\n    If ($Global:debug) {\n        $message = '{0} ({1} ms) - {2}' -f $date, ([math]::floor($measureTime.Elapsed.TotalMilliseconds)), $log\n        $measureTime.Restart()\n    } Else {\n        $message = '{0} - {1}' -f $date, $log\n    }\n    Write-Host($message)\n    $Global:StreamWriter.WriteLine($message)\n} # End Write-Log<\/pre>\n<p>It nicely format log message with date and time and, if running in debug mode, also include time difference between each log messages. This message will be displayed in console and store in script log file. StreamWriter global variable is set at script initialization. This is the most efficient method I found to write into files.<br \/>\n<a href=\"https:\/\/code.visualstudio.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Visual Studio Code<\/a> is also not to neglect. It is developed by Microsoft, customizable via plugins and includes an excellent integrated debugger.<\/p>\n<h4>Use classes<\/h4>\n<p>In some cases, it could be interesting to use classes to hide complexity of certain object.<br \/>\nFor example, I had the need to create an Excel report based on a WebService result. Instead of managing complexity of Excel in the main function, I created:<\/p>\n<ul>\n<li>A class with a constructor that start Excel and initiate the new sheet<\/li>\n<li>A method in this class to set the columns names and width<\/li>\n<li>A method to append a line where current line number is maintained in the class<\/li>\n<\/ul>\n<h4>Comments your code<\/h4>\n<p>I am sometimes in a hurry and do not comment properly the code. This will probably lead to a waste of time if some changes of code have to be done several weeks of months later. If you don&#8217;t do it for others, do it for yourself.<\/p>\n<h3>Algorithm<\/h3>\n<p>Algorithm is an important part of coding leading to efficient (or not) scripts. Each case is different and the faster algorithm is not necessarily the first you will think of. Just look at sorting algorithm, quick sort being the fastest one.<\/p>\n<h3>And you?<\/h3>\n<p>This is a few rules or tips that I am trying to follow.<br \/>\nDo you have some others to share ?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I saw, several times now, scripts were it is just a series of commands, sometime just created with copy\/paste and few changes. This is far from best practices in terms of readability, maintainability and debug (ability &#x1f642;).<\/p>\n","protected":false},"author":26,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[368],"tags":[],"type_dbi":[],"class_list":["post-12759","post","type-post","status-publish","format-standard","hentry","category-development-performance"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Scripting is not Just a Series of Commands - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scripting is not Just a Series of Commands\" \/>\n<meta property=\"og:description\" content=\"I saw, several times now, scripts were it is just a series of commands, sometime just created with copy\/paste and few changes. This is far from best practices in terms of readability, maintainability and debug (ability &#x1f642;).\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-08T14:24:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-11T13:09:56+00:00\" \/>\n<meta name=\"author\" content=\"Microsoft Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Microsoft Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/\"},\"author\":{\"name\":\"Microsoft Team\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4\"},\"headline\":\"Scripting is not Just a Series of Commands\",\"datePublished\":\"2019-09-08T14:24:00+00:00\",\"dateModified\":\"2024-09-11T13:09:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/\"},\"wordCount\":520,\"commentCount\":0,\"articleSection\":[\"Development &amp; Performance\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/\",\"name\":\"Scripting is not Just a Series of Commands - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"datePublished\":\"2019-09-08T14:24:00+00:00\",\"dateModified\":\"2024-09-11T13:09:56+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scripting is not Just a Series of Commands\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4\",\"name\":\"Microsoft Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g\",\"caption\":\"Microsoft Team\"},\"url\":\"https:\/\/www.dbi-services.com\/blog\/author\/microsoft-team\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Scripting is not Just a Series of Commands - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/","og_locale":"en_US","og_type":"article","og_title":"Scripting is not Just a Series of Commands","og_description":"I saw, several times now, scripts were it is just a series of commands, sometime just created with copy\/paste and few changes. This is far from best practices in terms of readability, maintainability and debug (ability &#x1f642;).","og_url":"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/","og_site_name":"dbi Blog","article_published_time":"2019-09-08T14:24:00+00:00","article_modified_time":"2024-09-11T13:09:56+00:00","author":"Microsoft Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Microsoft Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/"},"author":{"name":"Microsoft Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4"},"headline":"Scripting is not Just a Series of Commands","datePublished":"2019-09-08T14:24:00+00:00","dateModified":"2024-09-11T13:09:56+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/"},"wordCount":520,"commentCount":0,"articleSection":["Development &amp; Performance"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/","url":"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/","name":"Scripting is not Just a Series of Commands - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2019-09-08T14:24:00+00:00","dateModified":"2024-09-11T13:09:56+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/scripting-is-not-just-a-series-of-commands\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Scripting is not Just a Series of Commands"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4","name":"Microsoft Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g","caption":"Microsoft Team"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/microsoft-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/12759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=12759"}],"version-history":[{"count":1,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/12759\/revisions"}],"predecessor-version":[{"id":34732,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/12759\/revisions\/34732"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=12759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=12759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=12759"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=12759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}