We help IT Professionals succeed at work.

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

CuriousMAUser
CuriousMAUser asked
on
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 {}
}
Comment
Watch Question

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Besides a missing pipe in front of Export-CSV, what is the issue?
Will SzymkowskiSenior Solution Architect
Most Valuable Expert 2015
Top Expert 2015

Commented:
$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.

Author

Commented:
Thank you, Will

Author

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
Top Expert 2014

Commented:
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

Author

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
}

Author

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.

Author

Commented:
I plan to check into PowerShell Remoting and see if this an application
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Test-Connection will not fail with an error, so that condition is met already. I'll have to check the remaining code...
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
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

Author

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
Will SzymkowskiSenior Solution Architect
Most Valuable Expert 2015
Top Expert 2015

Commented:
No Split?

Will.