I am attempting to get remote registry values from a server using powershell.
I found some code online that worked for me:
$strComputer = "remoteComputerName"
$reg = [mcrosoft.win32.registryKey]::openRemoteBaseKey('LocalMachine',$strComputer)
$regKey = $reg.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion")
$regKey.getValue("ProgramFilesDir")
but when I try to put it in a function:
$strComputer = "remoteComputerName"
function getRegValue {
param($computerName, $strPath, $strKey)
$reg = [mcrosoft.win32.registryKey]::openRemoteBaseKey('LocalMachine',$computerName) #Errors out here
$regKey = $reg.OpenSubKey($strPath)
$regKey.getValue($strKey)
}
$a = "Software\\Microsoft\\Windows\\CurrentVersion"
$b = "ProgramFilesDir"
getRegValue($strComputer, $a, $b)
errors out:
Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The endpoint format is invalid."
What am I doing wrong?