How to Query remote PC's registry by OU for 2 values and export to CSV file.

I'm new to scripting and to Powershell but this is what I have managed to put together so far. Of course it fails. We have two custom entries in the registry that I want to query remote workstations for these values, Monitor 1 and Monitor 2. Output to a CSV along with the workstations name. Because of our AD structure I figured its just easier to input the OU individually as seen in the script. That portion of the script seems to work. I get the following error in bold when I run the script: I've Google'd and tinkered with this for a week now with no resolution and seem to be going in circles.  And yes, I had help to get this far.

Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The network path was not found.
"
At C:\utils\RegMonitor2.ps1:33 char:5
+     $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($Hive,$result.pro ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IOException

Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The network path was not found.
"
At C:\utils\RegMonitor2.ps1:33 char:5
+     $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($Hive,$result.pro ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IOException

# 1) Searches Active Directory for all Computers under said OU

# 2) Searches remote registry of those machines for the mentioned Monitor and Monitor2 subkeys.

# 3) Exports CSV (Can be opened and saved as excel format later) with ordered columns Computername, Monitor1 value, monitor2 value.

# ================================================================

$SearchPath = "OU=XXX,OU=XXX,OU=XXX,DC=XXX,DC=XXX,DC=XXX"

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$SearchPATH")
$objSearcher.PageSize = 1000
$objSearcher.Filter = "(objectClass=computer)"
$objSearcher.SearchScope = "Subtree"

$colProplist = "name"

$colResults = $objSearcher.FindAll()


$Store = @()
$Hive = [Microsoft.Win32.RegistryHive]"LocalMachine";

foreach ($result in $colResults)
{
    ####
    # Use $result.properties.name to retreive ComputerName
    ###
    $obj = New-Object PsObject
    $obj | Add-member -type noteproperty -name "Computername" -Value $result.properties.name
   
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($Hive,$result.properties.name);
    $ref = $regKey.OpenSubKey("SYSTEM\CurrentcontrolSet\control\Session Manager\Environment");
   
    $obj | Add-member -type Noteproperty -name "Monitor1" -value $ref.OpenSubKey("Monitor")
    $obj | Add-member -type Noteproperty -name "Monitor2" -value $ref.OpenSubKey("Monitor2")
    $store += $obj
}

$store | Select-Object Computername,Monitor1,Monitor2 | Export-CSV -noTypeInformation -Path "Pathtosave.csv"


January 13th, 2014 6:09pm

Hi,

What do your registry values look like in the Monitor and Monitor2 subkeys?

EDIT: This might help:

# Retrieve list of computers using Get-ADComputer and process each
Get-ADComputer -Filter * -SearchBase 'OU=Test PCs,DC=domain,DC=com' | ForEach {

    # Verify PC is alive
    If (Test-Connection $_.Name -Quiet -Count 1) {

        # Connect to registry
        $remoteHive = [Microsoft.Win32.RegistryHive]LocalMachine;
        $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($remoteHive,$($_.Name))

        # Open the Environment key
        $ref = $regKey.OpenSubKey('SYSTEM\CurrentcontrolSet\control\Session Manager\Environment')

        # Create an ordered hashtable with the data from string values named 'String Value One/Two' in Monitor and Monitor2 subkeys
        # You'll need to adjust these values based on your actual data
        # If you are running v2, remove [ordered] below (so the line reads $props = @{)
        $props = [ordered]@{
            Computer=$_.Name
            Monitor =$ref.OpenSubKey('Monitor').GetValue('String Value One')
            Monitor2=$ref.OpenSubKey('Monitor2').GetValue('String Value Two')
            }

        # Create a custom object based on the hashtable above
        New-Object PsObject -Property $props

    }

} | Sort-Object Computer | Export-Csv .\MonitorRegistryCheck.csv -NoTypeInformation
# The line above sorts the output object by the computer name and then exports the object to a CSV file
Free Windows Admin Tool Kit Click here and download it now
January 13th, 2014 7:23pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics