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

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)

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.

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)

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

Thank You Dave! You solution perfectly works for me.

Thank you once again!