如果我在服务器或工作站上,如何从 powershell 中找出?

How do I find out from powershell if I am on a server or workstation?

This doc explains how to get your windows version,但在 PowerShell 中更难找到它。

[System.Environment]::OSVersion 有很多有用的信息,但没有服务器工作站标志...

$osInfo = Get-CimInstance -ClassName Win32_OperatingSystem
$osInfo.ProductType

https://msdn.microsoft.com/en-us/library/aa394239%28v=vs.85%29.aspx

ProductType
Data type: uint32
Access type: Read-only
Additional system information.
Work Station (1)
Domain Controller (2)
Server (3)

因此,如果值为 1,那么您在工作站 OS 上。

如果是 2,则说明您使用的是域控制器。

如果是 3,则说明您所在的服务器不是域控制器。


如果您使用的是旧版本的 Windows / PowerShell,并且想要可以在所有版本上运行的东西,那是一样的,但是 Get-WmiObject:

$osInfo = Get-WmiObject -Class Win32_OperatingSystem
$osInfo.ProductType

(Get-WmiObject win32_OperatingSystem).Caption

(Get-ComputerInfo).OsProductType

在我的机器上,这返回 WorkStationServer