locked
All computers with not existing registry path RRS feed

  • Question

  • Hi,

    I try to get all computer list in AD domain which have NOT a specific registry path. The code is like below:

    $server = get-adcomputer -filter * | select dnshostname  
    foreach ($server in $servers) { 
     $registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $server)         
     $regKey= $reg.OpenSubKey("HKEY_LOCAL_MACHINE\SOFTWARE\myAPP\App1")      
     if ($regKey -eq $null){ $server } 
     }
    $server |Export-Csv C:\ServerList.csv -NoTypeInformation 

    Unfortunately, this lists all computers. When I change the if statement option from "-eq" to "-ne" there is no change in the result.

    How can I do this statement and list these computers?

    Best regards

    Birdal

    Monday, October 8, 2018 1:04 PM

All replies

  • Good Day

    Maybe cause the variable is server and not Servers
    Please try this:

    $servers = get-adcomputer -filter * | select dnshostname  
    foreach ($server in $servers) { 
     $registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $server)         
     $regKey= $reg.OpenSubKey("HKEY_LOCAL_MACHINE\SOFTWARE\myAPP\App1")      
     if ($regKey -eq $null){ $server } 
     }
    $server |Export-Csv C:\ServerList.csv -NoTypeInformation 

    Monday, October 8, 2018 1:15 PM
  • The code is a bit impossible as the names will never be returned anywhere.

    Here is how to do this with PowerShell.

    Get-AdComputer -filter * | 
        ForEach-Object{ 
            $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $_.DnsHostName)         
            if($reg.OpenSubKey("HKEY_LOCAL_MACHINE\SOFTWARE\myAPP\App1")){
                Write-Host "Server $($_.DnsHostName) has key" -fore Green
            }else{
                Write-Host "Server $($_.DnsHostName) does not have key" -fore red
                $_.DnsHostName
            }
        }


    \_(ツ)_/



    • Edited by jrv Monday, October 8, 2018 2:32 PM
    Monday, October 8, 2018 2:30 PM
  • HiAlvaro,

    no it is not related to $servers or $server.

    Best regards

    Birdal

    Monday, October 8, 2018 2:46 PM
  • You should also handle any errors to get a rational output.

    Get-AdComputer -filter * | 
        ForEach-Object{
            Try{
                $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $_.DnsHostName)         
                if($reg.OpenSubKey("HKEY_LOCAL_MACHINE\SOFTWARE\myAPP\App1")){
                    Write-Host "Server $($_.DnsHostName) has key" -fore Green
                }else{
                    Write-Host "Server $($_.DnsHostName) does not have key" -fore yellow
                    $_.DnsHostName
                }
                }
            Catch{
                Write-Host $_ -Fore Red
            }
        }


    \_(ツ)_/


    • Edited by jrv Monday, October 8, 2018 3:38 PM
    Monday, October 8, 2018 3:38 PM
  • Hi JRV,

    thank you for your feedback.

    Your script return unfortunately not correct results.

    Although the computers have the path "HKEY_LOCAL_MACHINE\SOFTWARE\myAPP\App1"

    the script returns

    "Server server1.mydomain.com does not have key"

    "Server server2.mydomain.com does not have key"

    ....

    Perhaps the parameter "$reg.OpenSubKey" not correct? 

    Best regards

    Birdal

    Tuesday, October 9, 2018 8:44 AM
  • You have mistakes in your script. 

    This line is wrong:

     if($reg.OpenSubKey("HKEY_LOCAL_MACHINE\SOFTWARE\myAPP\App1")){

    Should be:

     if($reg.OpenSubKey('SOFTWARE\myAPP\App1')){

    If you specify a key name incorrectly it will always fail that way.



    \_(ツ)_/

    Tuesday, October 9, 2018 9:06 AM
  • Hi JRV,
    thank you. Now it results correctly.
    But after some checks it apperas
    Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The network path was not found.
    Firewall/network problem?!

    I checked some serverts to connect remotely using regedt32. That was successful. But the script tells for these servers Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The network path was not found.
    Best Regards
    Birdal


    • Edited by _Birdal Tuesday, October 9, 2018 12:20 PM
    Tuesday, October 9, 2018 12:03 PM
  • Hi JRV,

    unfornutately the script did not check all servers and listed only very few servers on which this registry key doesn't exist.

    Additionally, only 5 servers are liosted which have this registry path.

    We know that much more server have this key.

    Best regards

    Birdal

    Tuesday, October 9, 2018 12:18 PM
  • Server must have the registry service running and the firewall must allow access.


    \_(ツ)_/

    Tuesday, October 9, 2018 3:25 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    Best Regards,

    Lee


    Just do it.

    Friday, October 26, 2018 8:58 AM
  • Hi,

    As this thread has been quiet for a while, we will mark it as ‘Answered’ as the information provided should be helpful. If you need further help, please feel free to reply this post directly so we will be notified to follow it up. You can also choose to unmark the answer as you wish.

    Best Regards,

    Lee


    Just do it.

    Monday, November 5, 2018 9:35 AM