Powershell 如何使用系统、管理、自动化、语言。。。ast
因此,我正在尝试为office创建一个自定义的PsAnalyzer规则,这是我第一次使用基于Powershell 如何使用系统、管理、自动化、语言。。。ast,powershell,Powershell,因此,我正在尝试为office创建一个自定义的PsAnalyzer规则,这是我第一次使用基于$ast的命令/变量。结果我有点不知所措 我一直在使用几个站点了解[System.Management.Automation.Language]对象类,即和 出于测试目的,我正在使用下面的函数——看起来很诡异的参数 Function IE { [cmdletbinding()] Param( $Url, $IEWIDTH = 550, $IeHeight = 450 )
$ast
的命令/变量。结果我有点不知所措
我一直在使用几个站点了解[System.Management.Automation.Language]
对象类,即和
出于测试目的,我正在使用下面的函数——看起来很诡异的参数
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-*
你知道我做错了什么吗?我找到了另一个网站,如果我使用这种方法一次测试单个变量,我会得到我期望看到的结果
为此,在函数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
与
AssignmentStatementAst
具有左
和右
(-hand)值属性不同,ParameterAst
具有名称
和默认值
属性,因此您需要在谓词块中使用名称
:
$predicate={
# ...
if($Ast-is[System.Management.Automation.Language.ParameterAst]){
[System.Management.Automation.Language.ParameterAst]$VariableList=$Ast
if($VariableAst.Name.VariablePath.UserPath-eq'i'){
$ReturnValue=$false
}
elseif($VariableList.Name.VariablePath.UserPath.Length-等式3){
$ReturnValue=$false
}
elseif($VariableAst.Name.VariablePath.UserPath-cnotmatch'^([A-Z][A-Z]+)+$){
$ReturnValue=$True
}
}
# ...
}
或者,翻转谓词逻辑以搜索父节点是AssignmentStatementAst
或ParameterAst
之一的VariableExpressionAst
:
$predicate={
参数($ast)
$ValidParents=@(
[系统.管理.自动化.语言.参数]
[系统.管理.自动化.语言.分配语句AST]
)
if($ast-is[VariableExpressionAst]-和$ValidParents.Where({$ast-is$},'First')){
[System.Management.Automation.Language.VariableExpressionList]$VariableList=$ast
#在此处检查$VariableList.VariablePath
}
返回$false
}
助理声明AST
!=<代码>参数设置$VariableAst.Left
->$VariableAst.Name
非常感谢@Mathias。工作很愉快。你是从网上资源还是从反复试验中发现的?我发现这个对象类对我很有帮助,所以如果你知道任何在线资源,你会帮上大忙的。还是谢谢你。他真的帮助了我out@GrahamJordan不客气!虽然我强烈鼓励使用Get Member
,也许还有:),但大多数情况下只是尝试和错误
$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