PowerShell PSRemoteRegistry - 查询多台机器

时间:2014-05-02 13:03:58

标签: arrays powershell registry

我正在尝试查询所有服务器以获取特定的注册表值。我意识到我应该添加一个try / catch片段,但我还没有。

如果通过执行以下操作来创建我的数组:

   
$Servers = "server1","server2","server3","server4", etc, etc 

它有效

如果我这样做:

$servers = Get-ADComputer -Filter {operatingsystem -like '*windows server*'} | select name

它创建了数组(带有标题),但是当通过下面的代码传递它时,它失败了:

get-regstring : Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The             network path was not found.
"
At line:1 char:27
+ foreach ($S in $servers) {get-regstring -cn $S -hive localmachine -key "SOFTWARE ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-      RegString

对于我的脚本,我尝试了这两种方法:

get-regstring -cn $servers -hive localmachine -key "SOFTWARE\Microsoft\Active Setup\InComponents\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}" -value isinstalled | Select-Object computername,data,value

foreach ($S in $servers) {get-regstring -cn $s -hive localmachine -key "SOFTWARE\Microsoft\Active Setup\InComponents\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}" -value isinstalled | Select-Object computername,data,value} 

有人可以帮我传递一个从查询构建的服务器名称数组吗?代码显然在向它传递数组时有效,但它不喜欢查询数组的某些内容。我是否需要将数组转储到文本文档并对其进行格式化并将其重新导入脚本中,以便它只是一个干净的列表?

** 的修改 **

如果有人关心,这是我改变格式后的最终代码。您仍然可以根据建议查看我使用 $ _。name 的位置,并修复了原始问题。此脚本的目的是读取IE ESC的注册表项并将其吐出为CSV。您可以轻松修改它以查找其他注册表项。

cls
Import-Module PSRemoteRegistry
Import-Module ActiveDirectory
#$ErrorActionPreference = "silentlycontinue"


######
#Code#
######


Get-ADComputer -Filter {operatingsystem -like '*windows server*'} | ForEach-Object { 
$var = $_.name
        IF (Test-Connection $var -count 1 -ea 0 -quiet)
            {
                Try {get-regstring -cn $_.name -hive localmachine -key "SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}" -value isinstalled | Select-Object computername,data,value}
                Catch {Write-Host "$var - ##### Unable to collect data #####"}
            }
            # A return of '0' means IE ESC is disabled; a return of '1' means it is enabled
        Else
            {write-host "$($_.name) - ***** Unable to Connect *****"}
    }| Sort-Object computername | Export-Csv c:\Temp\checkreg.csv -NoTypeInformation

Write-Host ""
Write-Host ""
Write-Host "----> Done!"

1 个答案:

答案 0 :(得分:0)

您正在为$servers var分配自定义对象列表( PSCustomObject ),它们需要是简单的字符串。 变化

$servers = Get-ADComputer -Filter {operatingsystem -like '*windows server*'} | select name

$servers = Get-ADComputer -Filter {operatingsystem -like '*windows server*'} | select -ExpandProperty name

$servers = (Get-ADComputer -Filter {operatingsystem -like '*windows server*'}).Name