0

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.

saturnSam
  • 21
  • 4
  • Try changing ``HKEY_LOCAL_MACHINE\`` to ``HKLM:\`` – Santiago Squarzon Apr 14 '23 at 16:15
  • That's gotten rid of the errors, but the script is outputting blank fields: ``` SCOM Name: SCOM Version Number: SCOM Build Number: ``` – saturnSam Apr 14 '23 at 16:20
  • I dont have access to that reg key but perhaps using `Get-ItemPropertyValue` might work, try `Get-ItemPropertyValue 'HKLM:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup' -Name DisplayName` – Santiago Squarzon Apr 14 '23 at 16:24

2 Answers2

1

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)"
Ξένη Γήινος
  • 2,181
  • 1
  • 9
  • 35
0

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.

Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33