Public/General/Export-ComputerSummary.ps1

<#
Copyright © 2024 Integris. For internal company use only. All rights reserved.
#>


FUNCTION Export-ComputerSummary {
    <#
    .SYNOPSIS
    Exports a summary of the computer's hardware and software configuration.
 
    .DESCRIPTION
    This function gathers and returns detailed information about the computer's hardware and software, including the chassis, CPU, RAM, display adapters, network adapters, monitors, peripheral devices, and installed applications. It also exports the summary to a CSV file.
 
    .PARAMETER ExportPath
    Specifies the path where the summary CSV file will be saved.
 
    .EXAMPLE
    Export-ComputerSummary -ExportPath "C:\Exports"
    Exports and displays a summary of the computer's hardware and software configuration, saving the results to the specified path.
 
    .NOTES
    The function uses various helper functions to collect information about the computer's components and installed applications.
    #>


    param(
        [Parameter()][string[]]$ExportPath = "$ModuleExportDir\$($PSCmdlet.MyInvocation.MyCommand.Name)"
    )

    $Results = @() 

    ### Get Info
    $ChassisInfo = Get-ChassisInfo
    $CPUInfo = Get-CPUInfo
    $OSName = (Get-CIMInstance Win32_OperatingSystem).Caption
    
    ### Computer Name
    IF ($True) {
        $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
            Category = "Computer"
            Item = "General"
            ItemDetails = "Hostname"
            ItemValue = $env:COMPUTERNAME
        }
    }

    ### Case Type
    IF ($True) {
        $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
            Category = "Computer"
            Item = "General"
            ItemDetails = "Form Factor"
            ItemValue = $ChassisInfo.ChassisType
        }
    }

    ### Model Type
    IF ($True) {
        $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
            Category = "Computer"
            Item = "General"
            ItemDetails = "Model Name"
            ItemValue = $ChassisInfo.Model
        }
    }

    ### Manufacturer
    IF ($True) {
        $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
            Category = "Computer"
            Item = "General"
            ItemDetails = "Manufacturer"
            ItemValue = $ChassisInfo.Manufacturer
        }
    }

    ### Recent Logins
    IF ($True) {
        $UniqueUsers = ((Get-WindowsLogonEvent).Username | Select-Object -Unique).Value
        $Logins = @()
        FOREACH ($UniqueUser in $UniqueUsers) {
            $Logins += Get-WindowsLogonEvent | Where-Object { $_.Username -eq $UniqueUser -and $_.Type -eq "Logon" } | Sort-Object -Descending -Property Time | Select-Object -First 1
        }
        FOREACH ($Login in $Logins) {
            TRY {
                $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                    Category = "User Logon"
                    Item = "Last Logon Time"
                    ItemDetails = $Login.Username
                    ItemValue = $Login.Time
                }
            }
            CATCH { }
        }
    }

    ### OS Name
    IF ($True) {
        $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
            Category = "Computer"
            Item = "General"
            ItemDetails = "Operating System"
            ItemValue = $OSName
        }
    }

    ### CPU Model
    IF ($True) {
        $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
            Category = "Hardware"
            Item = "CPU"
            ItemDetails = "Model"
            ItemValue = $CPUInfo.CPUModel
        }
    }

    ### RAM
    IF ($True) {
        $RAMModules = Get-RAMInfo

        FOREACH ($RAMModule in $RAMModules) {
            IF ($RAMModule.BankLabel -eq "Empty") { continue }

            $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                Category = "Hardware"
                Item = "RAM"
                ItemDetails = $RAMModule.BankLabel
                ItemValue = $RAMModule.GBs.ToString() + " GB"
            }
        }
    }

    ### Display Adapters
    IF ($True) {
        $DisplayAdapters = Get-GPUInfo

        FOREACH ($DisplayAdapter in $DisplayAdapters) {

            $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                Category = "Hardware"
                Item = "Video"
                ItemDetails = "Model"
                ItemValue = $DisplayAdapter.ModelName
            }
        }
    }

    ### Network Adapters
    IF ($True) {
        $NetworkAdapters = Get-NetAdapter -Physical

        FOREACH ($NetworkAdapter in $NetworkAdapters) {
            $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                Category = "Network"
                Item = "Adapter"
                ItemDetails = ($NetworkAdapter.Name)
                ItemValue = ($NetworkAdapter.InterfaceDescription)
            }
        }
    }

    ### Monitors
    IF ($True) {
        $Monitors = Get-DisplayDevice

        FOREACH ($Monitor in $Monitors) {
            $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                Category = "Peripheral"
                Item = "Hardware"
                ItemDetails = "Monitor"
                ItemValue = $Monitor.ModelName
            }
        }
    }

    ### Peripheral Devices
    IF ($True) {
        $Peripherals = Get-PeripheralDevice

        FOREACH ($Peripheral in $Peripherals) {
            $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                Category = "Peripheral"
                Item = "Hardware"
                ItemDetails = $Peripheral.DeviceType
                ItemValue = $Peripheral.DeviceName
            }
        }
    }

    ### Applications
    IF ($True) {
        $Applications = Get-ApplicationInstalled

        FOREACH ($Application in $Applications) {
            $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                Category = "Software"
                Item = "Application"
                ItemDetails = "Name"
                ItemValue = $Application.Name
            }
        }
    }

    $Results = $Results | Select-Object Category, Item, ItemDetails, ItemValue

    # Export the object to a CSV file with headers
    $ExportFilePath = "$ExportPath\Results_$(Get-Date -Format "yyyy-MM-dd_HHmmss").csv"
    IF (!(Test-Path $ExportPath -PathType Container)) {
        Write-Debug "Creating Folder: $ExportPath"
        New-Item -Path $ExportPath -ItemType Directory -Force | Out-Null
    }
    Write-Host "Exported Results to: $ExportFilePath"
    $Results | Export-Csv -Path $ExportFilePath -NoTypeInformation

    RETURN $Results | Format-Table -GroupBy Category
}
New-Alias -Name Get-ComputerSummary -Value Export-ComputerSummary