Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Duplicates in the results of my scan...not sure why?

$
0
0

Hello,

I'm using the following bit of code to scan all computers in a file to look for the IP Address, Subnet Mask, DNS Servers, MAC Address and Gateway.

It's working, but I'm seeing in my results sometimes multiple lines of exactly the same information for some of the computers I'm scanning?  I'm asking this community if they can think of why this would occur?

Thank you.

Here is my code:

$dataColl = @()#Makes an array, or a collection to hold all the object of the same fields.

$outputresults = "C:\Apps\Powershell_Scripts\Diskspace\subnetscan.csv"

#process {
 foreach ($Computer in (get-content C:\Apps\Powershell_Scripts\Diskspace\servers.txt))
  {
  if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
   $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
   foreach ($Network in $Networks) {
    $IPAddress  = $Network.IpAddress[0]
    $SubnetMask  = $Network.IPSubnet[0]
    $DefaultGateway = $Network.DefaultIPGateway
    $DNSServers  = $Network.DNSServerSearchOrder
    $IsDHCPEnabled = $false
    If($network.DHCPEnabled) {
     $IsDHCPEnabled = $true
    }
    $MACAddress  = $Network.MACAddress
    $OutputObj  = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value ($IPAddress -join ",")
    #$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
    #$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway
    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join “,”)
    #$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value ($DNSServers -join ",")
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
    $dataColl += $OutputObj

      $dataColl | Export-Csv -noTypeInformation -path $outputresults
   
   }
  }
 }


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>