获取主机名和操作系统版本的问题-PowerShell

获取主机名和操作系统版本的问题-PowerShell,powershell,Powershell,这里的快速问题,我需要拉操作系统版本200+服务器。。我找到了通过 $servers = get-content "C:\Automation\Servers.txt" Get-WmiObject -class win32_operatingsystem -ComputerName $servers | Select-Object Caption 然而,结果却是如此 Caption

这里的快速问题,我需要拉操作系统版本200+服务器。。我找到了通过

$servers = get-content "C:\Automation\Servers.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:\Automation\Servers.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)”,所以我不知道如何使其适应我的需要,任何帮助都会很好。谢谢你的帮助

我可能误读了您的帖子,但如果您试图将主机名与操作系统版本匹配,则可以执行以下操作:

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

我想你可以把它缩小一些,但我觉得这样读起来更容易

您正在获取的Win32_operatingsystem对象已具有PSComputerName属性,因此您只需选择它:

$servers = get-content "C:\Automation\Servers.txt"
Get-WmiObject -class win32_operatingsystem -ComputerName $servers | Select-Object PSComputerName,Caption
或者,如果您不喜欢名称“PSComputerName”,可以更改此属性的名称:

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

就这样。

完美!!!这正是我需要的。。。这是怎么回事?我真的不明白“{0},{1}”是什么意思?这种类型的操作叫做什么,这样我就可以在谷歌上搜索并了解它了吗?它创建了一个格式化的字符串。你可以有
“我的名字是{1},我最喜欢的颜色是{0}”-f“blue”,“Matt”
Matt将在字符串中占据位置1,blue将占据位置0创建
“我的名字是Matt,我最喜欢的颜色是blue”
链接包含了对-f操作符的更深入的研究,它可以做更多的工作。