Search code examples
powershellscom

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


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.


Solution

  • 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)"