Private/RSOP/_RunRSOPReport.ps1

function _RunRSOPReport {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateSet('HTML', 'Listing')]
        [string]$ReportType,

        [parameter(Mandatory = $true)]
        [string]$Identity,

        [ValidateScript( { Get-ADUser $_ })]
        [string]$User,

        [Parameter(Mandatory = $true)]
        [string]$Path,

        [parameter(Mandatory = $false)]
        [PSCredential]$Credential
    )

    begin {
        if (!(Test-Path $Path)) {
            New-Item -Type Directory $Path
        }
    }

    process {
        $RSOPSplat = @{
            ReportType = $ReportType
            Identity   = $Identity
            Path       = $Path
        }

        if ($Credential) {
            # TODO this will use the alternate credentials in the future
            # The UI will limit this to only the Listing report type.
            $RSOPSplat += @{
                Credential = $Credential
            }
        }
        if ($User) {
            $RSOPSplat += @{
                User = $User
            }
        }

        $ButtonPress = _ShowMessageBox -MessageText 'Be sure you have permissions or ServerTeamTools will hang forever.' -MessageTitle 'Warning' -MessageIcon 'Warning' -ButtonType 'OKCancel'
        if ($ButtonPress -eq 'OK') {
            $FileLocation = _GetRSOP @RSOPSplat
            if ($FileLocation -ne 'Unable to create file.') {
                $RSOP_RSOPFileLocationLabel.Text = $FileLocation
                Invoke-Item $FileLocation
            }
            else {
                _ShowMessageBox -MessageText $FileLocation -MessageIcon Asterisk -ButtonType OK -MessageTitle 'Error creating RSOP report'
            }
        }
    }

    end {
    }
}