问题提取主机名和Os版本- PowerShell



这里的快速问题,我需要拉Os版本200+服务器。我通过

找到了这样做的方法
$servers = get-content "C:AutomationServers.txt"
Get-WmiObject -class win32_operatingsystem -ComputerName $servers | Select-Object Caption

但是返回的结果如下

Caption                                                                                                                                      
-------                                                                                                                                      
Microsoft(R) Windows(R) Server 2003, Standard Edition                                                                                        
Microsoft(R) Windows(R) Server 2003, Enterprise Edition                                                                                      
Microsoft(R) Windows(R) Server 2003, Enterprise Edition                                                                                      
Microsoft(R) Windows(R) Server 2003, Enterprise Edition                                                                                      
Microsoft(R) Windows(R) Server 2003, Enterprise Edition                                                                                      
Microsoft(R) Windows(R) Server 2003, Enterprise Edition                                                                                      
Microsoft(R) Windows(R) Server 2003, Enterprise Edition                                                                                      
Microsoft(R) Windows(R) Server 2003 Enterprise x64 Edition                                                                                   
Microsoft(R) Windows(R) Server 2003, Enterprise Edition                                                                                      
Microsoft Windows Server 2008 R2 Standard                                                                                                    
Microsoft Windows Server 2008 R2 Datacenter                                                                                                  
Microsoft(R) Windows(R) Server 2003, Enterprise Edition                                                                                      
Microsoft(R) Windows(R) Server 2003, Enterprise Edition                                                                                      
Microsoft(R) Windows(R) Server 2003 Standard x64 Edition                                                                                     
Microsoft(R) Windows(R) Server 2003, Standard Edition  

现在主要的问题是一些失败了,所以我不能只是倒数列表,并把他们的名字…有没有办法让程序写主机名从我的文件旁边的版本?还是我找错对象了....

我确实找到了一种方法来处理Ip地址,如

$servers = get-content "C:AutomationServers.txt"
foreach ($server in $servers) {
  $addresses = [System.Net.Dns]::GetHostAddresses($server)
  foreach($a in $addresses) {
    "{0},{1}" -f $server, $a.IPAddressToString
  }

然而,这不是使用WMI对象,而是使用"System.Net.Dns]::GetHostAddresses($server)",所以我不确定如何适应我的需求,任何帮助将是伟大的。谢谢你的帮助。

你获得的Win32_operatingsystem对象已经有一个PSComputerName属性,所以你只需要选择它:

$servers = get-content "C:AutomationServers.txt"
Get-WmiObject -class win32_operatingsystem -ComputerName $servers | Select-Object PSComputerName,Caption

或者,如果你不喜欢"PSComputerName"这个名字,你可以改变这个属性的名字:

$servers = get-content "C:AutomationServers.txt"
Get-WmiObject -class win32_operatingsystem -ComputerName $servers | Select-Object @{Name="Hostname";Expression={$_.PSComputerName }},Caption

我可能读错了你的帖子,但是如果你想匹配主机名和操作系统版本,你可以这样做:

get-content "C:AutomationServers.txt" | ForEach-Object {
    $caption = Get-WmiObject -class win32_operatingsystem -ComputerName $_ | Select-Object Caption
    "{0},{1}" -f $_,$caption
}

我认为你可以把它弄小一点,但我觉得这样更容易读。

最新更新