|
收集有关计算机的信息
Get-WmiObject 是用于执行常规系统管理任务的最重要的 cmdlet。所有关键的子系统设置都是通过
WMI 公开的。此外,WMI 将数据视为有一个或多个项目的集合中的对象。由于 Windows PowerShell
还能处理对象,并且它的管道允许您以相同方式对待单个或多个对象,因此,通用 WMI 访问可让您用非常少的工作量执行一些高级任务。
以下示例演示如何通过对任意计算机使用 Get-WmiObject 来收集特定信息。我们使用表示本地计算机的点值 (.) 来指定
ComputerName 参数。您可以指定与可以通过 WMI 访问的任何计算机关联的名称或 IP 地址。若要检索有关本地计算机的信息,可以省略
-ComputerName.
列出桌面设置
我们首先介绍用于收集本地计算机的桌面相关信息的命令。
Get-WmiObject -Class Win32_Desktop -ComputerName . 此命令将返回所有桌面的信息,无论它们是否正在使用。
注意: 某些 WMI 类返回的信息可能非常详细,并且通常包含有关 WMI 类的元数据。由于这些元数据属性的名称大多数都以双下划线开头,因此可以使用
Select-Object 筛选这些属性。请使用 [a-z]* 作为 Property
值仅指定以字母字符开头的属性。例如:
Get-WmiObject -Class Win32_Desktop -ComputerName . | Select-Object -Property [a-z]* 若要筛选元数据,请使用管道运算符 (|) 将 Get-WmiObject 命令的结果发送到 Select-Object -Property
[a-z]*。
列出 BIOS 信息
WMI Win32_BIOS 类可以返回有关本地计算机系统 BIOS 的相当精简和完整的信息:
Get-WmiObject -Class Win32_BIOS -ComputerName .列出处理器信息
可以使用 WMI 的 Win32_Processor 类检索常规处理器信息,但还可能需要筛选这些信息:
Get-WmiObject -Class Win32_Processor -ComputerName . | Select-Object -Property [a-z]* 若要获取处理器系列的一般说明字符串,只需返回 Win32_ComputerSystem SystemType 属性:
PS> Get-WmiObject -Class Win32_ComputerSystem -ComputerName . | Select-Object -Property SystemType
SystemType
----------
X86-based PC
列出计算机制造商和型号
还可从 Win32_ComputerSystem 获取计算机型号信息。标准的显示输出不需要进行任何筛选,即可提供 OEM
数据:
PS> Get-WmiObject -Class Win32_ComputerSystem
Domain : WORKGROUP
Manufacturer : Compaq Presario 06
Model : DA243A-ABA 6415cl NA910
Name : MyPC
PrimaryOwnerName : Jane Doe
TotalPhysicalMemory : 804765696
像这样的命令输出(直接从某些硬件返回信息)实际上只是您拥有的数据。某些信息未被硬件制造商正确配置,因此可能不可用。
列出已安装的修补程序
可以使用 Win32_QuickFixEngineering 列出已安装的所有修补程序:
Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName . 该类返回修补程序的列表,如下所示:
Description : Update for Windows XP (KB910437)
FixComments : Update
HotFixID : KB910437
Install Date :
InstalledBy : Administrator
InstalledOn : 12/16/2005
Name :
ServicePackInEffect : SP3
Status :
若要得到更简洁的输出,可能需要排除某些属性。虽然可以使用 Get-WmiObject
Property 参数只选择 HotFixID,但这样做实际上将返回更多信息,因为默认情况下将显示所有元数据:
PS> Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName . -Property HotFixId
HotFixID : KB910437
__GENUS : 2
__CLASS : Win32_QuickFixEngineering
__SUPERCLASS :
__DYNASTY :
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
由于 Get-WmiObject 中的 Property 参数限制从 WMI 类实例返回的属性,而不限制返回到
Windows PowerShell 的对象,因此还会返回其他数据。若要减少输出,请使用 Select-Object:
PS> Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName . -Property Hot
FixId | Select-Object -Property HotFixId
HotFixId
--------
KB910437
列出操作系统版本信息
Win32_OperatingSystem 类属性包括版本和 Service Pack
信息。可以只明确选择这些属性,以便从 Win32_OperatingSystem 获取版本信息摘要:
Get-WmiObject -Class Win32_OperatingSystem -ComputerName . | Select-Object -Property BuildNumber,BuildType,OSType,ServicePackMajorVersion,ServicePackMinorVersion 还可以在 Select-Object Property 参数中使用通配符。因为所有以 Build 或
ServicePack 开头的属性在这里都是重要的,所以可以将该命令缩短为以下形式:
PS> Get-WmiObject -Class Win32_OperatingSystem -ComputerName . | Select-Object -Property Build*,OSType,ServicePack*
BuildNumber : 2600
BuildType : Uniprocessor Free
OSType : 18
ServicePackMajorVersion : 2
ServicePackMinorVersion : 0
列出本地用户和所有者
通过选择 Win32_OperatingSystem 属性可以查找本地常规用户信息,包括许可用户数、当前用户数和所有者名称。可以显式选择要显示的属性,如下所示:
Get-WmiObject -Class Win32_OperatingSystem -ComputerName . | Select-Object -Property NumberOfLicensedUsers,NumberOfUsers,RegisteredUser 使用通配符的更简洁版本是:
Get-WmiObject -Class Win32_OperatingSystem -ComputerName . | Select-Object -Property *user*获得可用磁盘空间
若要查看本地驱动器的磁盘空间和可用空间,可以使用 WMI Win32_LogicalDisk 类。这需要只显示 DriveType 为
3(这是 WMI 为固定硬盘分配的值)的实例。
Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" -ComputerName .
DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace : 65541357568
Size : 203912880128
VolumeName : Local DiskDeviceID : Q:
DriveType : 3
ProviderName :
FreeSpace : 44298250240
Size : 122934034432
VolumeName : New Volume
PS> Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" -ComputerName . | Measure-Object -Property FreeSpace,Size -SumGet-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" -ComputerName . | Measure-Object -Property FreeSpace,Size -Sum | Select-Object -Property Property,Sum
获得登录会话信息
通过 WMI Win32_LogonSession 类可以获得与用户关联的登录会话的常规信息:
Get-WmiObject -Class Win32_LogonSession -ComputerName .获得登录到计算机的用户
使用 Win32_ComputerSystem 可以显示登录到特定计算机系统的用户。此命令只返回登录到系统桌面的用户:
Get-WmiObject -Class Win32_ComputerSystem -Property UserName -ComputerName .从计算机获得本地时间
使用 WMI Win32_LocalTime 类可以在特定计算机上检索当前本地时间。因为默认情况下此类显示所有元数据,所以可能需要使用
Select-Object 对这些数据进行筛选:
PS> Get-WmiObject -Class Win32_LocalTime -ComputerName . | Select-Object -Property [a-z]*
Day : 15
DayOfWeek : 4
Hour : 12
Milliseconds :
Minute : 11
Month : 6
Quarter : 2
Second : 52
WeekInMonth : 3
Year : 2006
显示服务状态
若要查看特定计算机上所有服务的状态,可以像前面提到的那样在本地使用 Get-Service cmdlet。对于远程系统,可以使用 WMI Win32_Service 类。如果还使用 Select-Object 来筛选
Status、Name 和 DisplayName 的结果,则输出格式将与 Get-Service 的输出几乎相同。
Get-WmiObject -Class Win32_Service -ComputerName . | Select-Object -Property Status,Name,DisplayName 若要允许完整显示名称非常长的临时服务的名称,可能需要使用带有 AutoSize 和 Wrap 参数的
Format-Table 命令,以优化列宽并允许长名称换行而不被截断:
Get-WmiObject -Class Win32_Service -ComputerName . | Format-Table -Property Status,Name,DisplayName -AutoSize -Wrap
|
|
|