Public/Set-MSPBackupFilter.ps1

Function Set-MSPBackupFilter {
[CmdletBinding(SupportsShouldProcess = $true)]
    [OutputType('System.String')]
    Param(
        [String[]]$Add,
        [String[]]$Remove
    )
    Begin {
        Write-Verbose ('{0}:: Function started' -f $MyInvocation.MyCommand)
        $stdOutTempFile = [System.IO.Path]::GetTempFileName()
        $stdErrTempFile = [System.IO.Path]::GetTempFileName()

        $ArgArray = @(
            "-machine-readable",
            "control.filter.modify"
        )
        If($Add) { $ArgArray += "-add $Add"; $Filter = $Add }
        If($Remove) { $ArgArray += "-remove $Remove"; $Filter = $Remove }
    }
    Process {
        If($PSCmdlet.ShouldProcess("$Filter","Modify Filter")) {
            $StartProcessParams = @{
                FilePath = $Script:CmdPath
                ArgumentList = $ArgArray
                Wait = $true
                NoNewWindow = $true
                PassThru = $true
                RedirectStandardOutput = $stdOutTempFile
                RedirectStandardError = $stdErrTempFile
                ErrorAction = "SilentlyContinue"
            }
            $StartExe = Start-Process @StartProcessParams
            $ClientToolExitCode = $StartExe.ExitCode
            $ClientToolStandardOutput = Get-Content -Path $stdOutTempFile  | Out-String
            $ClientToolStandardOutput = $ClientToolStandardOutput.Trim()
            $ClientToolStandardError = Get-Content -Path $stdErrTempFile | Out-String
            $ClientToolStandardError = $ClientToolStandardError.Trim()

            If ($stdOutTempFile -or $stdErrTempFile) {
                Remove-Item @RemoveItemParams
            }
            $ArgArray | Out-Null
            $ClientToolExitCode | Out-Null
            If ($null -ne $ClientToolStandardError) {
                $Output = $ClientToolStandardError
            } Else {
                $Output = $ClientToolStandardOutput
            }
        }
    }
    End {
        Write-Verbose ('{0}:: Function ended' -f $MyInvocation.MyCommand)
        Return $Output
    }
}