vmware-archive / powernsx

PowerShell module that abstracts the VMware NSX-v API to a set of easily used PowerShell functions
173 stars 89 forks source link

Retrieve VM IPs attached to security groups #654

Closed Sk83r closed 3 years ago

Sk83r commented 3 years ago

Hi guys,

Wondering if there's any interesting way to retrieve all virtual machines IP that attached to security group.

EffectiveIPaddress switch retrieves all IPs from SG and I need just the ones related to VMs attached to it.

Thanks

dcoghlan commented 3 years ago

You can use Get-NsxSecurityGroupEffectiveIpAddress

or you can use Get-NsxSecurityGroupEffectiveMember which runs the following 4 cmdlets and returns all member types

Get-NsxSecurityGroupEffectiveVirtualMachine
Get-NsxSecurityGroupEffectiveIpAddress
Get-NsxSecurityGroupEffectiveMacAddress
Get-NsxSecurityGroupEffectiveVnic
Sk83r commented 3 years ago

Hi

Thanks for replying, here's an example

SG-TEST contains : | David-laptop , virtualMachine | Jay-laptop , virtualMachine | lab-VM , IPSet | SG-NESTED-TEST, securitygroup

Im interested in David-laptop and Jay-laptop IPs. nothing else Get-NsxSecurityGroupEffectiveIpAddress gives me all IP addresses, more than 10 IPs. dont want that. Get-NsxSecurityGroupEffectiveVirtualMachine gives me VM names but not the 2 IP addresses themselves.

Thanks

dcoghlan commented 3 years ago

You'll need to loop through the output of Get-NsxSecurityGroupEffectiveVirtualMachine and enumerate the IP addresses from the virtual machines. You can achieve this in multiple methods (either through vCenter or just with NSX Manager). Some example scripts which leverage various methods just using NSX Manager are below:

Sk83r commented 3 years ago

Thanks for this one @dcoghlan very nice