Get-WmiObject 命令使用
文章目录
- 了解 Get-WmiObject
- WMI介绍和使用
- Get-WmiObject 基本语法
- 演示
了解 Get-WmiObject
Get-WmiObject 是 PowerShell 中用于通过 Windows Management Instrumentation (WMI) 获取系统信息的命令。它允许用户查询和管理系统中的各种资源,如硬件、软件、服务等。
WMI介绍和使用
Windows Management Instrumentation (WMI) 是基于 Windows 的操作系统上管理数据和操作的基础结构。用户可以使用 WMI 管理本地和远程计算机,默认情况下,无须安装就可以使用。
某些Provider(提供程序)的安装会由系统配置而定。查看 WMI 对象内容
- 打开 Powershell
2)输入命令
# Get-WmiObject [WMI Provider]
Get-WmiObject WIn32_KeyBoard
执行后结果如下:
Get-WmiObject 基本语法
Get-WmiObject 的基本语法如下:
Get-WmiObject [-Class] <String> [[-Property] <String[]>] [-Filter <String>] [-Amended] [-DirectRead] [-AsJob] [-Impersonation <ImpersonationLevel>] [-Authentication <AuthenticationLevel>] [-Locale <String>] [-EnableAllPrivileges] [-Authority <String>] [-Credential <PSCredential>] [-ThrottleLimit <Int32>] [-ComputerName <String[]>] [-Namespace <String>] [<CommonParameters>]
常用参数解释
-Class:指定要查询的 WMI 类的名称。
-Property:指定要获取的属性。
-Filter:指定查询条件。
-Amended:获取经过修改的实例。
-DirectRead:直接读取数据,不经过缓存。
-AsJob:以作业形式运行命令。
-Impersonation:指定模拟级别。
-Authentication:指定身份验证级别。
-Locale:指定区域设置。
-EnableAllPrivileges:启用所有权限。
-Authority:指定安全权限提供者。
-Credential:指定凭据。
-ThrottleLimit:限制并发操作的数量。
-ComputerName:指定远程计算机名称。
-Namespace:指定命名空间。
演示
使用 Get-WmiObject 监视 CPU 使用率的示例代码:
$cpuInfo = Get-WmiObject -Class Win32_Processor
$cpuUsage = Get-WmiObject -Class Win32_PerfFormattedData_PerfOS_Processor | Where-Object {$_.Name -eq "_Total"} | Select-Object -ExpandProperty PercentProcessorTime
Write-Host "CPU信息:"
Write-Host "制造商:" $cpuInfo.Manufacturer
Write-Host "型号:" $cpuInfo.Name
Write-Host "核心数:" $cpuInfo.NumberOfCores
Write-Host "线程数:" $cpuInfo.NumberOfLogicalProcessors
Write-Host "CPU使用率:" $cpuUsage "%"