powershell 使用PowerShell语法查询WMI

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 使用PowerShell语法查询WMI相关的知识,希望对你有一定的参考价值。

function Get-WmiPowerShell {
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory)]$Class,
        [Parameter(Mandatory)]$Filter
    )
    $errors = $tokens = $null
    $AST= [Management.Automation.Language.Parser]::ParseInput($filter.ToString().Replace('$null','NULL'), [ref]$tokens, [ref]$errors)
    $tokens = $tokens | where { $_.Text -and $_.Name -ne '_' -and $_.Kind -ne 'Dot' }
    $htReplacements=@'
eq = =
lt = <
gt = >
le = <=
ge = >=
ne = !=
like = like
and = and
or = or
is = is
isnot = is not
'@ | ConvertFrom-StringData
    $wql = foreach ($token in $tokens) {
        switch($token){
            { $_.Value -ne $null }                             { "'$(([Management.Automation.WildcardPattern]$_.Value).ToWql())'"; break }
            { $_.Kind -eq 'Parameter' }                        { $htReplacements."$($_.ParameterName)"; break }
            { [string]$_.TokenFlags -like '*BinaryOperator*' } { $htReplacements."$($_.Text.Replace('-',''))"; break }
            { $_.Kind -eq 'Variable' }                         { "'$(Get-Variable $_.Name -ValueOnly)'"; break }
            Default                                            { $_.Text }
        }
    } 
    $PSBoundParameters.Filter = $wql -join ' '
    Get-WmiObject @PSBoundParameters
}

以上是关于powershell 使用PowerShell语法查询WMI的主要内容,如果未能解决你的问题,请参考以下文章

PowerShell 语法

用于验证 sql 语法错误的 Powershell 脚本

powershell 概念证明:使用Select-Object计算属性的简化语法

Powershell算术运算符

PowerShell Windows窗体包装器PowerShell表单控件包装器

在Python中运行Powershell命令(SyntaxError:无效语法)