如何使用System.管理.自动化语言ast



所以我正在尝试为办公室创建一个自定义的PsAnalyzer规则,这是我第一次使用基于$ast的命令/变量。结果我有点不知所措。

我一直在使用几个站点来了解[System.Management.Automation.Language]对象类,即this、this和this。

出于测试目的,我使用了下面的函数——看起来不可靠的参数。

Function IE { 
[cmdletbinding()]
Param( 
$Url,
$IEWIDTH  = 550,
$IeHeight = 450 
)

$IE = new-object -comobject InternetExplorer.Application
$IE.top  = 200 ; $IE.width      = $IEWIDTH ; $IE.height  = $IEHEIGHT
$IE.Left = 100 ; $IE.AddressBar = $FALSE   ; $IE.visible = $TRUE
$IE.navigate( "file:///$Url" )
}

然后使用下面的代码,我希望$IEWIDTH是唯一失败的参数。

Function Measure-PascalCase {
<#
.SYNOPSIS
The variables names should be in PascalCase.
.DESCRIPTION
Variable names should use a consistent capitalization style, i.e. : PascalCase.
.EXAMPLE
Measure-PascalCase -ScriptBlockAst $ScriptBlockAst
.INPUTS
[System.Management.Automation.Language.ScriptBlockAst]
.OUTPUTS
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]]
.NOTES
https://msdn.microsoft.com/en-us/library/dd878270(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/ms229043(v=vs.110).aspx
https://mathieubuisson.github.io/create-custom-rule-psscriptanalyzer/
#>
[CmdletBinding()]
[OutputType([Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]])]
Param
(
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.Language.ScriptBlockAst]
$ScriptBlockAst
)
Process {
$Results = @()
try {
#region Define predicates to find ASTs.
[ScriptBlock]$Predicate = {
Param ([System.Management.Automation.Language.Ast]$Ast)
[bool]$ReturnValue = $false
if ( $Ast -is [System.Management.Automation.Language.ParameterAst] ){
[System.Management.Automation.Language.ParameterAst]$VariableAst = $Ast
if ( $VariableAst.Left.VariablePath.UserPath -eq 'i' ){
$ReturnValue = $false
} elseif ( $VariableAst.Left.VariablePath.UserPath.Length -eq 3 ){
$ReturnValue = $false
} elseif ($VariableAst.Left.VariablePath.UserPath -cnotmatch '^([A-Z][a-z]+)+$') {
$ReturnValue = $True
}
}
return $ReturnValue
}
#endregion
#region Finds ASTs that match the predicates.
[System.Management.Automation.Language.Ast[]]$Violations = $ScriptBlockAst.FindAll($Predicate, $True)
If ($Violations.Count -ne 0) {
Foreach ($Violation in $Violations) {
$Result = New-Object `
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
-ArgumentList "$((Get-Help $MyInvocation.MyCommand.Name).Description.Text)",$Violation.Extent,$PSCmdlet.MyInvocation.InvocationName,Information,$Null
$Results += $Result
}
}
return $Results
#endregion
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
}
}
Export-ModuleMember -Function Measure-*

相反,我得到了:

Line Extent          Message                                                                        
---- ------          -------                                                                        
6 $Url            Variable names should use a consistent capitalization style, i.e. : PascalCase.
7 $IEWIDTH  = 550 Variable names should use a consistent capitalization style, i.e. : PascalCase.
8 $IeHeight = 450 Variable names should use a consistent capitalization style, i.e. : PascalCase.
6 $Url            Variable names should use a consistent capitalization style, i.e. : PascalCase.
7 $IEWIDTH  = 550 Variable names should use a consistent capitalization style, i.e. : PascalCase.
8 $IeHeight = 450 Variable names should use a consistent capitalization style, i.e. : PascalCase.

你知道我做错了什么吗?我在这里找到了另一个网站,如果我用这种方法一次测试单个变量,我会得到我期望看到的结果。

要做到这一点,请在函数IE、的末尾添加以下行

[System.Management.Automation.Language.Parser]::ParseInput($MyInvocation.MyCommand.ScriptContents, [ref]$null, [ref]$null)

然后下面的代码会给你长度数字。

$stuffast = .FunctionIe.ps1
$left = $Stuffast.FindAll({$args[0] -is [System.Management.Automation.Language.AssignmentStatementAst]},$true) 
$left[0].Left.Extent.Text.Length
$left[0].Left.VariablePath.UserPath.Length

与具有LeftRight(-hand(值属性的AssignmentStatementAst不同,ParameterAst具有NameDefaultValue属性,因此您希望在谓词块中使用Name

$predicate = {
# ...
if ( $Ast -is [System.Management.Automation.Language.ParameterAst] ) {
[System.Management.Automation.Language.ParameterAst]$VariableAst = $Ast
if ( $VariableAst.Name.VariablePath.UserPath -eq 'i' ) {
$ReturnValue = $false
}
elseif ( $VariableAst.Name.VariablePath.UserPath.Length -eq 3 ) {
$ReturnValue = $false
}
elseif ($VariableAst.Name.VariablePath.UserPath -cnotmatch '^([A-Z][a-z]+)+$') {
$ReturnValue = $True
}
}
# ...
}

或者,翻转谓词逻辑来搜索VariableExpressionAst,其中父节点是AssignmentStatementAstParameterAst:之一

$predicate = {
param($ast)
$ValidParents = @(
[System.Management.Automation.Language.ParameterAst]
[System.Management.Automation.Language.AssignmentStatementAst]
)
if($ast -is [VariableExpressionAst] -and $ValidParents.Where({$ast -is $_}, 'First')){
[System.Management.Automation.Language.VariableExpressionAst]$variableAst = $ast
# inspect $variableAst.VariablePath here
}
return $false
}

最新更新