Public/Add-ViewGroup.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
95
96
97
98
99
100
101
#Requires -Modules 'MilestonePSTools'

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 {
            if (-not $Force) {
                $existingViewGroups = $vmo.ManagementServer.PublicViewGroups | Where-Object Name -eq $Name
                if ($null -ne $existingViewGroups) {
                    Write-Error "There already exists $($existingViewGroups.Count) ViewGroups with the name $Name. Use the -Force parameter to override this error and create a duplicate viewgroup. This is allowed in Milestone but may be confusing to the user."
                    return
                }
            }

            $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()
    }
}