Author Posts

November 12, 2013 at 8:04 pm

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)

November 12, 2013 at 8:35 pm

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.

November 12, 2013 at 8:42 pm

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)

November 12, 2013 at 8:47 pm

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

November 12, 2013 at 8:48 pm

Thank You Dave! You solution perfectly works for me.

Thank you once again!