|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Custom rule for command name. |
| 4 | + .NOTES |
| 5 | + File: CommandName.psm1 |
| 6 | +#> |
| 7 | + |
| 8 | +enum RuleNames { |
| 9 | + Invalid_Cmdlet |
| 10 | + Is_Alias |
| 11 | + Capitalization_Conventions_Violated |
| 12 | +} |
| 13 | + |
| 14 | +<# |
| 15 | + .SYNOPSIS |
| 16 | + Returns invaild, alias or unrecognized cmdlets. |
| 17 | +#> |
| 18 | +function Measure-CommandName { |
| 19 | + [CmdletBinding()] |
| 20 | + [OutputType([Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] |
| 21 | + param( |
| 22 | + [Parameter(Mandatory)] |
| 23 | + [ValidateNotNullOrEmpty()] |
| 24 | + [System.Management.Automation.Language.ScriptBlockAst] |
| 25 | + $ScriptBlockAst |
| 26 | + ) |
| 27 | + begin{ |
| 28 | + $modulePath = "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1" |
| 29 | + Get-Item $modulePath | Import-Module -Global |
| 30 | + } |
| 31 | + process { |
| 32 | + $Results = @() |
| 33 | + $global:CommandParameterPair = @() |
| 34 | + $global:Ast = $null |
| 35 | + |
| 36 | + try { |
| 37 | + [ScriptBlock]$Predicate = { |
| 38 | + param([System.Management.Automation.Language.Ast]$Ast) |
| 39 | + $global:Ast = $Ast |
| 40 | + |
| 41 | + #Find all command in .ps1 |
| 42 | + if ($Ast -is [System.Management.Automation.Language.CommandAst]) { |
| 43 | + [System.Management.Automation.Language.CommandAst]$CommandAst = $Ast |
| 44 | + # Get wrapper function name by command element |
| 45 | + $funcAst = $CommandAst |
| 46 | + while($funcAst -isnot [System.Management.Automation.Language.FunctionDefinitionAst] -and $null -ne $funcAst.Parent.Parent.Parent){ |
| 47 | + $funcAst = $funcAst.Parent |
| 48 | + } |
| 49 | + $ModuleCmdletExNum = $funcAst.name |
| 50 | + |
| 51 | + if ($CommandAst.InvocationOperator -eq "Unknown") { |
| 52 | + $CommandName = $CommandAst.CommandElements[0].Extent.Text |
| 53 | + $GetCommand = Get-Command $CommandName -ErrorAction SilentlyContinue |
| 54 | + if ($null -eq $GetCommand) { |
| 55 | + # CommandName is not valid. |
| 56 | + $global:CommandParameterPair += @{ |
| 57 | + CommandName = $CommandName |
| 58 | + ParameterName = "<is not valid>" |
| 59 | + ModuleCmdletExNum = $ModuleCmdletExNum |
| 60 | + } |
| 61 | + return $true |
| 62 | + } |
| 63 | + else { |
| 64 | + if ($GetCommand.CommandType -eq "Alias") { |
| 65 | + # CommandName is an alias. |
| 66 | + $global:CommandParameterPair += @{ |
| 67 | + CommandName = $CommandName |
| 68 | + ParameterName = "<is an alias>" |
| 69 | + ModuleCmdletExNum = $ModuleCmdletExNum |
| 70 | + } |
| 71 | + return $true |
| 72 | + } |
| 73 | + if ($CommandName -cnotmatch "^([A-Z][a-z]+)+-([A-Z][a-z0-9]*)+$") { |
| 74 | + # CommandName doesn't follow the Capitalization Conventions. |
| 75 | + $global:CommandParameterPair += @{ |
| 76 | + CommandName = $CommandName |
| 77 | + ParameterName = "<doesn't follow the Capitalization Conventions>" |
| 78 | + ModuleCmdletExNum = $ModuleCmdletExNum |
| 79 | + } |
| 80 | + return $true |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return $false |
| 87 | + } |
| 88 | + |
| 89 | + # Find all false scriptblock |
| 90 | + [System.Management.Automation.Language.Ast[]]$Asts = $ScriptBlockAst.FindAll($Predicate, $false) |
| 91 | + for ($i = 0; $i -lt $Asts.Count; $i++) { |
| 92 | + if ($global:CommandParameterPair[$i].ParameterName -eq "<is not valid>") { |
| 93 | + $Message = "$($CommandParameterPair[$i].CommandName) is not a valid command name." |
| 94 | + $RuleName = [RuleNames]::Invalid_Cmdlet |
| 95 | + $RuleSuppressionID = "5000" |
| 96 | + $Remediation = "Check the spell of $($CommandParameterPair[$i].CommandName)." |
| 97 | + $Severity = "Error" |
| 98 | + } |
| 99 | + if ($global:CommandParameterPair[$i].ParameterName -eq "<is an alias>") { |
| 100 | + $Message = "$($CommandParameterPair[$i].CommandName) is an alias of `"$((Get-Alias $CommandParameterPair[$i].CommandName)[0].ResolvedCommandName)`"." |
| 101 | + $RuleName = [RuleNames]::Is_Alias |
| 102 | + $RuleSuppressionID = "5100" |
| 103 | + $Remediation = "Use formal name `"$((Get-Alias $CommandParameterPair[$i].CommandName)[0].ResolvedCommandName)`" of the alias `"$($CommandParameterPair[$i].CommandName)`"." |
| 104 | + $Severity = "Warning" |
| 105 | + } |
| 106 | + if ($global:CommandParameterPair[$i].ParameterName -eq "<doesn't follow the Capitalization Conventions>") { |
| 107 | + $Message = "$($CommandParameterPair[$i].CommandName) doesn't follow the Capitalization Conventions." |
| 108 | + $RuleName = [RuleNames]::Capitalization_Conventions_Violated |
| 109 | + $RuleSuppressionID = "5101" |
| 110 | + $name = $($CommandParameterPair[$i].CommandName) |
| 111 | + $textInfo = (Get-Culture).TextInfo |
| 112 | + $CorrectName = $textInfo.ToTitleCase(($name -split "-")[0]) |
| 113 | + $CorrectName += "-Az" |
| 114 | + $CorrectName += $textInfo.ToTitleCase(($name -split "Az")[1]) |
| 115 | + $Remediation = "Check the Capitalization Conventions. Suggest format: $CorrectName" |
| 116 | + $Severity = "Warning" |
| 117 | + } |
| 118 | + $ModuleCmdletExNum = $($CommandParameterPair[$i].ModuleCmdletExNum) |
| 119 | + $Result = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ |
| 120 | + Message = "$ModuleCmdletExNum-@$Message@$Remediation"; |
| 121 | + Extent = $Asts[$i].Extent; |
| 122 | + RuleName = $RuleName; |
| 123 | + Severity = $Severity |
| 124 | + RuleSuppressionID = $RuleSuppressionID |
| 125 | + } |
| 126 | + $Results += $Result |
| 127 | + } |
| 128 | + return $Results |
| 129 | + } |
| 130 | + catch { |
| 131 | + $Result = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ |
| 132 | + Message = $_.Exception.Message; |
| 133 | + Extent = $global:Ast.Extent; |
| 134 | + RuleName = $PSCmdlet.MyInvocation.InvocationName; |
| 135 | + Severity = "Error" |
| 136 | + } |
| 137 | + $Results += $Result |
| 138 | + return $Results |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +Export-ModuleMember -Function Measure-* |
0 commit comments