src/Add-CollectionTimeToAllEntriesInArray.ps1

Function Add-CollectionTimeToAllEntriesInArray
{
  <#
    .SYNOPSIS
    Add property CollectionTime (based on current time) to all entries on the object
 
    .DESCRIPTION
    Gives capability to do proper searching in queries to find latest set of records with same collection time
    Time Generated cannot be used when you are sending data in batches, as TimeGenerated will change
    An example where this is important is a complete list of applications for a computer. We want all applications to
    show up when queriying for the latest data
 
    .VERSION
    1.0
 
    .AUTHOR
    Morten Knudsen, Microsoft MVP - https://mortenknudsen.net
 
    .LINK
    https://github.com/KnudsenMorten/AzLogDcrIngestPS
 
    .PARAMETER Data
    Object to modify
 
    .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*"
 
    #-------------------------------------------------------------------------------------------
    # Preparing data structure
    #-------------------------------------------------------------------------------------------
    $DataVariable = Convert-CimArrayToObjectFixStructure -data $DataVariable -Verbose:$Verbose
    $DataVariable
 
    # add CollectionTime to existing array
    $DataVariable = Add-CollectionTimeToAllEntriesInArray -Data $DataVariable -Verbose:$Verbose
    $DataVariable
 
    #-------------------------------------------------------------------------------------------
    # Output
    #-------------------------------------------------------------------------------------------
 
    VERBOSE: Adding CollectionTime to all entries in array .... please wait !
    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:08:33
  #>


    [CmdletBinding()]
    param(
            [Parameter(mandatory)]
                [Array]$Data
         )

    [datetime]$CollectionTime = ( Get-date ([datetime]::Now.ToUniversalTime()) -format "yyyy-MM-ddTHH:mm:ssK" )

    Write-Verbose " Adding CollectionTime to all entries in array .... please wait !"

    $IntermediateObj = @()
    ForEach ($Entry in $Data)
        {
            $Entry | Add-Member -MemberType NoteProperty -Name CollectionTime -Value $CollectionTime -Force | Out-Null

            $IntermediateObj += $Entry
        }

    return [array]$IntermediateObj
}