{"id":81905,"date":"2017-01-31T00:01:25","date_gmt":"2017-01-31T08:01:25","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=81905"},"modified":"2019-02-18T09:10:14","modified_gmt":"2019-02-18T16:10:14","slug":"psscriptanalyzer-deep-dive-part-1-of-4","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/psscriptanalyzer-deep-dive-part-1-of-4\/","title":{"rendered":"PSScriptAnalyzer deep dive \u2013 Part 1 of 4"},"content":{"rendered":"<p><strong>Summary<\/strong>: Thomas Rayner, Microsoft Cloud and Datacenter Management MVP, shows the basics about how to use PSScriptAnalyzer.<\/p>\n<p>Hello! I\u2019m Thomas Rayner, a Cloud and Datacenter Management Microsoft MVP, filling in for The Scripting Guy this week. You can find me on Twitter (<a target=\"_blank\" href=\"http:\/\/twitter.com\/MrThomasRayner\">@MrThomasRayner<\/a>), or posting on my blog, <a target=\"_blank\" href=\"http:\/\/workingsysadmin.com\">workingsysadmin.com<\/a>. This week, I\u2019m presenting a four-part series about how to use PSScriptAnalyzer.<\/p>\n<p>Part 1 \u2013 Getting started with PSScriptAnalyzer<\/p>\n<p><a target=\"_blank\" href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2017\/02\/01\/psscriptanalyzer-deep-dive-part-2-of-4\/\">Part 2 \u2013 Suppressing, including, excluding rules<\/a><\/p>\n<p><a target=\"_blank\" href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2017\/02\/02\/psscriptanalyzer-deep-dive-part-3-of-4\/\">Part 3 &#8211; Wrapping PSScriptAnalyzer with Pester to get formatted results<\/a><\/p>\n<p><a target=\"_blank\" href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2017\/02\/03\/psscriptanalyzer-deep-dive-part-4-of-4\/\">Part 4 \u2013 Writing custom rules<\/a><\/p>\n<p>This is Part 1, so I\u2019m going to introduce you to PSScriptAnalyzer and show you how to get started.<\/p>\n<p>PSScriptAnalyzer (or PSSA), is an open-source tool that Microsoft developed. <a target=\"_blank\" href=\"https:\/\/github.com\/powershell\/psscriptanalyzer\">Its source code is hosted on GitHub<\/a>. The purpose of PSSA is to offer a tool that checks PowerShell code against a set of rules based on best practices. It comes with a bunch of built-in rules and you can add your own.<\/p>\n<p>The default PSSA rules, which cover a wide variety of best practices, are an excellent starting place. Just to mention a few: PSSA will check your script and warn you about using aliases instead of the full name of a command, using <code>Invoke-Expression<\/code> and other dangerous cmdlets, naming your functions correctly, and a whole lot more.<\/p>\n<p>So, how do you get started? First, you need to install the PSScriptAnalyzer module.<\/p>\n<p style=\"padding-left: 30px\"><code>Install-Module -Name PSScriptAnalyzer -Scope CurrentUser<\/code><\/p>\n<p>You can verify that the PSSA module was installed by running the following command.<\/p>\n<p style=\"padding-left: 30px\"><code>Get-Module -Name PSScriptAnalyzer -ListAvailable<\/code><\/p>\n<p>And you\u2019ll get output like this:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-013117.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-013117.png\" alt=\"Verification that the PSSA module was installed\" width=\"553\" height=\"115\" class=\"alignnone size-full wp-image-81906\" \/><\/a><\/p>\n<p>Now, you just need to import the module so you can use it.<\/p>\n<p style=\"padding-left: 30px\"><code>Import-Module -Name PSScriptAnalyzer<\/code><\/p>\n<p>Like with any module, you can easily explore all the commands that it brings into your session.<\/p>\n<p style=\"padding-left: 30px\"><code>Get-Command -Module PSScriptAnalyzer<\/code><\/p>\n<p>There are only two: <code>Get-ScriptAnalyzerRule<\/code> and <code>Invoke-ScriptAnalyzer<\/code>. We\u2019re going to look at Get-ScriptAnalyzerRule another time.<\/p>\n<p>Now you are ready to run your first test. It\u2019s as easy as running Invoke-ScriptAnalyzer and specifying a path to a script. In this example, I have a script named \u201cMyScript.ps1\u201d whose contents are as follows.<\/p>\n<p style=\"padding-left: 30px\"><code>param (<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>$Path,\n$DaysOld<\/code>\n<code>)<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$someVar = $null<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>Write-Host \"Counting items...\"<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$itemCount = (gci $Path | ? { $_.LastWriteTime -gt (Get-Date).AddDays(-$DaysOld)}).Count<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>Write-Host \"There are $itemCount items\"<\/code><\/p>\n<p>The script takes a path to a directory and gets all the items that have been written more than the specified number of days ago. You might be able to tell that there are some problems with this script already, just by looking at it. Let\u2019s see what PSSA says.<\/p>\n<p>I can run <code>Invoke-ScriptAnalyzer<\/code> on my script\u2026<\/p>\n<p style=\"padding-left: 30px\"><code>Invoke-ScriptAnalyzer -Path .\\MyScript.ps1<\/code><\/p>\n<p>\u2026 and see that there are some glaring violation of best practices.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-013117.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-013117.png\" alt=\"Result of running Invoke-ScriptAnalyzer -Path .\\MyScript.ps1\" width=\"623\" height=\"214\" class=\"alignnone size-full wp-image-81915\" \/><\/a><\/p>\n<p>As you can tell, there\u2019s a bunch of problems here. It looks like my script violates the following rules:<\/p>\n<ul>\n<li>PSUseDeclaredVarsMoreThanAssignment<\/li>\n<li>PSAvoidUsingWriteHost<\/li>\n<li>PSAvoidUsingCmdletAliases<\/li>\n<\/ul>\n<p>PSSA even tells you the line of your script where the violation occurs and provides a more detailed descripton of the rule violation. For example, on line 6, I declared <code>$someVar<\/code> but never used it in the rest of my script.<\/p>\n<p>Using this feedback, I change my script to make it is faster, more efficient, easier for other people to understand, and generally more conforming to PowerShell coding best practices.<\/p>\n<p>I should mention that sometimes you have a valid reason for breaking a PSSA rule, and they aren\u2019t all perfect. It\u2019s up to you to know your work well enough to interpret the PSSA results and decide whether they are valid. We\u2019re going to talk more about that tomorrow.<\/p>\n<p>Thanks Thomas!\u00a0 Looking forward to tomorrow\u2019s read!<\/p>\n<p>I invite you to follow the Scripting Guys on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to them at <a target=\"_blank\" href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow.<\/p>\n<p>Until then, always remember that with Great PowerShell comes Great Responsibility.<\/p>\n<p><strong>Sean Kearney<\/strong>\nHonorary Scripting Guy\nCloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Thomas Rayner, Microsoft Cloud and Datacenter Management MVP, shows the basics about how to use PSScriptAnalyzer. Hello! I\u2019m Thomas Rayner, a Cloud and Datacenter Management Microsoft MVP, filling in for The Scripting Guy this week. You can find me on Twitter (@MrThomasRayner), or posting on my blog, workingsysadmin.com. This week, I\u2019m presenting a four-part [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568,641],"tags":[56,652,45],"class_list":["post-81905","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-windows-powershell","tag-guest-blogger","tag-thomas-rayner","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Thomas Rayner, Microsoft Cloud and Datacenter Management MVP, shows the basics about how to use PSScriptAnalyzer. Hello! I\u2019m Thomas Rayner, a Cloud and Datacenter Management Microsoft MVP, filling in for The Scripting Guy this week. You can find me on Twitter (@MrThomasRayner), or posting on my blog, workingsysadmin.com. This week, I\u2019m presenting a four-part [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/81905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=81905"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/81905\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=81905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=81905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=81905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}