Skip to content

Commit 04a424f

Browse files
author
Kapil Borle
committed
Fix error caused by casting Hashtable to DiagnosticRecord
The error was caused by Hashtables being cast to DiagnosticRecord arrays. This commit fixes these errors caused when Pester is invoked on CustomizedRule.Tests.ps1 Consider the following example. $result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help"; "Extent" = $ast.Extent; "RuleName" = $PSCmdlet.MyInvocation.InvocationName; "Severity" = "Warning"} In PowerShell v4, this gives the following error. Cannot convert value "System.Collections.Hashtable" to type "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]". Error: "Cannot create object of type "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord". "Extent" is a ReadOnly property." At line:1 char:1 + $result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [], RuntimeException + FullyQualifiedErrorId : InvalidCastExceptionScalarToArray
1 parent 9b2548d commit 04a424f

File tree

6 files changed

+116
-169
lines changed

6 files changed

+116
-169
lines changed

Tests/Engine/CommunityAnalyzerRules/CommunityAnalyzerRules.psm1

Lines changed: 91 additions & 93 deletions
Large diffs are not rendered by default.

Tests/Engine/SampleRuleWithVersion/SampleRuleWithVersion/1.0.0.0/SampleRuleWithVersion.psm1

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,10 @@ function Measure-RequiresRunAsAdministrator
2828
[System.Management.Automation.Language.ScriptBlockAst]
2929
$testAst
3030
)
31+
$dr = New-Object `
32+
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
33+
-ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null
3134

32-
33-
$results = @()
34-
35-
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
36-
"Extent" = $ast.Extent;
37-
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
38-
"Severity" = "Warning"}
39-
40-
$results += $result
41-
42-
43-
return $results
44-
45-
35+
return @($dr)
4636
}
4737
Export-ModuleMember -Function Measure*

Tests/Engine/samplerule/samplerule.psm1

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
.SYNOPSIS
55
Uses #Requires -RunAsAdministrator instead of your own methods.
66
.DESCRIPTION
7-
The #Requires statement prevents a script from running unless the Windows PowerShell version, modules, snap-ins, and module and snap-in version prerequisites are met.
8-
From Windows PowerShell 4.0, the #Requires statement let script developers require that sessions be run with elevated user rights (run as Administrator).
7+
The #Requires statement prevents a script from running unless the Windows PowerShell version, modules, snap-ins, and module and snap-in version prerequisites are met.
8+
From Windows PowerShell 4.0, the #Requires statement let script developers require that sessions be run with elevated user rights (run as Administrator).
99
Script developers does not need to write their own methods any more.
1010
To fix a violation of this rule, please consider to use #Requires -RunAsAdministrator instead of your own methods.
1111
.EXAMPLE
@@ -27,21 +27,10 @@ function Measure-RequiresRunAsAdministrator
2727
[ValidateNotNullOrEmpty()]
2828
[System.Management.Automation.Language.ScriptBlockAst]
2929
$testAst
30-
)
31-
32-
33-
$results = @()
34-
35-
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
36-
"Extent" = $ast.Extent;
37-
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
38-
"Severity" = "Warning"}
39-
40-
$results += $result
41-
42-
43-
return $results
44-
45-
30+
)
31+
$dr = New-Object `
32+
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
33+
-ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null
34+
return @($dr)
4635
}
4736
Export-ModuleMember -Function Measure*

Tests/Engine/samplerule/samplerule1.psm1

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,11 @@ function Measure-RequiresRunAsAdministrator
2727
[ValidateNotNullOrEmpty()]
2828
[System.Management.Automation.Language.ScriptBlockAst]
2929
$testAst
30-
)
31-
32-
33-
$results = @()
34-
35-
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
36-
"Extent" = $ast.Extent;
37-
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
38-
"Severity" = "Warning"}
39-
40-
$results += $result
41-
42-
43-
return $results
44-
45-
30+
)
31+
$dr = New-Object `
32+
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
33+
-ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null
34+
35+
return @($dr)
4636
}
4737
Export-ModuleMember -Function Measure*

Tests/Engine/samplerule/samplerule2/samplerule2.psm1

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,10 @@ function Measure-RequiresRunAsAdministrator
2828
[System.Management.Automation.Language.ScriptBlockAst]
2929
$testAst
3030
)
31+
$dr = New-Object `
32+
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
33+
-ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null
3134

32-
33-
$results = @()
34-
35-
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
36-
"Extent" = $ast.Extent;
37-
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
38-
"Severity" = "Warning"}
39-
40-
$results += $result
41-
42-
43-
return $results
44-
45-
35+
return @($dr)
4636
}
4737
Export-ModuleMember -Function Measure*

Tests/Engine/samplerule/samplerule2/samplerule3/samplerule3.psm1

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,10 @@ function Measure-RequiresRunAsAdministrator
2828
[System.Management.Automation.Language.ScriptBlockAst]
2929
$testAst
3030
)
31+
$dr = New-Object `
32+
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
33+
-ArgumentList "This is help",$ast.Extent,$PSCmdlet.MyInvocation.InvocationName,Warning,$null
3134

32-
33-
$results = @()
34-
35-
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "this is help";
36-
"Extent" = $ast.Extent;
37-
"RuleName" = $PSCmdlet.MyInvocation.InvocationName;
38-
"Severity" = "Warning"}
39-
40-
$results += $result
41-
42-
43-
return $results
44-
45-
35+
return @($dr)
4636
}
4737
Export-ModuleMember -Function Measure*

0 commit comments

Comments
 (0)