script adding muliples of same PC during Inventory scans

Bob, my script  seems to be adding multiple records of  one of the  PCs, and it IP in the PCs.txt is in there only once,  I have even deleted the output file before running script again  and it still make multiple records of that 1  PC .

Otherwise the scripts works pretty well ..


$recipients = “dennis@domain.com"
$smtpsettings = @{
To = $recipients
From = “dennis@domain.com
Subject = “Daily Hard Drive Report”
SmtpServer = “mail.domain.com”
}


$a = @"
<style>
TABLE{border-width: 1px;border-style: solid;border-color:black;}
Table{background-color:#DFFFFF;border-collapse: collapse;}
TH{border-width:1px;padding:10px;border-style:solid;border-color:black;}
TD{border-width:1px;padding-left:10px;border-style:solid;border-color:black;}
TD{border-width:1px;padding-right:10px;border-style:solid;border-color:black;}
</style>
"@


$PCs = Get-Content -Path "C:\TEMP\PCs.txt"

$local = Get-Credential administrator
 $results = @{}
 $results = foreach ($PC in $PCs)
 {
     if (Test-Connection -ComputerName $PC -Count 1 -Quiet)
     {
         Write-Host "Working on $PC"
         $bios = Get-WmiObject Win32_BIOS -ComputerName $PC -Credential $local
         $Proc = Get-WmiObject Win32_processor -ComputerName $PC -Credential $local | Select-Object -First 1
         $memory = Get-WmiObject Win32_physicalmemory -ComputerName $PC -Credential $local
         $system = Get-WmiObject Win32_ComputerSystem -ComputerName $PC -Credential $local
         $os = Get-WmiObject Win32_OperatingSystem -ComputerName $PC -Credential $local
         $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $PC -Credential $local | ? {$_.IPEnabled}
  $uptime = (Get-Date) - ($os.ConvertToDateTime($os.lastbootuptime))
         $DisplayUptime = "Uptime: " + $Uptime.Days + " days, " + $Uptime.Hours + " hours, " + $Uptime.Minutes + " minutes"

 

       [PSCustomObject]@{
        "PC Name" = $os.csname
        "OS Version" = $os.Caption
       InstallDate = $os.ConvertToDateTime($os.installDate)
        "UserName" = $system.UserName
        "Manufacturer" = $system.Manufacturer
        "SystemType" = $system.SystemType
        "Domain" = $system.Domain
        "Model" = $system.Model
        "Processor Name" = $proc.Name
        "Processor Manufacturer" = $proc.Manufacturer
        "Processor Max Clock Speed" = $proc.MaxClockSpeed
        "IPAddress" = $networks.IpAddress[0]
        "SubnetMask" = $Networks.IPSubnet[0]
        "DefaultGateway" = $Networks.DefaultIPGateway[0]
        "DNS Servers" = $Networks.DNSServerSearchOrder[0]
 "Uptime" = $DisplayUptime
         }
     }
  }

#
# $results | ConvertTo-HTML | Out-File P:\Scripts\Inventory.htm
#
 
 
 $results | ConvertTo-HTML -head $a -building "<H1>Server Hard Drive Report</H1>", "<H2>This is a Test</H2>" , "<H2>Here is a report of the PCs you wanted investigated </H2>" | out-file P:\Scripts\InventoryTest.csv

$building = Get-Content -Path "P:\Scripts\InventoryTest.csv" | Out-String

Send-MailMessage @smtpsettings -building $building -BodyAsHTML

  • Laser235 said:
    Bob, my script  seems to be adding multiple records of  one of the  PCs, and it IP in the PCs.txt is in there only once,  I have even deleted the output file before running script again  and it still make multiple records of that 1  PC .

    Yeah, that is unexpected.  If you delete the offending PC, the list is OK?  I would try deleting it and reading it somewhere else in the list.  Either that or take a look at it in Notepad++ with (View | Show Symbol | Show All Characters) on just to make sure the text file is clean.