[New to Powershell] Need help in exporting data to csv/html

This topic contains 4 replies, has 2 voices, and was last updated by  ABHAY PATIL 4 years, 1 month ago.

  • Author
    Posts
  • #11402

    ABHAY PATIL
    Participant

    In following script I would like to generate CSV file with only 1 common header, currently script is generating separate header information for each computer record. Also, is it possible to eliminate duplicate rows if computer name matches?

    Any help if really appreciated.
    ______________________________________________________________________

    function inventory {
    PROCESS {
    $cs = gwmi Win32_ComputerSystem -comp $_ |select Status,Name,Domain,Manufacturer,Model,NUmberOfLogicalProcessors,NumberOfProcessors,@{n="Memory";e={$_.TotalPhysicalMemory/1gb -as [int]}}
    $wp = gwmi Win32_Processor -comp $_ | select Name, NumberOfCores -Unique
    $cs | Add-Member NoteProperty 'Processor' $wp.Name -force
    $cs | Add-Member NoteProperty NumberofCores ($wp.NumberOfCores * $wp.NumberOfProcessors) -Force
    write-output $cs
    }
    }

    $ipstart = "10.229.168."
    $ipend = 11

    do {
    If (Test-Connection $ipstart$ipend -quiet -count 2 -TimeToLive 2)
    {
    "$ipstart$ipend" | inventory | format-table
    $ipend++

    }

    } while ($ipend -le 15)

  • #11403

    Dave Wyatt
    Moderator

    You'll need to move the call to Format-Table outside your loop to get the objects to be displayed with a single header. Here's one way of accomplishing this, which requires very few changes to your existing code:

    $scriptBlock =  {
        $ipstart = "10.229.168."
        $ipend = 11
        
        do {
            If (Test-Connection $ipstart$ipend -quiet -count 2 -TimeToLive 2)
            {
                "$ipstart$ipend" | inventory
                $ipend++
            }
        } while ($ipend -le 15)
    }
    
    & $scriptBlock | Select-Object -Unique | Format-Table
    

    Putting the do/while loop inside a script block (or a function, if you prefer to do it that way) allows you to pipe its results to other cmdlets.

  • #11404

    ABHAY PATIL
    Participant

    I have updated script as above script was not having export-csv function (still not solved problem). Also attached its output
    _______________________________________________________________________________________________________
    function inventory {
    PROCESS {
    $cs = gwmi Win32_ComputerSystem -comp $_ |
    select Status,Name,Domain,Manufacturer,Model,NUmberOfLogicalProcessors,NumberOfProcessors,@{n="Memory";e={$_.TotalPhysicalMemory/1gb -as [int]}}
    $wp = gwmi Win32_Processor -comp $_ | select Name, NumberOfCores -Unique
    $cs | Add-Member NoteProperty 'Processor' $wp.Name -force
    $cs | Add-Member NoteProperty NumberofCores ($wp.NumberOfCores * $wp.NumberOfProcessors) -Force
    write-output $cs
    }
    }

    $ipstart = "10.229.168."
    $ipend = 11

    do {
    If (Test-Connection $ipstart$ipend -quiet -count 2 -TimeToLive 2)
    {
    "$ipstart$ipend" | inventory | export-csv 'C:\inventory.csv' -Append
    $ipend++

    }

    } while ($ipend -le 15)

  • #11406

    Dave Wyatt
    Moderator

    I don't see any duplicate headers in your attached CSV file. Either way, I prefer to make one call to Export-Csv outside the loop, with the same technique I posted before. This also makes it easy to use Select-Object -Unique to filter out the duplicates.

    $scriptBlock =  {
        $ipstart = "10.229.168."
        $ipend = 11
        
        do {
            If (Test-Connection $ipstart$ipend -quiet -count 2 -TimeToLive 2)
            {
                "$ipstart$ipend" | inventory
                $ipend++
            }
        } while ($ipend -le 15)
    }
    
    & $scriptBlock | Select-Object -Unique | Export-Csv 'c:\inventory.csv' -NoTypeInformation
    
    
  • #11407

    ABHAY PATIL
    Participant

    Thank You Dave! You solution perfectly works for me.

    Thank you once again!

You must be logged in to reply to this topic.