使用 powershell 区分 linux 和 windows 机器

differentiate between linux and windows machine with powershell

我是 运行 hyperV 服务器,它有许多 Linux 和 Windows 虚拟机。我想编写 powershell 脚本,它将给我 windows 机器的列表。

命令"Get-VM"给出所有虚拟机的列表。

我做了一些研究来区分 Linux 和 Windows 机器,我发现 windows 机器 returns 版本的操作系统 Linux 不要这样做。 这不是很有效,任何人都知道任何其他方法。

i have done some research where i found, windows machines returns version of Operating system where Linux don't do the same.

因此您需要遍历 VM 列表,并检查每个 VM 的 OS 版本。

顺便说一下,这听起来不是一个很好的方法。谁告诉您未来的 Hyper-V 更新不会 return 一个适用于 linux 机器的版本?

关于虚拟机的 powershell 命令只需从 hyper-v 查询,只需通过 powershell 将消息发送到 os (just windows)you can see all cmd-let for Hyper-v module in here,but you have get more information from xml file of machine see this
您也可以通过 powershell 将命令发送到 vm machine

Invoke-Command –ComputerName <Hostname> -Credential <Domain>\<Username> -Scriptblock {[System.Environment]::OSVersion.Version}

我试过这个脚本,对我来说效果很好。

 filter Import-CimXml
  {
   $CimXml = [Xml]$_
  $osNameNode = $CimXml.SelectSingleNode("/INSTANCE/PROPERTY[@NAME='Name']/VALUE[child::text() = 'OSName']")
if ($osNameNode -ne $null)
     {
     return $osNameNode.SelectSingleNode("/INSTANCE/PROPERTY[@NAME='Data']/VALUE/child::text()").Value
} 
}


  $vmList =Get-VM 

  foreach ($vm in $vmList)
   {
     $vmObj = Get-WmiObject -Namespace root\virtualization\v2 -Query "Select * From Msvm_ComputerSystem Where ElementName='$($vm.Name)'"
   $Kvp = $vmObj.GetRelated("Msvm_KvpExchangeComponent")
  $CimData = $Kvp.GuestIntrinsicExchangeItems

     if ($CimData -ne $null) #only for running vm
 {
  $osName = ($CimData | Import-CimXml)
  if ($osName -ne $null) # os like linux or something not supported KVPExchange
    {
    if ($osName.Contains("Window"))
     {
      Write-Host "$($vm.Name)"
     }
   }
} 
 }