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
}}