Public/Invoke-SMBeatSample.ps1

#Requires -Version 5.1

function Invoke-SMBeatSample {
    <#
    .SYNOPSIS
        Asks the resident collector to flush current TCP 445 deltas now.
    .DESCRIPTION
        The collector must already be running (Register-SMBeatCollector). This cmdlet
        writes a flush request and waits until the watcher appends a sample.
        Remote CIM sampling is not supported; ESTATA is local to the file server.
    #>

    [CmdletBinding(DefaultParameterSetName = 'Local')]
    param(
        [Parameter(ParameterSetName = 'ComputerName', ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('CN')]
        [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,

        [string]$Path
    )

    begin {
        Assert-SMBeatRemotingExclusive -ComputerName $ComputerName -CimSession $null -Session $Session
        $localSplat = @{}
        if ($Path) { $localSplat['Path'] = $Path }
    }

    process {
        if ($PSCmdlet.ParameterSetName -eq 'ComputerName' -or $PSCmdlet.ParameterSetName -eq 'Session') {
            $sb = {
                param($Splat)
                Import-Module SMBeat -ErrorAction Stop
                if ($Splat.Path) {
                    Invoke-SMBeatSample -Path $Splat.Path
                }
                else {
                    Invoke-SMBeatSample
                }
            }
            $invoke = @{
                ScriptBlock   = $sb
                ArgumentList  = @($localSplat)
                ThrottleLimit = $ThrottleLimit
            }
            if ($PSCmdlet.ParameterSetName -eq 'Session') {
                $invoke['Session'] = $Session
            }
            else {
                $invoke['ComputerName'] = $ComputerName
                if ($Credential) { $invoke['Credential'] = $Credential }
                if ($PSBoundParameters.ContainsKey('Authentication')) { $invoke['Authentication'] = $Authentication }
                if ($UseSSL) { $invoke['UseSSL'] = $true }
                if ($Port) { $invoke['Port'] = $Port }
                if ($SessionOption) { $invoke['SessionOption'] = $SessionOption }
            }
            Invoke-SMBeatRemoteCommand @invoke
            return
        }

        Invoke-SMBeatSampleInternal @localSplat
    }
}