拉出主机名和Os版本的问题 - PowerShell

时间:2014-07-12 02:49:28

标签: powershell

在这里快速提问,我需要推出Os版本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)”所以我不知道如何根据我的需要调整它,任何帮助都会很棒。感谢你的帮助。

2 个答案:

答案 0 :(得分:1)

您获取的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

那就是它。

答案 1 :(得分:0)

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

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

我认为你可以让它变小但我觉得这样更容易阅读。