您的位置:首页 > 运维架构 > Shell

WQL——用于PowerShell中获取WMI对象的查询语言

2020-12-11 18:08 1646 查看

WQL 语言

参照:(https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_wql?view=powershell-5.1)]About_WQL

1、WQL是什么?

WQL是用于获取PowerShell中的WMI(Windows Management Instrumentation)对象的WMI查询语言(WQL)。

2、为什么要用WQL语言?

​ WQL 查询比标准 Get-WmiObject 命令要快一些,而且在数百个系统上运行命令时,性能得到了改善。

3、如何引用WQL语句?

WQL查询语句可以接在“Get-WmiObject”和“Get-CimInstance”后使用,结构如下

Get-WmiObject -Query "<WQL Query> "

Get-CimInstance -Query "<WQL Query>"

WQL查询语句的基本结构:

Select <property> from <WMI-class> [where <property> <operator> <value>]

例子:查询Notepad进程的详细信息

使用Get-WmiObject

Get-WmiObject -Query {Select * from Win32_Process where Name = 'Notepad.exe'}

命令输出:

__GENUS                    : 2
__CLASS                    : Win32_Process
__SUPERCLASS               : CIM_Process
__DYNASTY                  : CIM_ManagedSystemElement
__RELPATH                  : Win32_Process.Handle="5444"
__PROPERTY_COUNT           : 45
__DERIVATION               : {CIM_Process, CIM_LogicalElement, CIM_ManagedSystemElement}
__SERVER                   : SZ-Test1119
__NAMESPACE                : root\cimv2
__PATH                     : \\SZ-GADZ050761\root\cimv2:Win32_Process.Handle="5444"
Caption                    : notepad.exe
CommandLine                : "C:\WINDOWS\system32\notepad.exe"
CreationClassName          : Win32_Process
CreationDate               : 20201211175155.893933+480
CSCreationClassName        : Win32_ComputerSystem
CSName                     : SZ-Test1119
Description                : notepad.exe
ExecutablePath             : C:\WINDOWS\system32\notepad.exe
ExecutionState             :
Handle                     : 5444
HandleCount                : 238
InstallDate                :
KernelModeTime             : 781250
MaximumWorkingSetSize      : 1380
MinimumWorkingSetSize      : 200
Name                       : notepad.exe
OSCreationClassName        : Win32_OperatingSystem
OSName                     : Microsoft Windows 10 企业版|C:\WINDOWS|\Device\Harddisk0\Partition2
OtherOperationCount        : 110
OtherTransferCount         : 2584
PageFaults                 : 4035
PageFileUsage              : 3108
ParentProcessId            : 3980
PeakPageFileUsage          : 3108
PeakVirtualSize            : 2203492605952
PeakWorkingSetSize         : 15484
Priority                   : 8
PrivatePageCount           : 3182592
ProcessId                  : 5444
QuotaNonPagedPoolUsage     : 14
QuotaPagedPoolUsage        : 244
QuotaPeakNonPagedPoolUsage : 14
QuotaPeakPagedPoolUsage    : 244
ReadOperationCount         : 1
ReadTransferCount          : 60
SessionId                  : 1
Status                     :
TerminationDate            :
ThreadCount                : 7
UserModeTime               : 0
VirtualSize                : 2203492605952
WindowsVersion             : 10.0.19042
WorkingSetSize             : 15851520
WriteOperationCount        : 0
WriteTransferCount         : 0
PSComputerName             : SZ-GADZ050761
ProcessName                : notepad.exe
Handles                    : 238
VM                         : 2203492605952
WS                         : 15851520
Path                       : C:\WINDOWS\system32\notepad.exe

使用Get-CimInstance

Get-CimInstance -Query "Select * from CIM_Process where Name = 'Notepad.exe'"

命令输出:

ProcessId Name        HandleCount WorkingSetSize VirtualSize
--------- ----        ----------- -------------- -----------
5444      notepad.exe 237         15912960       2203472412672

使用 “Get-CimInstance -Query” 的时候,后面的 WQL查询语句,不要用{},需要用""括起来,否则会报错。

注:使用 { }括起来的时候被作为脚本块解析了,脚本块中的 WQL没有输入。

PS C:\> Get-CimInstance -Query {Select * from Win32_Process where Name = 'Notepad.exe'}
Get-CimInstance : 无法评估参数“Query”,因为其参数被指定为脚本块,且没有输入。无法评估没有输入的脚本块。
所在位置 行:1 字符: 24
+ ... tance -Query {Select * from Win32_Process where Name = 'Notepad.exe'}
+                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : MetadataError: (:) [Get-CimInstance], ParameterBindingException
+ FullyQualifiedErrorId : ScriptBlockArgumentNoInput,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInsta
nceCommand

附 WQL查询语言中Where语句中有效的运算符:

Operator    Description
-----------------------
=           Equal
!=          Not equal
<>          Not equal
<           Less than
>           Greater than
<=          Less than or equal
>=          Greater than or equal
LIKE        Wildcard match
IS          Evaluates null
ISNOT       Evaluates not null
ISA         Evaluates a member of a WMI class

目录:返回我的PowerShell学习笔记:https://blog.51cto.com/3chou/2562634

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐