WQL——用於PowerShell中獲取WMI對象的查詢語言

WQL 語言

參照: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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章