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_NetworkAdapterConfig uration -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\DesktopIPVerifi ed.csv -Append -Notypeinformation
}
}
}
}
end {}
}
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_NetworkAdapterConfig
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\DesktopIPVerifi
}
}
}
}
end {}
}
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.*" }
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.
ASKER
Thank you, Will
ASKER
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
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 {}
}
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)
ASKER
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.Generic List'1[Sys tem.Attrib ute]. 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_NetworkAdapterConfig uration -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\DesktopIPVerifi ed.csv -Append -Notypeinformation
}
The script runs and the repetitive output occurs 708 times (once per listed OU desktop) but the output in column A reads, 'System.Collection.Generic
**************************
Set-StrictMode -Version Latest
Set-ExecutionPolicy remotesigned -Force
import-module activedirectory
$ComputerName = $env:ComputerName
Get-ADComputer -Filter '*' -SearchBase "OU=Computers,DC=sample,DC
$PC = $_.Name
{
foreach ($Computer in $ComputerName) {
if(Test-Connection -ComputerName $Computer -Count 1 -TimeToLive 5 -Quiet)
{
Networks = Get-WmiObject Win32_NetworkAdapterConfig
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\DesktopIPVerifi
}
ASKER
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.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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.
Will.