Automation

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

    Posted 27 days ago

    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 26 days ago
    Edited by LucD 26 days ago

    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 26 days ago

    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 26 days ago
    Edited by ganapa2000 25 days ago

    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 25 days ago

    Thank you very much LucD, that worked perfectly.