2014-10-02 74 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