@@ -19,6 +19,7 @@ Describe "AvoidDynamicVariableNames" {
1919 $violations.Extent.Text | Should - Be {New-Variable - Name $Test }.ToString()
2020 $violations.Message | Should - Be ($ruleMessage -f ' $Test' )
2121 }
22+
2223 It " Common dynamic variable iteration" {
2324 $scriptDefinition = {
2425 ' One' , ' Two' , ' Three' | ForEach-Object - Begin { $i = 1 } - Process {
@@ -32,6 +33,20 @@ Describe "AvoidDynamicVariableNames" {
3233 $violations.Extent.Text | Should - Be {New-Variable - Name " My$_ " - Value ($i ++ )}.ToString()
3334 $violations.Message | Should - Be ($ruleMessage -f ' My$_' )
3435 }
36+
37+ It " Set-Variable by positional parameter" {
38+ $scriptDefinition = {
39+ ' One' , ' Two' , ' Three' | ForEach-Object - Begin { $i = 1 } - Process {
40+ New-Variable " My$_ " ($i ++ )
41+ }
42+ $MyTwo # returns 2
43+ }.ToString()
44+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ($ruleName )
45+ $violations.Count | Should - Be 1
46+ $violations.Severity | Should - Be Warning
47+ $violations.Extent.Text | Should - Be {New-Variable " My$_ " ($i ++ )}.ToString()
48+ $violations.Message | Should - Be ($ruleMessage -f ' My$_' )
49+ }
3550 }
3651
3752 Context " Compliant" {
@@ -58,6 +73,15 @@ Describe "AvoidDynamicVariableNames" {
5873 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ($ruleName )
5974 $violations | Should - BeNullOrEmpty
6075 }
76+
77+ It " Verbatim (single quoted) name with dollar sign" {
78+ $scriptDefinition = {
79+ New-Variable - Name ' $Sign'
80+ Set-Variable - Name ' $Sign' - Value ' Dollar'
81+ }.ToString()
82+ $violations = Invoke-ScriptAnalyzer - ScriptDefinition $scriptDefinition - IncludeRule @ ($ruleName )
83+ $violations | Should - BeNullOrEmpty
84+ }
6185 }
6286
6387 Context " Suppressed" {
0 commit comments