We help IT Professionals succeed at work.
Get Started

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

632 Views
Last Modified: 2015-03-03
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
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 12 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE