Retrieving SCOM DB Information via SDK without Management Server access

This is just a quick post, based on an interesting question one of my colleagues asked the other day:
"How can I retrieve the SCOM database names and SQL server names (incl. instances) from a SCOM console running on some client without having access to one of the SCOM Management Server itself?"

My usual way is to retrieve these values by reading the RegKey HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup on a Management Server, but this requires access to the Management Server and so won't work in this case...

But just by accessing data from the SDK?
I searched the datastructure returned by get-scommanagementgroup but did not find anything helpful.
Today I analyzed the SCOM class hierarchy (created with a function discussed here) and found by chance two interesting classes:

Microsoft.SystemCenter.DataWarehouse
Microsoft.SystemCenter.Database.AppMonitoring

The class Microsoft.SystemCenter.Database.AppMonitoring provides two instances:
One for the OpsDB and one for the DWH. Once you know that, the rest is a "piece of cake" and you can write a quick PowerShell function to retrieve and return the desired information:

 

function get-customscomdbinformationfromsdk
{
param()
$ClassName = "Microsoft.SystemCenter.Database.AppMonitoring"
$class = Get-SCOMClass -Name $ClassName
$ClassInstances = Get-SCOMClassInstance -Class $class
$colDBInfo = @()

foreach($Instance in $ClassInstances)
{
$tempObject = New-Object pscustomobject
$tempObject | Add-Member -MemberType NoteProperty -Name SQLInstance -Value $Instance."[Microsoft.SystemCenter.Database.AppMonitoring].MainDatabaseServerName"
$tempObject | Add-Member -MemberType NoteProperty -Name SQLDBName -Value $Instance."[Microsoft.SystemCenter.Database.AppMonitoring].MainDatabaseName"

switch ($Instance.Displayname)
{
"App Diagnostics DB" {$strDBType = "OpsMgrDB"}
"App Advisor DB" {$strDBType = "OpsMgrDWH"}
}

$tempObject | Add-Member -MemberType NoteProperty -Name DBType -Value $strDBType
$colDBInfo+=$tempObject
}
return $colDBInfo
}
The result of the function looks like this:

2016-09-05_17-47-49

So if you ever need information about the SCOM databases from within the SDK give these classes a try.