Public/Storage/Remove-VmsArchiveStorage.ps1

function Remove-VmsArchiveStorage {
    <#
    .SYNOPSIS
        Removes a Milestone XProtect recording server archive storage configuration
    .DESCRIPTION
        If the specified archive is the the last one in the archive chain (it has the largest RetainMinutes value),
        this function removes the archive from the storage configuration.
    .PARAMETER Storage
        Specifies the Storage object from which to look for matching archive storage configurations
    .PARAMETER Name
        Specifies the name of the existing archive storage configuration to look for on the specified storage configuration
    .PARAMETER ArchiveStorage
        Specifies the ArchiveStorage object to be removed
    .EXAMPLE
        PS C:\> Get-RecordingServer | Get-VmsStorage -Name 'Example Storage' | Remove-VmsArchiveStorage -Name 'Retired NAS Storage'
        Removes all archive storages named 'Retired NAS Storage' from all storage configurations named 'Example Storage' on all recording servers
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
    param (
        [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'ByName')]
        [VideoOS.Platform.ConfigurationItems.Storage]
        $Storage,

        [Parameter(Mandatory, ParameterSetName = 'ByName')]
        [ValidateNotNullOrEmpty()]
        [string]
        $Name,

        [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'ByStorage')]
        [VideoOS.Platform.ConfigurationItems.ArchiveStorage]
        $ArchiveStorage
    )
    
    process {
        switch ($PSCmdlet.ParameterSetName) {
            'ByName' {
                foreach ($archiveStorage in $Storage | Get-VmsArchiveStorage -Name $Name) {
                    $archiveStorage | Remove-VmsArchiveStorage
                }
            }

            'ByStorage' {
                $recorder = [VideoOS.Platform.ConfigurationItems.RecordingServer]::new((Get-ManagementServer).ServerId, $Storage.ParentItemPath)
                $storage = [VideoOS.Platform.ConfigurationItems.Storage]::new((Get-ManagementServer).ServerId, $ArchiveStorage.ParentItemPath)
                if ($PSCmdlet.ShouldProcess("Recording server $($recorder.Name)", "Delete archive $($ArchiveStorage.Name) from $($storage.Name)")) {
                    $folder = [VideoOS.Platform.ConfigurationItems.ArchiveStorageFolder]::new((Get-ManagementServer).ServerId, $ArchiveStorage.ParentPath)
                    [void]$folder.RemoveArchiveStorage($ArchiveStorage.Path)
                }
            }
            Default {
                throw 'Unknown parameter set'
            }
        }
    }
}