Skip to content

Commit b872bda

Browse files
author
James Brundage
committed
Adding ValidateScriptBlock (#93)
1 parent 511efa4 commit b872bda

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<#
2+
.SYNOPSIS
3+
Validates Script Blocks
4+
.DESCRIPTION
5+
Validates Script Blocks for a number of common scenarios.
6+
.EXAMPLE
7+
{
8+
param(
9+
[ValidateScriptBlock(Safe)]
10+
[ScriptBlock]
11+
$ScriptBlock
12+
)
13+
14+
$ScriptBlock
15+
} | .>PipeScript
16+
.EXAMPLE
17+
{
18+
param(
19+
[ValidateScriptBlock(NoBlock,NoParameters)]
20+
[ScriptBlock]
21+
$ScriptBlock
22+
)
23+
24+
$ScriptBlock
25+
} | .>PipeScript
26+
.EXAMPLE
27+
{
28+
param(
29+
[ValidateScriptBlock(OnlyParameters)]
30+
[ScriptBlock]
31+
$ScriptBlock
32+
)
33+
34+
$ScriptBlock
35+
} | .>PipeScript
36+
#>
37+
[CmdletBinding(DefaultParameterSetName='Parameter')]
38+
param(
39+
# If set, will validate that ScriptBlock is "safe".
40+
# This will attempt to recreate the Script Block as a datalanguage block and execute it.
41+
[Alias('Safe')]
42+
[switch]
43+
$DataLanguage,
44+
45+
# If set, will ensure that the [ScriptBlock] only has parameters
46+
[Alias('OnlyParameters')]
47+
[switch]
48+
$ParameterOnly,
49+
50+
# If set, will ensure that the [ScriptBlock] has no named blocks.
51+
[Alias('NoBlocks')]
52+
[switch]
53+
$NoBlock,
54+
55+
# If set, will ensure that the [ScriptBlock] has no parameters.
56+
[Alias('NoParameters','NoParam')]
57+
[switch]
58+
$NoParameter,
59+
60+
# A VariableExpression. If provided, the Validation attributes will apply to this variable.
61+
[Parameter(Mandatory,ValueFromPipeline,ParameterSetName='VariableExpressionAST')]
62+
[Management.Automation.Language.VariableExpressionAST]
63+
$VariableAST
64+
)
65+
66+
process {
67+
68+
$validateScripts = @(
69+
if ($DataLanguage) {
70+
@'
71+
[ValidateScript({
72+
$sbCopy = "data { $_ }"
73+
try {
74+
$dataOutput = & ([ScriptBlock]::Create($sbCopy))
75+
return $true
76+
} catch {
77+
throw
78+
}
79+
})]
80+
'@
81+
}
82+
if ($ParameterOnly) {
83+
@'
84+
[ValidateScript({
85+
$statementCount = 0
86+
$statementCount += $_.Ast.DynamicParamBlock.Statements.Count
87+
$statementCount += $_.Ast.BeginBlock.Statements.Count
88+
$statementCount += $_.Ast.ProcessBlock.Statements.Count
89+
$statementCount += $_.Ast.EndBlock.Statements.Count
90+
if ($statementCount) {
91+
throw "ScriptBlock should have no statements"
92+
} else {
93+
return $true
94+
}
95+
})]
96+
'@
97+
}
98+
if ($NoBlock) {
99+
@'
100+
[ValidateScript({
101+
if ($_.Ast.DynamicParamBlock -or $_.Ast.BeginBlock -or $_.Ast.ProcessBlock) {
102+
throw "ScriptBlock should not have any named blocks"
103+
}
104+
return $true
105+
})]
106+
'@
107+
}
108+
if ($NoParameter) {
109+
@'
110+
[ValidateScript({
111+
if ($_.Ast.ParamBlock.Parameters.Count) {
112+
throw "ScriptBlock should not have parameters"
113+
}
114+
return $true
115+
})]
116+
'@
117+
}
118+
)
119+
if (-not $validateScripts) { return }
120+
121+
[scriptblock]::Create(@"
122+
$($validateScripts -join [Environment]::NewLine)$(
123+
if ($psCmdlet.ParameterSetName -eq 'Parameter') {
124+
'param()'
125+
} else {
126+
'$' + $VariableAST.variablePath.ToString()
127+
}
128+
)
129+
"@.Trim())
130+
131+
132+
}

0 commit comments

Comments
 (0)