Get operating system without using WMI

2019-08-09 02:47发布

I have a Powershell script where I need to get the operating system for various servers. Up until now I've been using WMI in order to accomplish this task, however I've read about how WMI can get timeouts, so I was wondering is there another method of getting the operating system of a server? Here is the code that I use at the moment and want to change in order to avoid WMI:

$serverVersion = Get-WMIObject -computer $server -class Win32_OperatingSystem

2条回答
劳资没心,怎么记你
2楼-- · 2019-08-09 03:25

Depending on how much granularity you need, you could also use .Net's System.Environment.OSVersion:

PS C:\> $os = [Environment]::OSVersion

on windows:

PS C:\> $os
Platform ServicePack    Version        VersionString
-------- -----------    -------        -------------
 Win32NT Service Pack 1 6.1.7601.65536 Microsoft Windows NT 6.1.7601 Service Pack 1

on Linux (Ubuntu 18.04):

PS /home> $os
Platform ServicePack Version   VersionString
-------- ----------- -------   ------------- 
Unix             4.15.0.36 Unix 4.15.0.36
查看更多
我只想做你的唯一
3楼-- · 2019-08-09 03:28

You will probably find what you are looking for in the registry. As of Windows 2000 the ProductName value can be found in this location: HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion.

To query this registry value using Powershell, try:

(get-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName
查看更多
登录 后发表回答