locked
How to get all computers with and without a registry key RRS feed

  • Question

  • Hi,

    I have no much experiences with PowerShell.

    I'd like to list all servers in an Active Directory OU which have, and do not have a registry key. I want to write the results in two different files (WithRegistry.csv and NoRegistry.csv).

    If I run following script I cannot get any results for "WithRegistry.csv".How can I script my purpose in a best way?

    $Computers = Get-ADComputer -Filter * -SearchBase "OU=Server,DC=mydomain,DC=com" -Property DNSHostName | Select -Property DNSHostName
    
    foreach ($Computer in $Computers) 
    {
    $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Computer.name)
    $RegKey = $Reg.OpenSubKey("SOFTWARE\MYKEY")
    If ($RegKey -eq $true ) {"$($Computer);$($RegKey)" | Out-File WithRegistry.csv -append}
    
    Else {"$($Computer);$($RegKey)" | Out-File NoRegistry.csv -append}
    }

    Best Regards

    Birdal


    • Edited by _Birdal Monday, February 19, 2018 10:29 AM
    • Moved by Bill_Stewart Monday, April 30, 2018 9:13 PM This is not "scripts on demand"
    Monday, February 19, 2018 10:28 AM

All replies

  • Bad syntax and structural and logic errors.

    Get-ADComputer -Filter * -SearchBase 'OU=Server,DC=mydomain,DC=com' |
        ForEach-Object{
            $computer = $_.Name
            $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computer)
            if($RegKey = $Reg.OpenSubKey('SOFTWARE\MYKEY')){
                "$($computer);$($RegKey)" | Out-File WithRegistry.csv -append
            }else{
                "$($computer);$($RegKey)" | Out-File NoRegistry.csv -append
            }
        }


    \_(ツ)_/

    Monday, February 19, 2018 10:50 AM
  • Run the following until you understand how PowerShell works.

    Get-ADComputer -Filter * -SearchBase 'OU=Server,DC=mydomain,DC=com' |
        ForEach-Object{
            $computer = $_.Name
            $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computer)
            if($RegKey = $Reg.OpenSubKey('SOFTWARE\MYKEY')){
                Write-Host "$computer;$RegKey" -fore green 
            }else{
                Write-Host "$computer;Not Found" -fore red
            }
        }
    

    You really should also handle errors.

    Get-ADComputer -Filter * -SearchBase 'OU=Server,DC=mydomain,DC=com' |
        ForEach-Object{
            $computer = $_.Name
            Try{
                $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computer)
                if($RegKey = $Reg.OpenSubKey('SOFTWARE\MYKEY')){
                    Write-Host "$computer;$RegKey" -fore green 
                }else{
                    Write-Host "$computer;Key not Found" -fore yellow
                }
            }
            Catch{
                Write-Host $_ -fore red
            }
        }


    \_(ツ)_/

    Monday, February 19, 2018 10:58 AM
  • Hi Jrv,

    thank you for your feedback and correctiony.

    If I run your script, I get many times following error:

    Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The network path was not found.
    "
    At C:\temp\CheckRegistry.ps1:4 char:64
    +         $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey <<<< ('LocalMachine', $computer)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

    Best Regards

    Birdal

    Monday, February 19, 2018 11:29 AM
  • Hi,

    how can I write the results to one result CSV file with a text "red", "green" and "yellow"?

    Best Regards

    Birdal

    Monday, February 19, 2018 11:34 AM
  • If the registry service is not running or the firewall is not open you will get that failure.


    \_(ツ)_/

    Monday, February 19, 2018 12:18 PM