Public/Show-SMBeat.ps1

#Requires -Version 5.1

function Show-SMBeat {
    <#
    .SYNOPSIS
        Builds and exports an HTML/CSV report with defaults.
    .DESCRIPTION
        Get-SMBeatReport plus Export-SMBeatReport in one step.
        Default window is the last 7 days. Files go to %ProgramData%\SMBeat-Reports.
        Theme is health. Language follows the UI of the machine that runs the cmdlet.
    .PARAMETER NoOpen
        Write the files but do not open the HTML report. Default is to open it.
    #>

    [CmdletBinding(DefaultParameterSetName = 'Local')]
    param(
        [Parameter(ParameterSetName = 'ComputerName', ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string[]]$ComputerName,

        [Parameter(ParameterSetName = 'Session')]
        [System.Management.Automation.Runspaces.PSSession[]]$Session,

        [pscredential]$Credential,
        [System.Management.Automation.Runspaces.AuthenticationMechanism]$Authentication,
        [switch]$UseSSL,
        [int]$Port,
        [System.Management.Automation.Remoting.PSSessionOption]$SessionOption,
        [int]$ThrottleLimit = 32,

        [datetime]$Start,
        [datetime]$End,
        [int]$LastHours,
        [int]$LastDays,
        [int]$LastWeeks,
        [ValidateSet('Hour', 'Day', 'Week')]
        [string[]]$Granularity = @('Hour', 'Day', 'Week'),
        [object]$TimeZone,
        [string]$Path,
        [string]$OutputPath,
        [ValidateSet('de', 'en')]
        [string]$Language,
        [ValidateSet('de', 'en')]
        [string]$CsvCulture,
        [ValidateSet('health', 'pine')]
        [string]$Theme = 'health',
        [switch]$NoOpen
    )

    begin {
        Assert-SMBeatRemotingExclusive -ComputerName $ComputerName -CimSession $null -Session $Session
        $names = New-Object System.Collections.Generic.List[string]
    }

    process {
        if ($PSCmdlet.ParameterSetName -eq 'ComputerName') {
            foreach ($name in @($ComputerName)) {
                if (-not [string]::IsNullOrWhiteSpace($name)) {
                    $names.Add($name) | Out-Null
                }
            }
            return
        }

        $get = @{
            Granularity = $Granularity
        }
        if ($PSBoundParameters.ContainsKey('Start')) { $get['Start'] = $Start }
        if ($PSBoundParameters.ContainsKey('End')) { $get['End'] = $End }
        if ($LastHours) { $get['LastHours'] = $LastHours }
        if ($LastDays) { $get['LastDays'] = $LastDays }
        if ($LastWeeks) { $get['LastWeeks'] = $LastWeeks }
        if ($TimeZone) { $get['TimeZone'] = $TimeZone }
        if ($Path) { $get['Path'] = $Path }
        if ($PSCmdlet.ParameterSetName -eq 'Session') {
            $get['Session'] = $Session
            if ($Credential) { $get['Credential'] = $Credential }
            if ($PSBoundParameters.ContainsKey('Authentication')) { $get['Authentication'] = $Authentication }
            if ($UseSSL) { $get['UseSSL'] = $true }
            if ($Port) { $get['Port'] = $Port }
            if ($SessionOption) { $get['SessionOption'] = $SessionOption }
            if ($ThrottleLimit) { $get['ThrottleLimit'] = $ThrottleLimit }
        }

        $report = Get-SMBeatReport @get
        Write-SMBeatReportFiles -Report $report -OutputPath $OutputPath -Language $Language -CsvCulture $CsvCulture -Theme $Theme -Open:(-not $NoOpen)
    }

    end {
        if ($PSCmdlet.ParameterSetName -ne 'ComputerName') {
            return
        }
        if ($names.Count -eq 0) {
            return
        }

        $get = @{
            ComputerName = $names.ToArray()
            Granularity  = $Granularity
        }
        if ($PSBoundParameters.ContainsKey('Start')) { $get['Start'] = $Start }
        if ($PSBoundParameters.ContainsKey('End')) { $get['End'] = $End }
        if ($LastHours) { $get['LastHours'] = $LastHours }
        if ($LastDays) { $get['LastDays'] = $LastDays }
        if ($LastWeeks) { $get['LastWeeks'] = $LastWeeks }
        if ($TimeZone) { $get['TimeZone'] = $TimeZone }
        if ($Path) { $get['Path'] = $Path }
        if ($Credential) { $get['Credential'] = $Credential }
        if ($PSBoundParameters.ContainsKey('Authentication')) { $get['Authentication'] = $Authentication }
        if ($UseSSL) { $get['UseSSL'] = $true }
        if ($Port) { $get['Port'] = $Port }
        if ($SessionOption) { $get['SessionOption'] = $SessionOption }
        if ($ThrottleLimit) { $get['ThrottleLimit'] = $ThrottleLimit }

        $report = Get-SMBeatReport @get
        Write-SMBeatReportFiles -Report $report -OutputPath $OutputPath -Language $Language -CsvCulture $CsvCulture -Theme $Theme -Open:(-not $NoOpen)
    }
}