2

I am trying to search several servers to see if a specific Registry key exists.

It looks like the code below is working, but as I start to add the final part of the key, it stops "finding" stuff. I can start to add a*, then ab* as the last key, but as soon as I get to the third character or even the full string that I know is there, it comes back False saying it did not find it.

$servers = Get-Content c:\input.txt | `
   Select-Object @{l='ComputerName';e={$_}},@{l='KeyExist';e={Test-Path "HKLM:\System\CurrentControlSet\services\*abcdefg*" }}
$servers | Format-Table -AutoSize
3
  • You are not check remote registries. Just you own computer every time. The double *'s are redundant. Commented Feb 23, 2017 at 16:23
  • Are you perhaps intending to test the registry path on different computers? Your current command just checks the registry on your local computer, for each server name you pipe in from the file Commented Feb 23, 2017 at 16:23
  • For remote registry , you should do something like this: $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $_.machinename); $key = $reg.OpenSubkey($keyname); $value = $key.GetValue('AutoAdminLogon') Commented Feb 23, 2017 at 16:29

2 Answers 2

1

Your problem is that you run Test-Path against the local computer for each remote server name. Unfortunately Test-Path doesn't support querying remote registries.

You could use WMI:

$RegProv = [wmiclass]"\\$servername\root\default:StdRegProv"
if($RegProv.EnumKey(2147483650,"System\CurrentControlSet\services").sNames -like 'abc*'){
    # key starting with abc exists
}

Wrap it in your calculated property like this:

@{Name='KeyExists';Expression={[bool](([wmiclass]"\\$_\root\default:StdRegProv").EnumKey(2147483650,"System\CurrentControlSet\services").sNames -like 'abc*')}}
Sign up to request clarification or add additional context in comments.

Comments

0

You can check the remote registry like this :

So for each server it will get the registry value and it will store the value in the arraylist and will display the final result.

Presently in your code, you are basically checking locally only.

   #####Get Registry Value #### 
$main = "LocalMachine"
$path= "registry key path"
$servers = Get-Content c:\input.txt #-- Get all the servers
$arr=New-Object System.Collections.ArrayList

foreach ($Server in $servers)  
{ 

$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($main, $Server) 
$regKey= $reg.OpenSubKey($path) 
$Value = $regkey.GetValue($key) 
$arr.Add($Value)
}
$arr

Note: Change the placeholders accordingly

4 Comments

With this approach you lose the correlation between value and machine name
Then what should be the best way . Could you please let me know. I do not want to use get-itemproperty or get-itempropertyvalue
Instead of using an ArrayList, you should output an object with both properties (using the [pscustomobject] type accelerator, or New-Object psobject), then assign the entire foreach(){} loop to a variable
Yes, that would be much more better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.