Private/ComputerStats/_PopulateComputerStats.ps1

function _PopulateComputerStats {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateSet('Software', 'Updates', 'Processes', 'Services', 'FirewallRules', 'FirewallLog')]
        [string]$LookupType,

        [Parameter(Mandatory = $true)]
        [string]$Identity
    )
    switch ($LookupType) {
        # TODO if computer is not reachable, notify in the datagrid
        'Software' {
            $SoftwareListing = _GetInstalledSoftware -Identity $Identity | Select-Object InstallDate, Name, Version, UninstallCommand | Sort-Object Name
            $Headers = [ordered]@{
                'InstallDate'      = [datetime]
                'Name'             = [string]
                'Version'          = [version]
                'UninstallCommand' = [string]
            }
            $Object = $SoftwareListing
            $ComputerStats.Title = 'Installed Software'
        }
        'Updates' {
            $Updates = _GetInstalledUpdates -Identity $Identity | Select-Object Title, Date, Description, Operation | Sort-Object Date -Descending
            $Headers = [ordered]@{
                'Title'       = [string]
                'Date'        = [datetime]
                'Description' = [string]
                'Operation'   = [string]
            }
            $Object = $Updates
            $ComputerStats.Title = 'Installed Updates'
        }
        'Processes' {
            $ScriptBlock = { Get-Process -IncludeUserName }
            try {
                if ($Identity -eq $env:COMPUTERNAME) {
                    $Processes = Invoke-Command -ScriptBlock $ScriptBlock -ErrorAction Stop
                }
                else {
                    $Processes = Invoke-Command @SessionSplat -ComputerName $Identity -ScriptBlock $ScriptBlock -ErrorAction Stop
                }
            }
            catch {
                $Object += [PSCustomObject]@{
                    Error = 'Unable to get processes'
                }
                break
            }
            # display the CPU and memorysize in appropriate measurements
            # TODO cpu and memory doesn't sort as an integer
            $Object = $Processes | Select-Object ProcessName, CPU, @{Name = 'Memory (KB)'; Expression = { [math]::Round($_.PrivateMemorySize / 1KB, 2) } }, Path, UserName, ID | Sort-Object CPU -Descending
            $Headers = [ordered]@{
                'ProcessName' = [string]
                'CPU'         = [int]
                'Memory (KB)' = [int]
                'Path'        = [string]
                'Username'    = [string]
                'ID'          = [int]
            }
            $ComputerStats.Title = 'Running Processes'
        }
        'Services' {
            $ScriptBlock = { Get-Service }
            try {
                if ($Identity -eq $env:COMPUTERNAME) {
                    $Services = Invoke-Command -ScriptBlock $ScriptBlock -ErrorAction Stop
                }
                else {
                    # TODO add the splat here
                    $Services = Invoke-Command @SessionSplat -ComputerName $Identity -ScriptBlock $ScriptBlock -ErrorAction Stop
                }
            }
            catch {
                $Object += [PSCustomObject]@{
                    Error = 'Unable to get services'
                }
                break
            }
            $Object = $Services | Select-Object Name, DisplayName, Status, StartType, @{Name = 'Dependent Services'; Expression = { if ($_.DependentServices.Name) {
                        $_.DependentServices.Name | _OutSingleStringFromArray
                    } }
            }, ID
            $Headers = [ordered]@{
                'Name'               = [string]
                'DisplayName'        = [string]
                'Status'             = [string]
                'StartType'          = [string]
                'Dependent Services' = [string]
                'ID'                 = [int]
            }
            $ComputerStats.Title = 'Running Services'
        }
        'FirewallRules' {
            $ScriptBlock = { Get-NetFirewallRule }
            try {
                if ($Identity -eq $env:COMPUTERNAME) {
                    $FirewallRules = Invoke-Command -ScriptBlock $ScriptBlock -ErrorAction Stop
                }
                else {
                    $FirewallRules = Invoke-Command @SessionSplat -ComputerName $Identity -ScriptBlock $ScriptBlock -ErrorAction Stop
                }
            }
            catch {
                $Object += [PSCustomObject]@{
                    Error = 'Unable to get firewall rules'
                }
                break
            }
            $Object = $FirewallRules
            # $Headers = $Object | Get-Member | Where-Object membertype -Like '*property' | Select-Object -ExpandProperty name
            $Headers = [ordered]@{
                'Enabled'             = [string]
                'DisplayName'         = [string]
                'Profile'             = [string]
                'PrimaryStatus'       = [string]
                'Action'              = [string]
                'Direction'           = [string]
                'Name'                = [string]
                'DisplayGroup'        = [string]
                'Description'         = [string]
                'LocalOnlyMapping'    = [string]
                'LooseSourceMapping'  = [string]
                'Status'              = [string]
                'EdgeTraversalPolicy' = [string]
            }
            $ComputerStats.Title = 'Firewall Rules'
        }
        'FirewallLog' {
            $ScriptBlock = {
                $ProfileName = 'Domain'
                $LastNEntries = '100'

                $ProfileInfo = Get-NetFirewallProfile -PolicyStore activestore
                $FirewallLogLocation = ($ProfileInfo | Where-Object name -EQ $ProfileName | Select-Object -ExpandProperty LogFileName) -replace '%systemroot%', 'C:\Windows'
                if (Test-Path $FirewallLogLocation) {
                    $FirewallLog = Get-Content $FirewallLogLocation
                }
                else {
                    $MessageSplat = @{
                        MessageText  = "File doesn't exist."
                        MessageIcon  = 'Hand'
                        ButtonType   = 'OK'
                        MessageTitle = 'Error'
                    }
                    _ShowMessageBox @MessageSplat
                    break
                }
                $FirewallLog = $FirewallLog | Select-Object -Last $LastNEntries

                $Output = @()
                foreach ($LogEntry in $FirewallLog) {
                    if ($LogEntry -like '2*') {
                        $Output += [PSCustomObject]@{
                            Date            = $LogEntry.Split(' ')[0]
                            Time            = $LogEntry.Split(' ')[1]
                            Action          = $LogEntry.Split(' ')[2]
                            Protocol        = $LogEntry.Split(' ')[3]
                            SourceIP        = $LogEntry.Split(' ')[4]
                            DestinationIP   = $LogEntry.Split(' ')[5]
                            SourcePort      = $LogEntry.Split(' ')[6]
                            DestinationPort = $LogEntry.Split(' ')[7]
                            Size            = $LogEntry.Split(' ')[8]
                            TCPFlags        = $LogEntry.Split(' ')[9]
                            TCPSyn          = $LogEntry.Split(' ')[10]
                            TCPAck          = $LogEntry.Split(' ')[11]
                            TCPWin          = $LogEntry.Split(' ')[12]
                            ICMPType        = $LogEntry.Split(' ')[13]
                            ICMPCode        = $LogEntry.Split(' ')[14]
                            Info            = $LogEntry.Split(' ')[15]
                            Path            = $LogEntry.Split(' ')[16]
                            PID             = $LogEntry.Split(' ')[17]
                        }
                    }
                    else {
                    }
                }
                return $Output
            }
            try {
                if ($Identity -eq $env:COMPUTERNAME) {
                    $FirewallLog = Invoke-Command -ScriptBlock $ScriptBlock -ErrorAction Stop
                }
                else {
                    $FirewallLog = Invoke-Command @SessionSplat -ComputerName $Identity -ScriptBlock $ScriptBlock -ErrorAction Stop
                }
            }
            catch {
                $Object += [PSCustomObject]@{
                    Error = 'Unable to get firewall logs'
                }
                break
            }
            $Object = $FirewallLog

            $Headers = $Object | Get-Member | Where-Object membertype -EQ noteproperty | Select-Object -ExpandProperty name
            $ComputerStats.Title = 'Firewall Log'
        }
    }

    $ComputerStats_FilterComboBox.Clear

    foreach ($Header in $Headers.GetEnumerator()) {
        $ComputerStats_FilterComboBox.Items.Add($Header.Key)
    }

    $Script:ComputerStatsDataTable = New-Object System.Data.DataTable
    foreach ($Header in $Headers.GetEnumerator()) {
        $ComputerStatsDataTable.Columns.Add($Header.Key, $Header.Value)
    }
    foreach ($Item in $Object) {
        $Array = @()
        Foreach ($Header in $Headers.GetEnumerator()) {
            switch ($Header.Value) {
                'string' {
                    $array += [string]$Item.$($Header.Key)
                }
                'int' {
                    $array += [int]$Item.$($Header.Key)
                }
                'version' {
                    $array += [version]$Item.$($Header.Key)
                }
                'datetime' {
                    $array += [datetime]$Item.$($Header.Key)
                }
            }
        }
        [void]$ComputerStatsDataTable.Rows.Add($array)
    }

    $ComputerStats_DataGrid.ItemsSource = $ComputerStatsDataTable.DefaultView
}