Nathan Horn
asked on
VMware PowerCLI, PowerNSX Determine External IP of VM
I'm looking for a way to determine the external IP address of a VMware VM using PowerCLI or PowerNSX. I'm currently using PowerCLI to query a vmware environment and record VM details to a database. External IP's in my environment are configured in NSX via NAT. I had originally thought I could use Invoke-WebRequest and Invoke-VMScript however it that would require authenticating against the guest OS which I cannot do.
How can I query NSX to determine an external IP?
Expert of Script:
$VMs = Get-VM | Select *
$VMGuestInfo = Get-VM | Get-VMGuest | Select *
# Inserting each row
ForEach ($VM in $VMs)
{
$VMID = $VM.ID
$PersistentID = $VM.PersistentID
$Name = $VM.Name
$PowerState = $VM.PowerState
$Notes = $VM.Notes
$Guest = $VM.Guest
$NumCpu = $VM.NumCpu -as [int]
$CoresPerSocket = $VM.CoresPerSocket -as [int]
$MemoryGB = $VM.MemoryGB -as [int]
$VMHostID = $VM.VMHostID
$VMHost = $VM.VMHost
$VApp = $VM.VApp
$FolderID = $VM.FolderID
$FolderPath = Get-Folder -ID $VM.FolderID | Get-FolderPath
$ExternalIP = ???
$Hostname = $VMGuest.HostName
How can I query NSX to determine an external IP?
Expert of Script:
$VMs = Get-VM | Select *
$VMGuestInfo = Get-VM | Get-VMGuest | Select *
# Inserting each row
ForEach ($VM in $VMs)
{
$VMID = $VM.ID
$PersistentID = $VM.PersistentID
$Name = $VM.Name
$PowerState = $VM.PowerState
$Notes = $VM.Notes
$Guest = $VM.Guest
$NumCpu = $VM.NumCpu -as [int]
$CoresPerSocket = $VM.CoresPerSocket -as [int]
$MemoryGB = $VM.MemoryGB -as [int]
$VMHostID = $VM.VMHostID
$VMHost = $VM.VMHost
$VApp = $VM.VApp
$FolderID = $VM.FolderID
$FolderPath = Get-Folder -ID $VM.FolderID | Get-FolderPath
$ExternalIP = ???
$Hostname = $VMGuest.HostName
ASKER
That solution was great, however, while implementing it I learned it will only work if you can authenticate with the guest OS. I cannot authenticate the guest OS so I need to find a solution that does not require authenticating. Pulling the external IP from NSX seems like the next possible route.
(Invoke-VMScript -VM $VM -ScriptText 'Invoke-RestMethod http://ipinfo.io/json | Select -exp ip' -ScriptType Powershell -ErrorAction SilentlyContinue | Select -ExpandProperty ScriptOutput)
(Invoke-VMScript -VM $VM -ScriptText 'Invoke-RestMethod http://ipinfo.io/json | Select -exp ip' -ScriptType Powershell -ErrorAction SilentlyContinue | Select -ExpandProperty ScriptOutput)
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
https://www.experts-exchange.com/questions/29120803/External-VM-IP-using-VMware-PowerCLI.html