locked
array and array list with custom object RRS feed

  • Question

  • Hello,

    I would like to know what is/are the best possibility for this function :

    (write-ezlog is function from a powershell module to provide log in a file)

    $allDCs = (Get-ADForest).Domains | Foreach-Object { Get-ADDomainController -Filter * -Server $_ } | Sort-Object name


    function Get-AllDCHardware {

    $obj=@() Write-EZLog -Category INF -Message "### Start Get-AllDCHardware ###" $allDCs | Foreach-Object { Write-EZLog -Category INF -Message "Process Domain controller $($_.hostname)" $obj+=[PSCustomObject]@{ ServerName = $_.name LogicalCPUNb = (Get-CimInstance win32_computersystem -ComputerName $_.hostname).numberOfLogicalProcessors 'RAM(GB)' = (Get-CimInstance win32_computersystem -ComputerName $_.hostname).TotalPhysicalMemory/1GB -as [INT] Type = (get-CimInstance win32_computersystem -ComputerName $_.hostname).model OS = (Get-CimInstance Win32_OperatingSystem -ComputerName $_.hostname).caption LastBoot = (Get-CimInstance -ClassName win32_operatingsystem -ComputerName $_.hostname).lastbootuptime } Write-EZLog -Category INF -Message "object created. ServerName = $($obj.ServerName) ; LogicalCPUNb = $($obj.LogicalCPUNb) ; RAM(GB) = $($obj.'RAM(GB)')" Write-EZLog -Category INF -Message "Type = $($obj.Type) ; OS = $($obj.OS) ; LastBoot = $($obj.LastBoot)" Write-EZLog -Category INF -Message "End Domain controller $($_.hostname)" } $obj Write-EZLog -Category INF -Message "### End Get-AllDCHardware ###" }

    or

    function Get-AllDCHardware
    {
        $obj=[System.Collections.ArrayList]@()
        Write-EZLog -Category INF -Message "### Start Get-AllDCHardware ###"
        $allDCs | Foreach-Object {
            Write-EZLog -Category INF -Message "Process Domain controller $($_.hostname)"
            $obj.add([PSCustomObject]@{
                ServerName = $_.name
                LogicalCPUNb = (Get-CimInstance win32_computersystem -ComputerName $_.hostname).numberOfLogicalProcessors
                'RAM(GB)' = (Get-CimInstance win32_computersystem -ComputerName $_.hostname).TotalPhysicalMemory/1GB -as [INT]
                Type = (get-CimInstance win32_computersystem -ComputerName $_.hostname).model
                OS = (Get-CimInstance Win32_OperatingSystem -ComputerName $_.hostname).caption
                LastBoot = (Get-CimInstance -ClassName win32_operatingsystem -ComputerName $_.hostname).lastbootuptime
            })
            Write-EZLog -Category INF -Message "object created. ServerName = $($obj.ServerName) ; LogicalCPUNb = $($obj.LogicalCPUNb) ; RAM(GB) = $($obj.'RAM(GB)')"
            Write-EZLog -Category INF -Message "Type = $($obj.Type) ; OS = $($obj.OS) ; LastBoot = $($obj.LastBoot)"
            Write-EZLog -Category INF -Message "End Domain controller $($_.hostname)"
        }
        $obj
        Write-EZLog -Category INF -Message "### End Get-AllDCHardware ###"
    }

    If I wouldn't want to log it I would have only to do this

    function Get-AllDCHardware { $allDCs | Foreach-Object { [PSCustomObject]@{ ServerName = $_.name LogicalCPUNb = (Get-CimInstance win32_computersystem -ComputerName $_.hostname).numberOfLogicalProcessors 'RAM(GB)' = (Get-CimInstance win32_computersystem -ComputerName $_.hostname).TotalPhysicalMemory/1GB -as [INT] Type = (get-CimInstance win32_computersystem -ComputerName $_.hostname).model OS = (Get-CimInstance Win32_OperatingSystem -ComputerName $_.hostname).caption LastBoot = (Get-CimInstance -ClassName win32_operatingsystem -ComputerName $_.hostname).lastbootuptime }

    } }



    Merci de marquer comme reponses les interventions qui vous ont ete utile.



    Tuesday, June 9, 2020 2:40 PM

Answers

  • The answer depends on what you need to use the array for. Mostly either will work.  When working with forms the ArrayList may be required.  Other code may require an ArrayList.  Most code does not require an ArrayList.

    The following lines are mostly equivalent:

    $myobj = [PSCustomObject]@{
        ServerName = "aaaa"
        LogicalCPUNb = "bbbb"
    }
    
    [System.Collections.ArrayList]$list += $myobj
    [array]$array += $obj
    

    The major difference is that an array cannot be altered without recreating the array where an ArrayList can be added, removed and inserted.

    We can also turn any array into an ArrayList as follows:

    PS C:\scripts> $a = 1,2,3,4,5
    PS C:\scripts> $a.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Object[]                                 System.Array
    
    
    PS C:\scripts> $al = [System.Collections.ArrayList]$a
    PS C:\scripts> $al.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     ArrayList                                System.Object
    
    
    PS C:\scripts>
    

     

    \_(ツ)_/

    • Marked as answer by matteu31400 Tuesday, June 9, 2020 9:07 PM
    Tuesday, June 9, 2020 7:33 PM

All replies

  • I recommend contacting the author of your logging module for help with the module.  How and what you log is not something we can determine.  It depends on what you think needs to be logged.

    "Start-Transcript" is the best thing to use for troubleshooting logs.


    \_(ツ)_/

    Tuesday, June 9, 2020 4:52 PM
  • Thank you for your answer.

    I would like to know if it's best to use :

    $obj=[System.Collections.ArrayList]@()

    $obj.add([PSCustomObject]@{ ServerName = "aaaa" LogicalCPUNb = "bbbb"

    })

    or

    $obj=@()

    $obj +=[PSCustomObject]@{ ServerName = "aaaa" LogicalCPUNb = "bbbb"

    }

    thanks




    Merci de marquer comme reponses les interventions qui vous ont ete utile.


    Tuesday, June 9, 2020 7:14 PM
  • The answer depends on what you need to use the array for. Mostly either will work.  When working with forms the ArrayList may be required.  Other code may require an ArrayList.  Most code does not require an ArrayList.

    The following lines are mostly equivalent:

    $myobj = [PSCustomObject]@{
        ServerName = "aaaa"
        LogicalCPUNb = "bbbb"
    }
    
    [System.Collections.ArrayList]$list += $myobj
    [array]$array += $obj
    

    The major difference is that an array cannot be altered without recreating the array where an ArrayList can be added, removed and inserted.

    We can also turn any array into an ArrayList as follows:

    PS C:\scripts> $a = 1,2,3,4,5
    PS C:\scripts> $a.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Object[]                                 System.Array
    
    
    PS C:\scripts> $al = [System.Collections.ArrayList]$a
    PS C:\scripts> $al.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     ArrayList                                System.Object
    
    
    PS C:\scripts>
    

     

    \_(ツ)_/

    • Marked as answer by matteu31400 Tuesday, June 9, 2020 9:07 PM
    Tuesday, June 9, 2020 7:33 PM
  • OK, thank you for your answer.

    I saw arraylist is really really faster for lot of record because it's exactly what you write here :Arraylist can add element but array need to rebuild it each time a new element need to be added.

    Perfect explanations !


    Merci de marquer comme reponses les interventions qui vous ont ete utile.

    Tuesday, June 9, 2020 9:07 PM