Public/Get-ViewGroup.ps1

#Requires -Modules 'MilestonePSTools'

function Get-ViewGroup {
    <#
    .SYNOPSIS
        Gets View Group objects from the connected Milestone XProtect Management Server
    .DESCRIPTION
        Working with View Groups is not currently supported in MIP SDK, however the redistributable SDK
        includes an assembly containing the VmoClient which is a component used internally by the SDK
        and applications like XProtect Management Client.
 
        This function makes use of this VmoClient to manipulate the View Groups to enable complex tasks
        required in some environments.
    .EXAMPLE
        PS C:\> Get-ViewGroup -Name 'Remote Guards' | Remove-ViewGroup
        Gets the View Group(s) named 'Remote Guards' and removes (deletes) them from the Management Server, including all views contained within.
    .OUTPUTS
        The output of this function will be zero or more [VideoOS.Management.VmoClient.PublicViewGroup] objects
        with the following properties:
 
        Parent :
        Views : {}
        PublicViewGroups : {}
        Name :
        Description :
        CreatedDate : 1/1/0001 12:00:00 AM
        LastModified : 1/1/0001 12:00:00 AM
        Id : 00000000-0000-0000-0000-000000000000
        IsDirty : True
        IsCreated : False
        IsDeleted : False
        Exists : False
        CustomProperties : {}
        IsCustomPropertiesSupported : True
        ServerVersion : 20.3.1
        }
    #>

    [CmdletBinding()]
    [OutputType([VideoOS.Management.VmoClient.PublicViewGroup])]
    param(
        # Specifies the display name of the ViewGroup. Wildcards are supported.
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $Name = '*'
    )

    begin {
        $vmo = Get-VmoClient
    }

    process {
        try {
            $resultFound = $false
            $viewGroups = [system.collections.generic.list[VideoOS.Management.VmoClient.PublicViewGroup]]$vmo.ManagementServer.PublicViewGroups
            foreach ($viewGroup in $viewGroups) {
                if ($viewGroup.Name -like $Name) {
                    Write-Output $viewGroup
                    $resultFound = $true
                }
            }
            if (!$resultFound -and ![system.management.automation.wildcardpattern]::ContainsWildcardCharacters($Name)) {
                Write-Error -Message "View Group named '$Name' not found" -Category ObjectNotFound
            }
        }
        catch {
            $vmo.Dispose()
            throw
        }
    }

    end {
        $vmo.Dispose()
    }
}