vCenter Server and ESX/ESXi hosts determine the level of access for the user by reading the permissions that are assigned to the user. The combination of user name, password, and permissions authorizes the user to perform activities on vSphere server objects.
PowerCLI 4.0 U1 introduced a full set of cmdlets for managing vSphere permissions:
Name
|
Description
|
Retrieves the permissions defined on the specified inventory objects
|
|
Creates new permissions on the specified inventory objects for the provided users and groups in the role.
|
|
Modifies the properties of the specified permissions.
|
|
Removes the specified permissions.
|
|
Retrieve the privilege groups and items for the provided servers.
|
|
Retrieves all roles defined on the provided servers.
|
|
Creates a new role on the specified servers and applies the provided privileges.
|
|
Modifies the privileges of the provided roles.
|
|
Removes the specified roles.
|
By using these cmdlets, you can fully automate the setup of vSphere permissions.
The example below shows a sample scenario how to create a custom role and set permissions to a user.
First, you can get the privileges of the read-only role. A role is a predefined set of privileges. Privileges define basic individual rights required to perform actions and read properties.
1 |
<span style="background-color: #e6e6e6;">$readOnlyPrivileges = Get-VIPrivilege -Role readonly </span> |
To create a new role with custom privileges, use New-VIRole.
1 |
<span style="background-color: #e6e6e6;">$myRole = New-VIRole -Privilege $readOnlyPrivileges -Name MyRole </span> |
You can check the list of roles on the server including newly created role:
1 |
<span style="background-color: #e6e6e6;">Get-VIRole</span> |
If you want to add more privileges to the newly created role just use the Set-VIRole cmdlet:
1 |
<span style="background-color: #e6e6e6;">$powerOnPrivileges = Get-VIPrivilege -Name "Power On" </span><br /><span style="background-color: #e6e6e6;">$myRole = Set-VIRole –Role $myRole –AddPrivilege $powerOnPrivileges</span> |
The privileges on the updated role can be examined by using Get-VIPrivilege with the Role parameter:
1 |
<span style="background-color: #e6e6e6;">Get-VIPrivilege -Role $myRole</span> |
We already have a custom created role, so we can grant a permission to a user. We’ll apply permissions to a vSphere root object and propagate them across the hierarchy:
1 |
<span style="background-color: #e6e6e6;">$rootFolder = Get-Folder -NoRecursion</span><br /><span style="background-color: #e6e6e6;">$myPermission = New-VIPermission -Entity $rootFolder -Principal "myuser" -Role readonly -Propagate:$true</span> |
Note that the Principal parameter supports local users and groups as well as domain users/groups if the vSphere server is joined in AD.
As you noticed, we’ve granted a read-only permission, so we need to update Role of the newly created permission with our custom role:
1 |
<span style="color: #111111; background-color: #e6e6e6;">$myPermission = Set-VIPermission -Permission $myPermission -Role $myRoleRemove permission</span> |
These are the simple steps how to create a new role and grant permissions to a user.
If you want to remove permission, you can just use the Remove-VIPermission cmdlet
1 |
<span style="background-color: #e6e6e6;">Remove-VIPermission $myPermission</span> |
and Remove-VIRole to remove your custom role:
1 |
<span style="background-color: #e6e6e6;">Remove-VIRole MyRole</span> |