Last active
April 1, 2019 13:22
-
-
Save rikwatson/494cc6d740ca6246268d3fb8721105b0 to your computer and use it in GitHub Desktop.
Approve a Powershell script for deployment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Approve-PowershellScripts.ps1 | |
# | |
# requires -version 3 | |
$here = Split-Path -Parent $MyInvocation.MyCommand.Path | |
$powershellFiles = Get-ChildItem -Recurse $here -Include '*.ps*' | |
$nonTestFiles = $powershellFiles | Where-Object { -Not ($_.Name.EndsWith('.Tests.ps1')) } | |
$testFiles = $powershellFiles | Where-Object { $_.Name.EndsWith('.Tests.ps1') } | |
$functions = Get-ChildItem -Recurse $here -Include *.ps1 | Where-Object { $_ -notmatch ".Tests.ps1" } | |
Describe "Azure Profile" { | |
Context "Meet Style Guidelines" { | |
It "Contains copyright header" { | |
$powershellFiles | ForEach-Object { | |
$_ | Should contain '# Copyright \(c\) 20[0-9]{2}, 20[0-9]{2} _COMPANY_ Ltd. <contact@_COMPANY_.com> All rights reserved\. No warranty, explicit or implicit, provided.' | |
} | |
} | |
It "Contains version header" { | |
$powershellFiles | ForEach-Object { | |
$_ | Should contain '# requires -version [0-5]' | |
} | |
} | |
It "Contains .SYNOPSIS Help Text" { | |
$functions | ForEach-Object { | |
$_ | Should contain '\.SYNOPSIS' | |
} | |
} | |
It "Contains .DESCRIPTION Help Text" { | |
$functions | ForEach-Object { | |
$_ | Should contain '\.DESCRIPTION' | |
} | |
} | |
It "Contains .EXAMPLE Help Text" { | |
$functions | ForEach-Object { | |
$_ | Should contain '\.EXAMPLE' | |
} | |
} | |
It "Contains .OUTPUTS Help Text" { | |
$functions | ForEach-Object { | |
$_ | Should contain '\.OUTPUTS' | |
} | |
} | |
It "Contains .COMPONENT Help Text" { | |
$functions | ForEach-Object { | |
$_ | Should contain '\.COMPONENT' | |
} | |
} | |
It "Has No ScriptCop Issues" { | |
if ( -Not (Get-Module -Name ScriptCop)) | |
{ | |
return | |
} | |
$functions | ForEach-Object { . $_.FullName } | |
$commands = $functions.Name.Replace('.ps1', [String]::Empty) | Get-Command | |
$results = $commands | Test-Command -ExcludedRule 'Test-CommandNamingConvention','Test-Help' | |
if (($results | Measure-Object).Count -gt 0) { | |
$results | Out-File "$here\ScriptCopResults.tmp"; | |
} | |
($results | Measure-Object).Count | Should be 0 | |
} | |
It "Uses Approved Verbs for Exported Functions" { | |
$functions | ForEach-Object { . $_.FullName } | |
$commands = $functions.Name.Replace('.ps1', [String]::Empty) | Get-Command | |
$approvedVerbs = (Get-Verb).Verb | |
$myVerbs = $commands.Verb | Where-Object { -Not $_.StartsWith("_") } | |
$results = $myVerbs | ForEach-Object { $approvedVerbs -contains $_ } | |
$results | ForEach-Object { $_ | Should be $true } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment