简体   繁体   中英

How can I get the machine IP address only for several PCs using ForEach-Object in Powershell?

I am working on a script to get the IP address information from several computers using the ForEach statement. Problem is that the IP address string returns extra info, so in order to get rid of that info, I run this...

Get-WmiObject win32_networkadapterconfiguration | 
    where { $_.ipaddress -like "1*" } | 
    select -ExpandProperty ipaddress | 
    select -First 1}

Which is fine for one computer but when I add it to the whole ForEach part of my script below, it returns the IP Address for my machine attached to all machine queried in the script.

Get-Content $MultiActivePing | foreach {
    Get-WmiObject win32_networkadapterconfiguration -ComputerName $_ |
    where { $_.ipaddress -ne $null} | 
    select @{Expression={$_.__SErver};Label="Computer"}
      ,@{Expression={Get-WmiObject win32_networkadapterconfiguration | where { $_.ipaddress -like "1*" } | select -ExpandProperty ipaddress | select -First 1};Label="IPAddress"} `
      ,@{Expression={$_.DefaultIPGateway};Label="Gateway"} 
                                        }

So I was wondering if there was a way to expand the property of the IP address without having to run another GWMI inside the SELECT IP address portion.

Admitting I am a little confused since I don't know what you are expecting exactly however I can venture a guess. Mostly I'm not sure what you want the IP Address output to look like.

Get-Content $MultiActivePing | ForEach-Object {
    # Save the current computer name so we can use down the line. 
    $computer = $_
    Get-WmiObject win32_networkadapterconfiguration -ComputerName $_ |
    where { $_.ipaddress -ne $null} |
    Select @{l="IPAddress";e={$_.ipaddress | Select -First 1}},@{l="Computer";e={$computer}},DHcpenabled,macaddress
}

The part about this that will vary depending on what you are trying to accomplish:

Select @{l="IPAddress";e={$_.ipaddress | Select -First 1}}

You already know the IP so there is no need to acquire it again. This takes the first IP in the array and outputs it alone. This is the part I think you are having issues with but I don't know how you choose what information to drop. The caveat obviously is that you could lose IP information that you actually want depending on the order it appears. I would suggest this then as well so nothing is lost:

Change the calculated property for IPAddress highlighted above to this:

@{l="IPAddresses";e={$_.ipaddress -Join ";"}}

That would take all IP's and convert them into a semicolon delimited string

So thanks to Matt's logic, I just added this to the script...

,@{E={$_.ipaddress | Select -First 1};L="IPAddress"}

Here's what it looks like now, something so simple. Took care of my issue...

ForEach {
Get-WmiObject win32_networkadapterconfiguration -ComputerName $_ |
where { $_.ipaddress -ne $null} |
Select @{E={$_.__SErver};L="Computer"}
      ,@{E={$_.ipaddress | Select -First 1};L="IPAddress"}
      ,@{E={$_.DefaultIPGateway | Select -First 1};L="Gateway"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM