Get operating system without using WMI

旧城冷巷雨未停 提交于 2019-12-11 09:31:33

问题


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

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/11937087/get-operating-system-without-using-wmi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!