Public/Add-ViewGroup.ps1

function Add-ViewGroup {
<#
.SYNOPSIS
    Creates a new View Group on the currently connected 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:\> Add-ViewGroup -Name 'Remote Guards' -Description 'Views required by remote guard stations'
    Creates a new View Group named 'Remote Guards', as long as a View Group by that name does not
    exist already.
.EXAMPLE
    PS C:\> Add-ViewGroup -Name 'Remote Guards' -Force -PassThru
    Creates a new View Group named 'Remote Guards', and if the group already exists, a second one
    will be created. Please note that this can be confusing to users and is not recommended.
 
    The function will output an object representing the new View Group including the Id of that group.
.OUTPUTS
    The output of this function when called with the -PassThru switch will be a
    [VideoOS.Management.VmoClient.PublicViewGroup] object 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 View Group
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Name,

        # Specifies the description of the new View Group
        [Parameter()]
        [string]
        $Description,

        # Pass the newly created viewgroup object to the pipeline
        [Parameter()]
        [switch]
        $PassThru,

        # Allow the creation of ViewGroups with duplicate names
        [Parameter()]
        [switch]
        $Force
    )

    begin {
        $vmo = Get-VmoClient
    }

    process {
        try {
            $viewGroup = [VideoOS.Management.VmoClient.PublicViewGroup]::new($vmo.ManagementServer)
            $viewGroup.Name = $Name
            $viewGroup.Description = $Description
            $viewGroup.Create()
            if ($PassThru) {
                Write-Output $viewGroup
            }
        }
        catch {
            $vmo.Dispose()
            throw
        }
    }

    end {
        $vmo.Dispose()
    }
}