Link to home
Start Free TrialLog in
Avatar of CuriousMAUser
CuriousMAUser

asked on

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 {}
}
Avatar of Qlemo
Qlemo
Flag of Germany image

Besides a missing pipe in front of Export-CSV, what is the issue?
$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.
Avatar of CuriousMAUser
CuriousMAUser

ASKER

Thank you, Will
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
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

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
}
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.
I plan to check into PowerShell Remoting and see if this an application
Test-Connection will not fail with an error, so that condition is met already. I'll have to check the remaining code...
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
No Split?

Will.