src/Add-ColumnDataToAllEntriesInArray.ps1

Function Add-ColumnDataToAllEntriesInArray
{
  <#
    .SYNOPSIS
    Adds up to 3 extra columns and data to the object
 
    .DESCRIPTION
    Gives capability to extend the data with for example Computer and UserLoggedOn, which are nice data to have in the inventory
 
    .PARAMETER Data
    Object to modify
 
    .PARAMETER Column1Name
    Name of the column to add (for example Computer)
 
    .PARAMETER Column1Data
    Data to add to the column1 (for example $Env:Computer)
 
    .PARAMETER Column2Name
    Name of the column to add (for example UserLoggedOn)
 
    .PARAMETER Column2Data
    Data to add to the column1 (for example $UserLoggedOn)
 
    .PARAMETER Column3Name
    Name of the column to add (for example ComputerType)
 
    .PARAMETER Column3Data
    Data to add to the column1 (for example $ComputerType)
 
    .INPUTS
    None. You cannot pipe objects
 
    .OUTPUTS
    Updated object with CollectionTime
 
    .EXAMPLE
    #-------------------------------------------------------------------------------------------
    # Variables
    #-------------------------------------------------------------------------------------------
    $Verbose = $true # $true or $false
 
    #-------------------------------------------------------------------------------------------
    # Collecting data (in)
    #-------------------------------------------------------------------------------------------
    $DNSName = (Get-CimInstance win32_computersystem).DNSHostName +"." + (Get-CimInstance win32_computersystem).Domain
    $ComputerName = (Get-CimInstance win32_computersystem).DNSHostName
    [datetime]$CollectionTime = ( Get-date ([datetime]::Now.ToUniversalTime()) -format "yyyy-MM-ddTHH:mm:ssK" )
 
    $UserLoggedOnRaw = Get-Process -IncludeUserName -Name explorer | Select-Object UserName -Unique
    $UserLoggedOn = $UserLoggedOnRaw.UserName
 
    $DataVariable = Get-CimInstance -ClassName Win32_Processor | Select-Object -ExcludeProperty "CIM*"
    $DataVariable
 
    #-------------------------------------------------------------------------------------------
    # Preparing data structure
    #-------------------------------------------------------------------------------------------
    $DataVariable = Convert-CimArrayToObjectFixStructure -data $DataVariable -Verbose:$Verbose
    $DataVariable
 
    # add CollectionTime to existing array
    $DataVariable = Add-CollectionTimeToAllEntriesInArray -Data $DataVariable -Verbose:$Verbose
    $DataVariable
 
    # add Computer & UserLoggedOn info to existing array
    $DataVariable = Add-ColumnDataToAllEntriesInArray -Data $DataVariable -Column1Name Computer -Column1Data $ComputerName -Column2Name UserLoggedOn -Column2Data $UserLoggedOn -Verbose:$verbose
    $DataVariable
 
    #-------------------------------------------------------------------------------------------
    # Output
    #-------------------------------------------------------------------------------------------
    Caption : Intel64 Family 6 Model 165 Stepping 5
    Description : Intel64 Family 6 Model 165 Stepping 5
    InstallDate :
    Name : Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz
    Status : OK
    Availability : 3
    ConfigManagerErrorCode :
    ConfigManagerUserConfig :
    CreationClassName : Win32_Processor
    DeviceID : CPU0
    ErrorCleared :
    ErrorDescription :
    LastErrorCode :
    PNPDeviceID :
    PowerManagementCapabilities :
    PowerManagementSupported : False
    StatusInfo : 3
    SystemCreationClassName : Win32_ComputerSystem
    SystemName : STRV-MOK-DT-02
    AddressWidth : 64
    CurrentClockSpeed : 2904
    DataWidth : 64
    Family : 198
    LoadPercentage : 1
    MaxClockSpeed : 2904
    OtherFamilyDescription :
    Role : CPU
    Stepping :
    UniqueId :
    UpgradeMethod : 1
    Architecture : 9
    AssetTag : To Be Filled By O.E.M.
    Characteristics : 252
    CpuStatus : 1
    CurrentVoltage : 8
    ExtClock : 100
    L2CacheSize : 2048
    L2CacheSpeed :
    L3CacheSize : 16384
    L3CacheSpeed : 0
    Level : 6
    Manufacturer : GenuineIntel
    NumberOfCores : 8
    NumberOfEnabledCore : 8
    NumberOfLogicalProcessors : 16
    PartNumber : To Be Filled By O.E.M.
    ProcessorId : BFEBFBFF000A0655
    ProcessorType : 3
    Revision :
    SecondLevelAddressTranslationExtensions : False
    SerialNumber : To Be Filled By O.E.M.
    SocketDesignation : U3E1
    ThreadCount : 16
    Version :
    VirtualizationFirmwareEnabled : False
    VMMonitorModeExtensions : False
    VoltageCaps :
    PSComputerName :
    CollectionTime : 12-03-2023 16:19:12
    Computer : STRV-MOK-DT-02
    UserLoggedOn : 2LINKIT\mok#>

  #>

    [CmdletBinding()]
    param(
            [Parameter(mandatory)]
                [Array]$Data,
            [Parameter(mandatory)]
                [string]$Column1Name,
            [Parameter(mandatory)]
                [string]$Column1Data,
            [Parameter()]
                [string]$Column2Name,
            [Parameter()]
                [string]$Column2Data,
            [Parameter()]
                [string]$Column3Name,
            [Parameter()]
                [string]$Column3Data
         )

    Write-Verbose " Adding columns to all entries in array .... please wait !"
    $IntermediateObj = @()
    ForEach ($Entry in $Data)
        {
            If ($Column1Name)
                {
                    $Entry | Add-Member -MemberType NoteProperty -Name $Column1Name -Value $Column1Data -Force
                }

            If ($Column2Name)
                {
                    $Entry | Add-Member -MemberType NoteProperty -Name $Column2Name -Value $Column2Data -Force
                }

            If ($Column3Name)
                {
                    $Entry | Add-Member -MemberType NoteProperty -Name $Column3Name -Value $Column3Data -Force
                }

            $IntermediateObj += $Entry
        }
    return [array]$IntermediateObj
}