title: vCenter VM permission query using Powershell / PowerCLI
body: <p>I've been trying to use Powershell with imported PowerCLI commands for VMware administration and I've hit a snag.  What I'm trying to do is query all VM's in a location (doesn't matter where), and for every VM I want the group with "Virtual Machine User with Snapshot" permission, and then use that group name to run a Get-ADGroupMembers query for everyone in that group.  I also have to remove the domain prefix from the AD query, which would otherwise cause an error.</p>
  
  <p>After some more playing around with outputting hash table information into the csv, as opposed to 'SystemObject[]', I finally got the script so it doesn't return errors EXCEPT on VM's where there is more than one group.  It throws an error but the script continues, and just outputs the members of the first group.</p>
  
  <p>How do I get it to do a recursive AD query for every group that is pulled into the owner groups hashtable?  The output would be the same as for all the other VM's, just with a line for each group and members.</p>
  
  <pre><code>$AllVMs = @()
  $vms = get-vm  * -Location datacenter
  foreach ($vm in $vms)
      {
          $owners = Get-VIPermission $vm.name | where-object {$_.role -eq "virtual machine user with snapshot"}   
          foreach ($owner in $owners) 
              {
              $members = Get-ADGroupMember ($owners.principal -replace '^prefix\\')
  
          $temp = New-Object psobject |
          Add-Member Noteproperty "Name" -value $vm.name -PassThru |
          Add-Member Noteproperty "Owner" -value (@($owners.principal) -join ',') -PassThru |
          Add-Member Noteproperty "Members" -value  (@($members.SamAccountName) -join ',') -passthru
          $AllVMs+=$temp
      }
  $AllVMs | Export-Csv -Path c:\users\me\desktop\AllVMs.csv
  </code></pre>