我有一个 Powershell 脚本,我需要在其中获取各种服务器的操作系统。到目前为止,我一直在使用 WMI 来完成此任务,但是我已经阅读了有关 WMI 如何获取超时的信息,所以我想知道是否有另一种获取服务器操作系统的方法?这是我目前使用并希望更改以避免 WMI 的代码:
$serverVersion = Get-WMIObject -computer $server -class Win32_OperatingSystem
我有一个 Powershell 脚本,我需要在其中获取各种服务器的操作系统。到目前为止,我一直在使用 WMI 来完成此任务,但是我已经阅读了有关 WMI 如何获取超时的信息,所以我想知道是否有另一种获取服务器操作系统的方法?这是我目前使用并希望更改以避免 WMI 的代码:
$serverVersion = Get-WMIObject -computer $server -class Win32_OperatingSystem
您可能会在注册表中找到您要查找的内容。从 Windows 2000 开始,ProductName
可以在以下位置找到该值:HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion
.
要使用 Powershell 查询此注册表值,请尝试:
(get-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName
根据您需要多少粒度,您还可以使用 .Net 的System.Environment.OSVersion:
PS C:\> $os = [Environment]::OSVersion
在窗户上:
PS C:\> $os
Platform ServicePack Version VersionString
-------- ----------- ------- -------------
Win32NT Service Pack 1 6.1.7601.65536 Microsoft Windows NT 6.1.7601 Service Pack 1
在 Linux (Ubuntu 18.04) 上:
PS /home> $os
Platform ServicePack Version VersionString
-------- ----------- ------- -------------
Unix 4.15.0.36 Unix 4.15.0.36