2014-10-02 73 views
0

我試圖確定列表中的哪些服務器(例如servers.txt)安裝了.NET 4.5以及PowerShell的版本。我有獨立的命令,它會告訴我這在一個盒子 -爲多個遠程主機運行PowerShell命令並高效顯示

**

**PS D:\tools> Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client' 
Version  : 4.5.51641 
CBS   : 1 
TargetVersion : 4.0.0 
Install  : 1 
InstallPath : C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ 
Servicing  : 0 
Release  : 378675 
PSPath  : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework 
       Setup\NDP\v4\Client 
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4 
PSChildName : Client 
PSDrive  : HKLM 
PSProvider : Microsoft.PowerShell.Core\Registry** 

** 這都說明在頂部(版),它的確是4.5。有沒有辦法以表格格式顯示版本字段和主機名(或計算機名)?

此外,同爲以下命令,向你的PowerShell的版本 -

PS D:\tools> get-host |select-object version 

Version 
------- 
4.0 

其實我放的完整路徑。這是我的屏幕後,顯示txt文件,然後運行命令 -

PS D:\tools> type h:\servers.txt 
OPP-HOLD 
zOPP-SQL12HAa 
zOPP-SQLESMA 

PS D:\tools> Get-content -path h:\servers.txt | where {$_-match "\S"} | foreach Get-ItemProperty -ComputerName $_ 'HKLM: 
\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client' -name Version 
foreach : Input name "Get-ItemProperty" cannot be resolved to a method. 
At line:1 char:60 
+ Get-content -path h:\servers.txt | where {$_-match "\S"} | foreach Get-ItemPrope ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidArgument: (OPP-HOLD:PSObject) [ForEach-Object], PSArgumentException 
    + FullyQualifiedErrorId : MethodNotFound,Microsoft.PowerShell.Commands.ForEachObjectCommand 

foreach : Input name "Get-ItemProperty" cannot be resolved to a method. 
At line:1 char:60 
+ Get-content -path h:\servers.txt | where {$_-match "\S"} | foreach Get-ItemPrope ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidArgument: (zOPP-SQL12HAa:PSObject) [ForEach-Object], PSArgumentException 
    + FullyQualifiedErrorId : MethodNotFound,Microsoft.PowerShell.Commands.ForEachObjectCommand 

foreach : Input name "Get-ItemProperty" cannot be resolved to a method. 
At line:1 char:60 
+ Get-content -path h:\servers.txt | where {$_-match "\S"} | foreach Get-ItemPrope ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidArgument: (zOPP-SQLESMA:PSObject) [ForEach-Object], PSArgumentException 
    + FullyQualifiedErrorId : MethodNotFound,Microsoft.PowerShell.Commands.ForEachObjectCommand 

回答

0

喜歡這個?

Invoke-command -comp (cat servers.txt) -command { 
    get-host} | where { $_.version -eq '4.5' } | format-table *computer*, version 
+0

'Invoke-Command'是正確的方法,但我認爲OP正在尋找你沒有的兩個命令輸出。此外,我不認爲有一個PowerShell 4.5版本 – Matt 2014-10-02 16:42:29

1

Invoke-Command就是你要求的。另一個答案是不解決來自遠程主機的數據請求。

$computers = Get-Content servers.txt 

Invoke-Command -ComputerName $computers -ScriptBlock{ 
    $powerShellVersion = get-host | select-object -expandproperty version 
    $frameWorkVersion = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client' | select-object -expandproperty version 

    New-Object psobject -Property @{ 
     "Computer" = $singleComputer 
     "PowerShell Version" = $powerShellVersion 
     "Framework Version" = $frameWorkVersion 
    } 
} 

這個未經測試的代碼可以簡化,但我的目標是爲了更好的understaning。對於每個服務器運行將在自定義對象中返回PowerShell版本和.NET版本的腳本塊。這應該適用於PowerShell 3.0。請注意檢查Framework版本的the caveats。如果你只是在尋找4.5這應該工作。如果未安裝4.5,則可能需要添加-erroraction

+1

這不如直接將計算機名稱傳遞給'Invoke-Command'。如果在多臺計算機上使用'-ComputerName'參數,則在每臺計算機上創建遠程作業並且並行運行。在這裏,您正在連續運行這些作業。 – 2014-10-02 18:51:45

+0

@mikez瞭解。你將如何在輸出中記錄計算機名稱?我這樣做的唯一原因是爲了保存輸出的計算機名稱。 – Matt 2014-10-02 18:58:42

+0

PowerShell應自動附加在PSComputerName NoteProperty中運行該命令的計算機名稱。 – 2014-10-02 19:00:18