Automation

 View Only
  • 1.  How to get filter and get particular VM IP

    Posted May 21, 2024 02:19 AM

    Hi,

    I have multiple VMs which has multiple IP address with different subnets. How can I query and get particular IP subnet of the particular VM

    for example, disktest has two NICs where one NIC is having 172.27.18.x subnet and other one is having 172.27.1.x

    now, I want to get only 172.27.1.x IP from a particular VM

    I tried as below, it is not retrieving the particular subnet IP

    Get-VM disktest | Where-Object {$_.Guest.IPAddress -match "172.27.6." -or $_.Guest.IPAddress -match "172.27.1.*"} | Select Name, @{N="IP_Address";E={@($_.Guest.IPAddress)}}| Select -Expandproperty "IP_Address" 

    Please help!!



  • 2.  RE: How to get filter and get particular VM IP

    Posted May 21, 2024 01:05 PM
    Edited by LucD May 21, 2024 01:05 PM

    The -match operator expects a RegEx.
    Try with this RegEx

    Get-VM disktest |

    Where-Object {$_.Guest.IPAddress -match "^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.1\."} |

    Select Name, @{N="IP_Address";E={@($_.Guest.IPAddress)}}|

    Select -Expandproperty "IP_Address"



    ------------------------------


    Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference


    ------------------------------



  • 3.  RE: How to get filter and get particular VM IP

    Posted May 22, 2024 01:10 AM

    LucD, I am getting all 4 IP address from the above, but I want only IP address that starts with 172.27.1 (172.27.1.19)




  • 4.  RE: How to get filter and get particular VM IP
    Best Answer

    Posted May 22, 2024 03:43 AM
    Edited by ganapa2000 May 22, 2024 05:24 AM

    Ok, then try like this

    Get-VM  |
    Where-Object { $_.Guest.IPAddress -match "^172\.27\.1\." } |
    Select-Object Name,
        @{N = "IP_Address"; E = { $_.Guest.IPAddress.where{$_ -match "^172\.27\.1\."}}}

    ------------------------------


    Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference


    ------------------------------



  • 5.  RE: How to get filter and get particular VM IP

    Posted May 22, 2024 05:25 AM

    Thank you very much LucD, that worked perfectly.