Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Reading Remote registry help

$
0
0

Hello All,

Can you please take a look at my script block. It works exactly as expected when I copy/paste it into the console window. However, when I am running it inside a ps1 file, it crashes with the following error:

 

 

Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The network path was not found."
At C:\scripts\InstalledProgramSearch.ps1:61 char:60
+     $RegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey <<<< ($KeyType, $Server.Name)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
--- Script -----
$KeyType = [Microsoft.Win32.RegistryHive]::LocalMachine
Foreach ($Server in $LiveServers){
	$RegKey = $Null
	$RegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($KeyType, $Server.Name)
	IF ($Server.Processor -like 32){$Keys = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'}
	IF ($Server.Processor -like 64){$Keys = 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall','SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
	}
	$ProgramsCollection = Foreach ($Key in $Keys){
				$SearchKey = $RegKey.OpenSubKey($Key)
				Foreach($sub in $SearchKey.GetSubKeyNames()){
				IF($Sub -like "Microsoft .NET Framework 4 Client Profile"){
				Try {
   				$Server | add-member -MemberType NoteProperty -Name DotNet -Value "Installed" -ErrorAction Stop
				} Catch {}
			}
	}
} 
	IF($Server.DotNet -Like $Null){ $Server | add-member -MemberType NoteProperty -Name DotNet -Value $Null}
}

I know for 100% the failure is due to 2 values assigned to $Keys. For some (i don't get why) reason, when the Foreach grabs the 2nd Key to go into the next loop,

$SearchKey = $RegKey.OpenSubKey($Key)

becomes Null instead of a new value. I have restarted my powershell session and it still persists. Naturally this error takes place on 64-bit systems only Thank you for your help! Russ


Viewing all articles
Browse latest Browse all 6937

Trending Articles