Private/ComputerStats/_PopulateComputerStats.ps1
function _PopulateComputerStats { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [ValidateSet('Software', 'Updates', 'Processes', 'Services')] [string]$LookupType, [Parameter(Mandatory = $true)] [string]$Identity ) $Object = @() 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 $Object += [PSCustomObject]$SoftwareListing $ComputerStats.Title = 'Installed Software' } 'Updates' { $Updates = _GetInstalledUpdates -Identity $Identity | Select-Object Title, Date, Description, Operation | Sort-Object Date -Descending $Object += [PSCustomObject]$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 $Object += [PSCustomObject]$Processes | Select-Object ProcessName, CPU, @{n="Memory (KB)";e={[math]::Round($_.PrivateMemorySize/1KB,2)}}, Path, UserName, ID | Sort-Object CPU -Descending $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 += [PSCustomObject]$Services = $Services | Select-Object Name, DisplayName, Status, StartType, @{Name = 'Dependent Services'; Expression = { if ($_.DependentServices.Name) { $_.DependentServices.Name | _OutSingleStringFromArray } } } $ComputerStats.Title = 'Running Services' } Default { } } $ComputerStats_DataGrid.ItemsSource = $Object } |