Find computers in domain with static IPs - PowerShell

Solution 1:

What am I doing wrong?

The backtick ` is the line continuation character, and the first error you get 'missing expression after unary operator' appears to be PowerShell not seeing the -Query line as part of the previous line.

Possibly you are missing the ` at the end of the previous line in your copy of the script, or have got it replaced with an apostrophe by some quirk of copy/paste/font/editor code mangling, or something.

or what should I add/remove from the script to make it run?

Remove most of it, it's convoluted and ugly and redundant for what you want.

Why is it querying for Win32_NetworkAdapterConfiguration things and calling them ints? That's so misleading.

Why is it taking the IPAddress, calling it addr, then calling it ip, then calling it IP?

Why is it taking the GatewayAddress, calling it gw_addr, then gw, then Gateway?

The test for -eq $null seems needless, if you try to use foreach on an empty sequence it will work as expected (skip over it).

Using regexes to pick up IPv4 addresses instead of IPv6 addresses - useful, but waffly. IPv6 addresses use : to delimit them, and IPv4 addresses use ., so if you care for only IPv4 addresses, just look for a . in the address.

Why copy the address out, build a list of results, then select everything out of the list, when you could just print the address as you find it?

Do you even care at all about the gateway address?

Why test if the IP address is empty when you are in a block that's processing the IP address at this point so it can't be null at this point?

Why is the whole thing a chain using get-computers % { code } | select results, when that's no help to you?

I've pruned it back a bit and renamed things and got to the following clearer, simpler structure:

  1. Setup.
  2. Get the computers
  3. For each computer, get the network interfaces with static addresses.
  4. For each interface, get the IP addresses.
  5. For each IP address, identify the IPv4 ones by looking for .
  6. Display the computer name and this IPv4 address.

    param ( 
    [string]$LDAPFilter = '(name=*)'
    )
    
    $wmiQuery = "select IPAddress, DefaultIPGateway from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE and DHCPEnabled=FALSE"
    
    $computers = (Get-ADComputer -LDAPFilter $LDAPFilter)
    foreach ($computer in $computers) { 
    
        $networkAdapters = (Get-WmiObject -ErrorAction SilentlyContinue -ComputerName $computer.DNSHostName -Query $wmiQuery) 
        foreach ($networkAdapter in $networkAdapters) { 
            foreach ($ip in $networkAdapter.IPAddress) 
            { 
                if ($ip -match "\.") 
                { 
                    Write-Host $($computer.DNSHostName), $ip } 
                }
        } 
    }
    

I haven't tested this completely, but it works against one computer to show just the name and static IPs.

N.B. it won't show you any names of computers that it can't contact, but then nor would the original one.