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 = @( 'InstallDate' 'Name' 'Version' 'UninstallCommand' ) $Object = $SoftwareListing $ComputerStats.Title = 'Installed Software' } 'Updates' { $Updates = _GetInstalledUpdates -Identity $Identity | Select-Object Title, Date, Description, Operation | Sort-Object Date -Descending $Headers = @( 'Title' 'Date' 'Description' 'Operation' ) $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 = @( 'ProcessName' 'CPU' 'Memory (KB)' 'Path' 'Username' 'ID' ) $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 } } } $Headers = @( 'Name' 'DisplayName' 'Status' 'StartType' 'Dependent Services' 'ID' ) $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 = @( 'Enabled' 'DisplayName' 'Profile' 'PrimaryStatus' 'Action' 'Direction' 'Name' 'DisplayGroup' 'Description' 'LocalOnlyMapping' 'LooseSourceMapping' 'Status' 'EdgeTraversalPolicy' ) $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' } Default { Write-Verbose 'hit the default.' } } $ComputerStats_FilterComboBox.Clear foreach ($Header in $Headers) { $ComputerStats_FilterComboBox.Items.Add($Header) } $Script:ComputerStatsDataTable = New-Object System.Data.DataTable $ComputerStatsDataTable.Columns.AddRange($Headers) foreach ($Item in $Object) { $Array = @() Foreach ($Header in $Headers) { $array += $Item.$Header } [void]$ComputerStatsDataTable.Rows.Add($array) } $ComputerStats_DataGrid.ItemsSource = $ComputerStatsDataTable.DefaultView } |