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

SecurityTag assignments #339

Closed haribcs closed 7 years ago

haribcs commented 7 years ago

Hi Team,

We are using nsx 6.3 and 6.2, get-nsxsecuritytagAssignment is not showing the security Tag name.

pandom commented 7 years ago

This is as intended, per the API's return.

Here is how to work with it and retrieve name.

TLDR; traverse $tag.securitytag.name property.

Example:

PS /> $tag = get-vm win-01 | Get-NsxSecurityTagAssignment
PS /> $tag[0]

SecurityTag VirtualMachine
----------- --------------
securityTag win-01

PS /Users/aburke> $tag[0].SecurityTag

objectId           : securitytag-12
objectTypeName     : SecurityTag
vsmUuid            : 4201B045-B1F9-457F-E621-B54038A6AFA5
nodeId             : 4b749a6a-bc41-431b-bf24-cf9e54dcb452
revision           : 4
type               : type
name               : ST-DFW-Exclusion
clientHandle       :
extendedAttributes :
isUniversal        : false
universalRevision  : 0
systemResource     : false
vmCount            : 4

PS /Users/aburke> $tag.securitytag.name
ST-DFW-Exclusion
ST-Windows

Happy for others to authors to comment here.

haribcs commented 7 years ago

Hi friend,

I need to collect all the security tag with virtual machines attached to it. any way to approach it?

Connect-NsxServer $test -Username admin -Password xxxxx -VICred $cred foreach ($tag in (Get-NsxSecurityTag |where {$.vmcount -ne "0"} | foreach{$.name})){ $data = Get-NsxSecurityTag -Name $tag | Get-NsxSecurityTagAssignment | select Virtualmachine $data | Add-Member -Name "Security Tag" -Value $tag -MemberType NoteProperty $data | Add-Member -Name "Server" -Value $test -MemberType NoteProperty $report += $data }

IM trying this but its taking hours to produce the putput

pandom commented 7 years ago

It seems you're looping twice.

$data = Get-NsxSecurityTag -Name $tag | Get-NsxSecurityTagAssignment | select Virtualmachine

could be

$data = $tag | Get-NsxSecurityTagAssignment | select Virtualmachine

This reduces an API call by not retrieving all Security Tags again.