2014-07-12 61 views
0
这里

快速的问题,我需要拉OS版本200+服务器..我发现做到这一点通过问题拉动主机名和操作系统版本 - PowerShell的

$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这里是指不使用其使用“System.Net WMI对象这样

$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 
    } 

但是地址。 Dns] :: GetHostAddresses($ server)“所以我不知道如何适应我的需要,任何帮助将是伟大的。感谢你的帮助。

回答

0

我可能会误解你的文章,但如果你想匹配的操作系统版本的主机名,你可以做到以下几点:

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

我认为你可以使它更小,但我觉得它更容易喜欢阅读这个。

+0

PERFECT !!!这正是我需要的......现在这是如何工作的?我真的不明白“{0},{1}”“是干什么的?这种行为是否被称为某种东西,我可以谷歌和了解它? – Mal229

+0

这是创建一个格式化的字符串。你可以有 ''我的名字是{1},我最喜欢的颜色是{0}“-f”蓝色“,”Matt“ 马特将假设位置1在字符串中,蓝色将假设位置0创建 '”我的名字是马特,我最喜欢的颜色是蓝色“#: http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/11/understanding-powershell-and-basic-string-formatting.aspx The链接包含了更深入的-f运算符,它可以做更多的事情。 – Matt

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 

就是这样。

相关问题