简体   繁体   中英

How can I get IPV4 address into powershell variable with IPV6 and IPV4 together?

I have a simple powershell script to capture IP Addresses from SCCM server by the MAC addresses list, like that:

$ServerName = Get-Content "C:\suport\macs.txt"  
  
 foreach ($Server in $ServerName) {  

 $maq = (Get-WmiObject -Namespace "root/SMS/Site_G01" -ComputerName SCCMSERVERr -class SMS_R_System -filter "MACAddresses like '$Server'").IPAddresses
 
 Write-Output $maq

 }

But, the output is IPV4 + IPV6 addresses...:

...
172.10.20.155
fe76::4112:5ecd:bfe2:10ff
172.10.15.158
fe76::e098:d709:5cce:d09c
...

I need only the IPV4 output, like that:

...
172.10.20.155
172.10.15.158
...

Can you help me? Thanks a lot: :)

You could simply filter out the IPv6 by the colon.

$ServerName = Get-Content "C:\suport\macs.txt"  

 foreach ($Server in $ServerName) {  

     $maq = (Get-WmiObject -Namespace "root/SMS/Site_G01" -ComputerName SCCMSERVERr -class SMS_R_System -filter "MACAddresses like '$Server'").IPAddresses | where {$_ -notmatch ':'}

     Write-Output $maq

 }

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