Question

I am trying to query all of our servers for a specific registry value. I realize I should probably add a try/catch piece to it but I am not at that point yet.

If create my array by doing something like :

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

it works

If I do it this way:

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

It creates the array (with a header) but when passing it thru the code below it fails with:

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

For my script, I have tried both of these methods:

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} 

Can someone help me pass an array of server names built from a query? The code obviously works when passing an array to it but it doesn't like something about the queried array. Do I need to dump the array to a text doc and format it and import it back into the script so it is just a clean list?

** EDIT **

If anyone cares, here is my final code after changing the format a bit. You can still see where I used $_.name per the suggestion and it fixed my original issue. This scripts purpose is to read the registry key for IE ESC and spit it out into a CSV. You can modify it easily to look for other reg keys.

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!"
Was it helpful?

Solution

You are assigning a list of custom objects(PSCustomObject) to $servers var and they need to be simple strings. Change

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

to

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

or

$servers = (Get-ADComputer -Filter {operatingsystem -like '*windows server*'}).Name
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top