1

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 are just going to have to parse the individual strings in the `SMS_R_System.IPAddresses` array and print out only the strings that look like IPv4 addresses. Or, maybe you can just ignore any strings in the `SMS_R_System.IPAddresses` array that are also in the `SMS_R_System.IPv6Addresses` array. – Remy Lebeau Aug 19 '20 at 23:19
  • three ways come to mind ... [1] test for `:` and exclude those addresses. [2] use the `[ipaddress]` type accelerator to convert the strings to ip address objects & then test the `.AddressFamily` property. [3] if the items are being returned as `ipaddress` objects instead of strings, you can also test for the address family. – Lee_Dailey Aug 19 '20 at 23:50
  • The problem is: when I convert one MAC address to IP, each MAC report 1 IPv4 address and 1 IPv6 addres... Ex: $server = AA:BB:CC:DD:EE:FF $maq = 172.10.20.155 fe76::4112:5ecd:bfe2:10ff I need capute only IPV4 addresses to execute some remote commands... – Raphael Cunha Aug 20 '20 at 00:48
  • 1
    As an aside: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell [Core] (version 6 and above), where all future effort will go, doesn't even _have_ them anymore. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 Aug 20 '20 at 01:48

1 Answers1

1

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

 }
Doug Maurer
  • 8,090
  • 3
  • 12
  • 13