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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", 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
CuriousMAUserAuthor Commented:
Thank you, Will
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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
Qlemo"Batchelor", 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
Qlemo"Batchelor", 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.