2014-07-12 60 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 

就是這樣。

相關問題