Why the Duplicate Rows

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of ca ca 4 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #40442
    Profile photo of ca
    ca
    Participant

    Hello,
    I'm doing a pretty straightforward inventory of workstations in my environment, but for some reason the script adds duplicate rows to the csv and I'm not sure why. For some PCs there are 17 duplicate entries... Anyone have suggestions?


    $pc = Get-Content "C:\scripts\75700_pc_inv.txt"

    $tblname = "Computer Inventory"
    $tbl = New-Object system.Data.DataTable "$tblname"
    $col1 = New-Object system.Data.DataColumn ComputerName,([string])
    $col2 = New-Object system.Data.DataColumn Vendor,([string])
    $col3 = New-Object system.Data.DataColumn Model,([string])
    $col4 = New-Object system.Data.DataColumn Product,([string])
    $col5 = New-Object system.Data.DataColumn SerialNumber,([string])
    $col6 = New-Object system.Data.DataColumn OS,([string])
    $col7 = New-Object system.Data.DataColumn RAM,([string])
    $col8 = New-Object system.Data.DataColumn DiskSize,([string])
    $tbl.Columns.add($col1)
    $tbl.Columns.add($col2)
    $tbl.Columns.add($col3)
    $tbl.Columns.add($col4)
    $tbl.Columns.add($col5)
    $tbl.Columns.add($col6)
    $tbl.Columns.add($col7)
    $tbl.Columns.add($col8)

    foreach ($name in $pc) {

    $row = $tbl.NewRow()

    $csp = Get-WmiObject Win32_ComputerSystemProduct -ComputerName $name
    $cspV = $csp.Vendor
    $cspM = $csp.Version
    $cspM2 = $csp.Name
    $cspS = $csp.IdentifyingNumber

    $OS = (Get-WmiObject Win32_OperatingSystem -ComputerName $name).Caption

    $R = 0
    $ram = Get-WmiObject Win32_PhysicalMemory -ComputerName $name | `
    % {$R += $_.Capacity}

    $ddS = (Get-WmiObject win32_diskDrive -ComputerName $name).Size

    $row.ComputerName = $name
    $row.Vendor=$cspV
    $row.Model=$cspM
    $row.Product=$cspM2
    $row.SerialNumber=$cspS
    $row.OS=$OS
    $row.RAM=$R
    $row.DiskSize=$ddS

    $tbl.Rows.Add($row)
    $tbl | export-csv "C:\scripts\$tblname.csv" -noType -Append

    }}

    #40446
    Profile photo of ca
    ca
    Participant

    **ignore the second closed bracket

    #40468
    Profile photo of Anthony Stringer
    Anthony Stringer
    Participant

    try this.
    the notable change is the first line. trim will remove blank spaces before and after the computer names, and select unique will make sure there is only one of each computer in the list.
    also, use [pre] and [/pre] before and after your code respectively (use greater than and less than instead of [])

    $pc = Get-Content 'C:\scripts\75700_pc_inv.txt' | % {$_.Trim()} | select -Unique
    
    $results = foreach ($name in $pc) {
        $csp = Get-WmiObject Win32_ComputerSystemProduct -ComputerName $name
        $cspV = $csp.Vendor
        $cspM = $csp.Version
        $cspM2 = $csp.Name
        $cspS = $csp.IdentifyingNumber
    
        $OS = (Get-WmiObject Win32_OperatingSystem -ComputerName $name).Caption
    
        $R = 0
        $ram = Get-WmiObject Win32_PhysicalMemory -ComputerName $name | % {$R += $_.Capacity}
    
        $ddS = (Get-WmiObject win32_diskDrive -ComputerName $name).Size
    
        [pscustomobject]@{
            ComputerName = $name
            Vendor = $cspV
            Model = $cspM
            Product = $cspM2
            SerialNumber = $cspS
            OS = $OS
            RAM = $R
            DiskSize = $ddS
        }
    }
    
    $results
    
    $results | Export-Csv 'C:\scripts\Computer Inventory.csv' -NoTypeInformation
    
    #40755
    Profile photo of ca
    ca
    Participant

    Hey! Anthony, thanks for the pointers. So after testing each of your mods, it turned out to be the way the results were being put into the table.

    If I kept the export-csv inside the Foreach loop, duplicate values were between 15 to 4 each PC. But if assigned a variable to the entire loop and then exported it as you did, no duplicates! I have no idea why but thanks again.

    I also wasn't aware of creating a hash table using [pscustomobject]@{} but its definitely cleaner. I was originally creating a hash table but stopped halfway through for some reason and gave the table some action.

    ca

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.