为多个远程主机运行PowerShell命令并高效显示

时间:2014-10-02 15:16:54

标签: .net powershell version

我正在尝试确定列表中的哪些服务器(例如,servers.txt)安装了.NET 4.5以及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

2 个答案:

答案 0 :(得分: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
    }
}

这个未经测试的代码可以简化,但我的目标是更好地理解。对于每个服务器,运行将在自定义对象中返回PowerShell版本和.NET版本的scriptblock。这应该适用于PowerShell 3.0。请注意检查这样的Framework版本的the caveats。如果你只是寻找4.5,这应该工作。如果未安装4.5,则可能需要添加-erroraction

答案 1 :(得分:0)

喜欢这个吗?

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