Powershell script cannot find registry key that exists, and has permission to read

864 Views Asked by At

I am trying to write a script that retrieves the SCOM Name, Version Number, and Build on a SCOM Management Server.

$scomRegistryPath = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup"
$scomDisplayName = (Get-Item -Path $scomRegistryPath).GetValue("DisplayName")
$scomVersion = (Get-Item -Path $scomRegistryPath).GetValue("DisplayVersion")
$scomBuild = (Get-Item -Path $scomRegistryPath).GetValue("BuildNumber")
Write-Host "SCOM Name: $($scomDisplayName)"
Write-Host "SCOM Version Number: $($scomVersion)"
Write-Host "SCOM Build Number: $($scomBuild)"

When I run it, I get the following errors:

Get-Item : Cannot find path 'C:\Users\UserName\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup' because it does not exist.
At line:3 char:21
+ $scomDisplayName = (Get-Item -Path $scomRegistryPath).GetValue("DisplayName")
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\UserName...nager\3.0\Setup:String) [Get-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
 
You cannot call a method on a null-valued expression.
At line:3 char:1
+ $scomDisplayName = (Get-Item -Path $scomRegistryPath).GetValue("DisplayName")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Get-Item : Cannot find path 'C:\Users\UserName\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup' because it does not exist.
At line:4 char:17
+ $scomVersion = (Get-Item -Path $scomRegistryPath).GetValue("DisplayVersion")
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\UserName...nager\3.0\Setup:String) [Get-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
 
You cannot call a method on a null-valued expression.
At line:4 char:1
+ $scomVersion = (Get-Item -Path $scomRegistryPath).GetValue("DisplayVersion")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Get-Item : Cannot find path 'C:\Users\UserName\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup' because it does not exist.
At line:5 char:15
+ $scomBuild = (Get-Item -Path $scomRegistryPath).GetValue("BuildNumber")
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\UserName...nager\3.0\Setup:String) [Get-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
 
You cannot call a method on a null-valued expression.
At line:5 char:1
+ $scomBuild = (Get-Item -Path $scomRegistryPath).GetValue("BuildNumber")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

The HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup registry key is accessible and readable from the account using regedit, so I am confused as to why this script isn't working.

2

There are 2 best solutions below

0
Ξένη Γήινος On BEST ANSWER

Your registry path is malformed, you should use 'HKLM:' instead of 'HKEY_LOCAL_MACHINE' in PowerShell.

Also your command to get registry values is wrong. You should use Get-ItemProperty to query registry values.

The correct syntax for it is Get-ItemProperty -Path $path -Name $name

Fixed code:

$scomRegistryPath = "HKLM:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup"
$scomDisplayName = Get-ItemProperty -Path $scomRegistryPath -Name "DisplayName"
$scomVersion = Get-ItemProperty -Path $scomRegistryPath -Name "DisplayVersion"
$scomBuild = Get-ItemProperty -Path $scomRegistryPath -Name "BuildNumber"
Write-Host "SCOM Name: $($scomDisplayName)"
Write-Host "SCOM Version Number: $($scomVersion)"
Write-Host "SCOM Build Number: $($scomBuild)"
0
Jeff Zeitlin On

When you are accessing the registry with Get-Item or Get-ItemProperty, you need to specify that the "provider" is the Registry Provider. If you wish to use the fully-expanded path (e.g., HKEY_LOCAL_MACHINE\Software\...), you need to prefix the path with Registry:: (that is, Get-Item -Path "Registry::HKEY_LOCAL_MACHINE\Software\Micro..."). You may instead choose to use the "drive" notation for the registry, e.g., HKLM:\Software\Microsoft..., where the "drive" name HKLM implicity includes the Registry Provider.