PowerShell 使用Win32_OperatingSystem获取操作系统信息 4


Win32_ComputerOperatingSystem类提供了关于你机器上安装的操作系统的详细信息。下面的这些例子是从这里转换过来的。

# ServicePack 版本
Get-CimInstance -ClassName Win32_OperatingSystem | 
select ServicePackMajorVersion, ServicePackMinorVersion

# 操作系统的安装日期
Get-CimInstance -ClassName Win32_OperatingSystem | 
select Installdate

# Windows 版本
Get-CimInstance -ClassName Win32_OperatingSystem | 
select Caption, Version

# windows 目录 
Get-CimInstance -ClassName Win32_OperatingSystem | 
select WindowsDirectory

# 所有的信息
Get-CimInstance -ClassName Win32_OperatingSystem | 
select Caption, Version, ServicePackMajorVersion, 
ServicePackMinorVersion, Installdate, WindowsDirectory

你也可以创建一个函数:

function get-OS { 
[CmdletBinding()] 
param( 
[string]$computername = $env:COMPUTERNAME 
)

Get-CimInstance -ClassName Win32_OperatingSystem  -ComputerName $computername| 
select Caption, Version, ServicePackMajorVersion, 
ServicePackMinorVersion, Installdate, WindowsDirectory

}

然后选择你需要的属性:

PS> get-OS | Format-Table Caption, Installdate

Caption                                                     Installdate
-------                                                     -----------
Microsoft Windows Server 2012 R2 Datacenter                 1/7/2014 10:58:02 AM

PS> get-OS

Caption                 : Microsoft Windows Server 2012 R2 Datacenter
Version                 : 6.3.9600
ServicePackMajorVersion : 0
ServicePackMinorVersion : 0
Installdate             : 1/7/2014 10:58:02 AM
WindowsDirectory        : C:\Windows

PS> get-OS | Format-Table Caption, Installdate -AutoSize

Caption                                     Installdate
-------                                     -----------
Microsoft Windows Server 2012 R2 Datacenter 1/7/2014 10:58:02 AM

原文作者:Richard Siddaway
原文链接:Win32_OperatingSystem examples

本文链接: https://www.pstips.net/get-os-info-by-win32_operatingsystem.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

回复 Bob 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

4 条评论 “PowerShell 使用Win32_OperatingSystem获取操作系统信息

  • Bob

    楼主你好,我执行你的code出现如下错误,请问我是不是少了操作?求指导!

    PS C:\> get-OS
    无法将“Get-CimInstance”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请
    检查名称的拼写,如果包括路径,请确保路径正确,然后重试。
    所在位置 C:\1.ps1:7 字符: 16
    + Get-CimInstance <<<< -ClassName Win32_OperatingSystem -ComputerName $comput
    ername|
    + CategoryInfo : ObjectNotFound: (Get-CimInstance:String) [], Com
    mandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException