Helpers/Get-SCOMManagementGroupConnectionActive.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

function Get-SCOMManagementGroupConnectionActive
{
    [CmdletBinding()]
    [OutputType([Microsoft.SystemCenter.Core.Connection.Connection])]
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingCmdletAliases', '')]
    param ()

    $ActiveConnection = Get-SCOMManagementGroupConnection | Where-Object { $_.IsActive }

    if ($null -ne $ActiveConnection)
    {
        return $ActiveConnection
    }
    else
    {
        $Exception   = New-Object -TypeName 'System.Exception' -ArgumentList 'The Data Access service is either not running or not yet initialized. Please check the Management Group connection.'
        $ErrorRecord = New-Object -TypeName 'System.Management.Automation.ErrorRecord' -ArgumentList $Exception, '0', 'ConnectionError', $null

        $PSCmdlet.ThrowTerminatingError($ErrorRecord)
    }
}