Public/Show-SMBeat.ps1

#Requires -Version 5.1

function Show-SMBeat {
    <#
    .SYNOPSIS
        Builds and exports an HTML/CSV report with defaults.
    .DESCRIPTION
        Aggregates samples and writes HTML/CSV in one step.
        Default window is all samples still on disk (first listen to now).
        Files go to Documents\SMBeat-Reports on the machine that runs the cmdlet.
        Theme is health. Language follows the UI of that machine.
    .PARAMETER NoOpen
        Write the files but do not open the HTML report. Default is to open it.
    .PARAMETER PassThru
        Return the report object after writing the files (for scripts).
    #>

    [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', 'dark')]
        [string]$Theme = 'health',
        [switch]$NoOpen,
        [switch]$PassThru
    )

    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)
        if ($PassThru) {
            $report
        }
    }

    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)
        if ($PassThru) {
            $report
        }
    }
}