There was a requirement in one of the projects to export permissions assigned to each Virtual Machine hosted in a vSphere Environment. We wrote a PowerCLI script to fetch the list of users along with the Role assigned to each user and export these details to a CSV file.
Requirements:
- PowerCLI
- PowerShell 5.1 or later
Script:
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$cred = Get-Credential
Connect-VIServer vCenter_IP/FQDN -Credential $cred
$vms = Get-VM
$output = New-Object System.Collections.ArrayList($null)
$vmpermission = @()
foreach ($vm in $vms)
{
$permissions = $vm | Get-VIPermission | Get-Unique
Foreach ($permission in $permissions)
{
$formatpermission = $permission.Principal + " (" + $permission.Role + ")"
$permission | Add-Member -NotePropertyName FormatPermission -NotePropertyValue $formatpermission
}
$vmpermission = [pscustomobject]@{VMName=$vm.Name;Role=$permissions.FormatPermission -join ', '}
[void]($output.Add($vmpermission))
}
$output | Export-csv C:\Users\VMPermissionsExport.csv -NoTypeInformation
Just replace the vCenter_Server_IP_Address/FQDN and Path of CSV File and run the script to export VI Permissions assigned to each Virtual Machine in vSphere environment. BOOM!!
Happy Scripting!!