Reputation: 29
I'm trying to create a script that takes an input of computer names from a file and checks to see if a registry key exists. The script is erroring out from the second line, it seems the computer names are not being added from the variable. If the computer name is entered manually, the script works fine. Here is the error:
Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The network path was not found. At line:3 char:1 + $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$lap)
Here is the script:
$laptop = Get-Content -Path 'Laptop.txt'
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$lap")
$RegKey= $Reg.OpenSubKey("SOFTWARE\Status\")
$NetbackupVersion = $RegKey.GetValue("Activated")
foreach ($lap in $laptop) {
if ($NetbackupVersion -eq "Yes") {
echo "$lap has the key"
} else {
echo "$lap does not have the key"
}
}
Another issue is that is the key doesn't exist, the script shows errors but it still echos that the machine has the key.
Upvotes: 0
Views: 295
Reputation: 200273
You have a spurious trailing double quote after $lap
in the OpenRemoteBaseKey()
call. Remove that. Also, the registry lookup code belongs in the body of your foreach
loop. The loop variable $lap
is undefined outside.
Change this:
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$lap")
$RegKey = $Reg.OpenSubKey("SOFTWARE\Status\")
$NetbackupVersion = $RegKey.GetValue("Activated")
foreach ($lap in $laptop) {
...
}
into this:
foreach ($lap in $laptop) {
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $lap)
$RegKey = $Reg.OpenSubKey("SOFTWARE\Status\")
$NetbackupVersion = $RegKey.GetValue("Activated")
...
}
Upvotes: 1