Functions/Get-SCOMAgentManagementGroup.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

<#
    .SYNOPSIS
    Get all Management Group connections of a SCOM Agent.

    .DESCRIPTION
    Use PowerShell Remoting in combination with a local COM object query to get
    the current Management Group configuration of the SCOM Agent.

    .INPUTS
    None.

    .OUTPUTS
    OperationsManagerFever.AgentManagementGroup.

    .EXAMPLE
    PS C:\> Get-SCOMAgentManagementGroup -ComputerName 'PC1', 'PC2'
    Get the registered Management Groups on the SCOM Agents PC1 and PC2.

    .NOTES
    Author : Claudio Spizzi
    License : MIT License

    .LINK
    https://github.com/claudiospizzi/OperationsManagerFever
#>


function Get-SCOMAgentManagementGroup
{
    [CmdletBinding()]
    param
    (
        # Computer name of the target agent.
        [Parameter(Mandatory = $false)]
        [System.String[]]
        $ComputerName = 'localhost',

        # Optional credentials to perform the action.
        [Parameter(Mandatory = $false)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = $null
    )

    begin
    {
        # Optional credential parameter
        $credentialParam = @{}
        if ($Credential -ne $null)
        {
            $credentialParam['Credential'] = $Credential
        }

        # Query script block
        $query = {

            try
            {
                $config = New-Object -ComObject AgentConfigManager.MgmtSvcCfg
            }
            catch
            {
                throw "No Monitoring Agent installed on $Env:ComputerName."
            }

            return $config.GetManagementGroups()
        }
    }

    process
    {
        foreach ($computer in $ComputerName)
        {
            $managementGroups = Invoke-Command -ComputerName $ComputerName -ScriptBlock $query @credentialParam -ErrorAction Stop

            foreach ($managementGroup in $managementGroups)
            {
                $object = New-Object -TypeName PSObject -Property @{
                    ComputerName         = $computer
                    ManagementGroupName  = $managementGroup.managementGroupName
                    ManagementServer     = $managementGroup.managementServer
                    ManagementServerPort = $managementGroup.managementServerPort
                    IsActiveDirectory    = $managementGroup.IsManagementGroupFromActiveDirectory
                    ActionAccount        = $managementGroup.ActionAccount
                }

                $object.PSTypeNames.Insert(0, 'OperationsManagerFever.AgentManagementGroup')

                Write-Output $object
            }
        }
    }
}