How do I apply a ForEach loop to a computer list to determine valid IP address information?

Set-StrictMode -Version Latest
Set-ExecutionPolicy remotesigned -Force
import-module activedirectory

$ComputerName = $env:ComputerName

Get-ADComputer -Filter '*' -SearchBase "OU=Computers, DC=sample,DC=com" -Properites Name | % {
    $PC = $_Name
begin {}
process {
 foreach ($Computer in $ComputerName) {
  if(Test-Connection -ComputerName $Computer -Count 1 -TimeToLive -Quiet)
  {
   $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
 
   foreach ($Network in $Networks) {
     $IsDHCPEnabled = $false    
            "ComputerName"   = $_.ComputerName
            "IPAddress"      = $_.IpAddress
            "SubnetMask"     = $_.IPSubnet
            "DefaultGateway" = $_.DefaultIPGateway
            "DNSServers"     = $_.DNSServers
            "MACAddress"     = $_.MACAddress
            "IsDHCPEnabled"  = $_.false

    If($Network.DHCPEnabled) {
     $IsDHCPEnabled = $true
    }
        New-Object PSObject -Property @{
            "ComputerName"   = $_.ComputerName
            "IPAddress"      = $_.IPAddress
            "Subnet"         = $_.SubnetMask
            "DefaultGateway" = $_.DefaultGateway
            "DNSServers"     = $_.DNSServers
            "MACAddress"     = $_.MACAddress
            "IsDHCPEnabled"  = $_.True

     } Export-Csv c:\Scripts\DesktopIPVerified.csv -Append -Notypeinformation
    }
   }
  }
 }          
 end {}
}
CuriousMAUserAsked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
There are a lot of syntax and non-syntax elements mixed up.
Set-StrictMode -Version Latest
Set-ExecutionPolicy remotesigned -Force
import-module activedirectory

Get-ADComputer -Filter '*' -SearchBase "OU=Computers,DC=sample,DC=com" -Properties Name | Select -ExpandProperty Name |
  ? { Test-Connection -ComputerName $_ -Count 1 -TimeToLive 5 -Quiet } |
  % {
    $PC = $_
    Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $PC |
      ? {$_.IPAddress -like "192.168.*" -or $_.IPAddress -like "10.10.*"} |
      %  {
        New-Object PSObject -Property @{
          "ComputerName"   = $PC
          "IPAddress"      = $_.IPAddress[0]
          "Subnet"         = $_.IPSubnet[0]
          "DefaultGateway" = $_.DefaultIPGateway[0]
          "DNSServers"     = $_.DNSServerSearchOrder[0]
          "MACAddress"     = $_.MACAddress
          "IsDHCPEnabled"  = $_.DHCPEnabled
        }  
      } 
  } | Export-Csv c:\Scripts\DesktopIPVerified.csv -Append -Notypeinformation

Open in new window

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Besides a missing pipe in front of Export-CSV, what is the issue?
0
 
Will SzymkowskiSenior Solution ArchitectCommented:
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}

Rather than doing the above why not change it to the following...

$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPAddress -like "192.168.*" -or $_.Address -like "10.10.*" }

Open in new window


This would then only show Network Adapter info where an IPAddress is defined based on your filter options. Replace the IP's with valid ones on your internal network.

Will.
0
On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

 
CuriousMAUserAuthor Commented:
Thank you, Will
0
 
CuriousMAUserAuthor Commented:
Hi QLemo,

Thank you, I'd added the pipeline you referenced. The issue occurs at the beginning of the ForEach loop.

'Assigned expression is not valid'

foreach ($Network in $Networks) {
     $IsDHCPEnabled = $false    
            "ComputerName"   = $_.ComputerName
            "IPAddress"      = $_.IpAddress
            "SubnetMask"     = $_.IPSubnet
            "DefaultGateway" = $_.DefaultIPGateway
            "DNSServers"     = $_.DNSServers
            "MACAddress"     = $_.MACAddress
            "IsDHCPEnabled"  = $_.false
0
 
footechCommented:
Set-StrictMode -Version Latest
 Set-ExecutionPolicy remotesigned -Force
 import-module activedirectory

 $ComputerName = $env:ComputerName

 Get-ADComputer -Filter '*' -SearchBase "OU=Computers, DC=sample,DC=com" -Properites Name | % {
     $PC = $_Name 
 begin {}
 process {
  foreach ($Computer in $ComputerName) {
   if(Test-Connection -ComputerName $Computer -Count 1 -TimeToLive -Quiet) 
   {
    $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
   
    foreach ($Network in $Networks) {
      $IsDHCPEnabled = $false     
             "ComputerName"   = $_.ComputerName
             "IPAddress"      = $_.IpAddress
             "SubnetMask"     = $_.IPSubnet
             "DefaultGateway" = $_.DefaultIPGateway
             "DNSServers"     = $_.DNSServers
             "MACAddress"     = $_.MACAddress
             "IsDHCPEnabled"  = $_.false

     If($Network.DHCPEnabled) {
      $IsDHCPEnabled = $true
     }
         New-Object PSObject -Property @{
             "ComputerName"   = $_.ComputerName
             "IPAddress"      = $_.IPAddress
             "Subnet"         = $_.SubnetMask
             "DefaultGateway" = $_.DefaultGateway
             "DNSServers"     = $_.DNSServers
             "MACAddress"     = $_.MACAddress
             "IsDHCPEnabled"  = $_.True

      } Export-Csv c:\Scripts\DesktopIPVerified.csv -Append -Notypeinformation
     }
    }
   } 
  }           
  end {}
 }

Open in new window

I see a variety of syntax problems.  Putting the whole thing in a code block so I can reference line numbers.
Line 8 - Where is $PC being used?  On the right of the equals it should be $_.Name
The begin, process, and end blocks on lines 9, 10, and 43 don't make sense as you are already inside the process block of ForEach-Object.
Lines 18-24 are all invalid.  Maybe you meant to put them in hash table to define the properties of new object like on line 29?

A more appropriate start to the script at line 7 would be like
Get-ADComputer -Filter '*' -SearchBase "OU=Computers, DC=sample,DC=com" -Properites Name | % {
    $PC = $_.Name 
    if (Test-Connection -ComputerName $PC -Count 1 -TimeToLive -Quiet) 
    {
        $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $PC | ? {$_.IPEnabled}
        foreach ($Network in $Networks)

Open in new window

0
 
CuriousMAUserAuthor Commented:
Hi,

The script runs and the repetitive output occurs 708 times (once per listed OU desktop) but the output in column A reads, 'System.Collection.GenericList'1[System.Attribute]. I'd prefer the detailed IP address information. Completes in 15-20 seconds. Never seems to address each system separately. What am I missing?

*************************************
Set-StrictMode -Version Latest
Set-ExecutionPolicy remotesigned -Force
import-module activedirectory

$ComputerName = $env:ComputerName
Get-ADComputer -Filter '*' -SearchBase "OU=Computers,DC=sample,DC=com" -Properties Name | % {
 $PC = $_.Name
   {
    foreach ($Computer in $ComputerName) {
      if(Test-Connection -ComputerName $Computer -Count 1 -TimeToLive 5 -Quiet)
        {
        Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPAddress -like "192.168.*" -or $_.IPAddress -like "10.10.*"}
                foreach ($Network in $Networks) {
                $IsDHCPEnabled = $false    
                    If($Network.DHCPEnabled) {
                    $IsDHCPEnabled = $true
                        }
                        New-Object PSObject -Property @{
                        "ComputerName"   = $_.ComputerName
                        "IPAddress"      = $_.IPAddress
                        "Subnet"         = $_.SubnetMask
                        "DefaultGateway" = $_.DefaultGateway
                        "DNSServers"     = $_.DNSServers
                        "MACAddress"     = $_.MACAddress
                        "IsDHCPEnabled"  = $_.True

     }  
    }
   }
  }  
 } | Export-Csv c:\Scripts\DesktopIPVerified.csv -Append -Notypeinformation
}
0
 
CuriousMAUserAuthor Commented:
I'd like to also trap for errors so when the Test-Connection fails the logic will continue to the next computer to validate the next ip address details until all 708 desktop are exhausted ... nesting logic is where I'm having issues in addition to syntax. thank you.
0
 
CuriousMAUserAuthor Commented:
I plan to check into PowerShell Remoting and see if this an application
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Test-Connection will not fail with an error, so that condition is met already. I'll have to check the remaining code...
0
 
CuriousMAUserAuthor Commented:
Hello Mr. QLemo,

Tremendously helpful. Thank you. Time for a training class to reinforce proper coding techniques. You distill the code down to the simplest terms. Thank you so much.

Hello Mr. Will,

Thank you also for your suggestions. Very valuable.

Thankfully,
CusiousMAUser
0
 
Will SzymkowskiSenior Solution ArchitectCommented:
No Split?

Will.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.