HWInformation.psm1

<#
    .NOTES
    --------------------------------------------------------------------------------
     Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2023 v5.8.220
     Generated on: 2023-05-17 3:30
     Generated by: hacks
    --------------------------------------------------------------------------------
    .DESCRIPTION
        Script generated by PowerShell Studio 2023
#>


using namespace System.Xml
using namespace System.Management.Automation.PSTypeName
using module @{ ModuleName = 'GPUInfo'; ModuleVersion = '1.3.2.0' }

    $ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
    #region Functions

    function New-HWInfo ()
    {
        <#
        .SYNOPSIS
        Creates a hardware inventory snapshot for the local Windows computer.
        .DESCRIPTION
        Returns a new HWInfo object containing CPU, memory and display adapter data. It reads Windows inventory through CIM.
        .EXAMPLE
        $machine = New-HWInfo
        $machine.GPUs | Select-Object Name, Vendor
 
        Reuse one snapshot to list the graphics adapters.
 
        .EXAMPLE
        $machine = New-HWInfo
        $machine | Select-Object CPU, CPUCores, CPUThreads, 'Memory (Gb)', RamSticks
 
        Review the processor and physical memory from one collection.
 
        .EXAMPLE
        New-HWInfo -WhatIf
 
        Preview the inventory operation without constructing the snapshot.
 
        .LINK
        about_HWInformation
        .EXTERNALHELP HWInformation.psm1-Help.xml
        #>


        [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
        [OutputType([HWInfo])]
        param ()

        process
        {
            if ($PSCmdlet.ShouldProcess("ShouldProcess?"))
            {
                return [HWInfo]::new()
            }
        }
    }

    function Get-HWInfo ()
    {
        <#
        .SYNOPSIS
        Gets CPU, memory and display adapter information for this computer.
        .DESCRIPTION
        Returns a freshly collected HWInfo object. Store the result if you need to read several properties without recollecting the inventory.
        .EXAMPLE
        Get-HWInfo | Select-Object CPU, CPUCores, CPUThreads, 'Memory (Gb)'
 
        Display a compact processor and RAM summary.
 
        .EXAMPLE
        $machine = Get-HWInfo
        $machine.RAMBytes
        $machine.GPUs | Select-Object Name, DriverVersion, MemoryBytes
 
        Read the exact installed RAM and display the GPU driver and dedicated memory from the same snapshot.
 
        .EXAMPLE
        $machine = Get-HWInfo
        $machine | Select-Object CPU, CPUCores, CPUThreads, 'Memory (Gb)', RamSticks |
            Export-Csv -LiteralPath '.\hardware.csv' -NoTypeInformation -Encoding UTF8
 
        Record a compact inventory of processor and RAM data in a CSV file.
 
        .LINK
        about_HWInformation
        .EXTERNALHELP HWInformation.psm1-Help.xml
        #>


        [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
        [OutputType([HWInfo])]
        param ()

        process
        {
            if ($PSCmdlet.ShouldProcess("ShouldProcess?"))
            {
                return [HWInfo]::new()
            }
        }
    }

    #endregion

    #region Classes
    class HWInfo {

        [string]$CPU
        [string[]]$CPU_ID
        [int]$CPUCores
        [int]$CPUThreads
        [int]${L2Cache (Kb)}
        [int]${L3Cache (Kb)}
        hidden [int64]$RAMBytes
        [int32]${Memory (Gb)}
        [int]$RamSticks
        [GPUInfo[]]$GPUs

        HWInfo ()
        {
            $CPUInfo = Get-HWProcessor
            $this.GetSystemSpecs($CPUInfo)
            $this.GPUs = [GPUInfo]::GetGPU()
        }

        HWInfo ([CimInstance]$CPU, [CimInstance[]]$VideoCards)
        {
            if (! (($VideoCards)[0].psbase.CimClass.CimClassName -like 'Win32_VideoController'))
            {
                $E = [System.Management.Automation.ErrorRecord]::new([System.FormatException]::new("One of the identified items was in an invalid format.`r`nItem is not: [CimInstance][Win32_VideoController]."), 'InvalidTypeException', [System.Management.Automation.ErrorCategory]::InvalidType, $VideoCards)
                $E.CategoryInfo.TargetType = ($VideoCards)[0].psbase.CimClass.CimClassName
                throw $E
            }

            if (! (($CPU)[0].psbase.CimClass.CimClassName -like 'Win32_Processor'))
            {
                $E = [System.Management.Automation.ErrorRecord]::new([System.FormatException]::new("One of the identified items was in an invalid format.`r`nItem is not: [CimInstance][Win32_Processor]."), 'InvalidTypeException', [System.Management.Automation.ErrorCategory]::InvalidType, $CPU)
                $E.CategoryInfo.TargetType = ($CPU)[0].psbase.CimClass.CimClassName
                throw $E
            }

            $this.GetSpecs($CPU, $VideoCards)

        }

        [void] GetSpecs ($CPUInfo, $VideoCards)
        {
            $this.GetSystemSpecs($CPUInfo)
            $this.GPUs = [GPUInfo]::GetGPU($VideoCards)
        }

        hidden [void] GetSystemSpecs($CPUInfo)
        {
            $this.CPU = $CPUInfo.Name
            $this.CPU_ID = $CPUInfo.Caption.Split(' ')[4]
            $this.CPUCores = $CPUInfo.NumberOfCores
            $this.CPUThreads = $CPUInfo.NumberOfLogicalProcessors
            $this.'L2Cache (Kb)' = $CPUInfo.L2CacheSize
            $this.'L3Cache (Kb)' = $CPUInfo.L3CacheSize
            $this.RamSticks = 0
            $this.RAMBytes = 0
            (Get-HWMemoryModule).Capacity | % { $this.RAMBytes += $_; $this.RamSticks += 1 }
            $this.'Memory (Gb)' = [math]::round($this.RAMBytes / 1Gb, 2)
        }
    }
    #endregion

    #region Variables
    $DotSourced = $false
    if ($MyInvocation.invocationname -like '.') { $DotSourced = $true }
    if ($DotSourced) {}
    #endregion

    #region Cleanup
    Remove-Variable DotSourced
    #endregion

    if ($MyInvocation.MyCommand.Name.Split(".")[1] -like 'ps1' -or $MyInvocation.MyCommand.Name.Split(".")[0] -match 'psf')
    {
        Write-Debug 'Dot Sourced: ' + $Host.Name
        return
    }
    else
    {
    }



# Make the public classes discoverable by type-name completion in the shell.
if ($ExecutionContext.SessionState.Module) {
    $completionTypes = @([HWInfo])
    $completionRegistry = [psobject].Assembly.GetType('System.Management.Automation.TypeAccelerators')
    $completionExisting = $completionRegistry::Get
    foreach ($completionType in $completionTypes) {
        if ($completionExisting.ContainsKey($completionType.Name) -and
            $completionExisting[$completionType.Name] -ne $completionType) {
            throw "The type name '$($completionType.Name)' is already registered for a different class. Start a fresh PowerShell session to load HWInformation."
        }
    }
    $completionOwned = @{}
    foreach ($completionType in $completionTypes) {
        if (!$completionExisting.ContainsKey($completionType.Name)) {
            $completionRegistry::Add($completionType.Name, $completionType)
            $completionOwned[$completionType.Name] = $completionType
        }
    }
    $ExecutionContext.SessionState.Module.OnRemove = {
        foreach ($completionEntry in $completionOwned.GetEnumerator()) {
            if ($completionRegistry::Get[$completionEntry.Key] -eq $completionEntry.Value) {
                $null = $completionRegistry::Remove($completionEntry.Key)
            }
        }
    }.GetNewClosure()
    Remove-Variable completionTypes, completionRegistry, completionExisting, completionOwned, completionType
}