| author | habibaum |
|---|---|
| ms.author | v-uhabiba |
| ms.topic | include |
| ms.date | 09/24/2024 |
-
Ensure that you're using a PowerShell 7.4 runbook.
-
Assign permissions to the appropriate managed identity. The runbook can use the Automation account's system-assigned managed identity or a user-assigned managed identity.
The following script examples (starting and stopping VMs) require the Virtual Machine Contributor role or a custom role with these specific permissions:
- Microsoft.Compute/virtualMachines/start/action
- Microsoft.Compute/virtualMachines/deallocate/action
- Microsoft.Compute/virtualMachines/restart/action
- Microsoft.Compute/virtualMachines/powerOff/action
You can use either the Azure portal or Azure PowerShell cmdlets to assign permissions to each identity:
To assign permissions, follow the steps in Assign Azure roles using the Azure portal.
To assign a role to the system-assigned managed identity, use the New-AzRoleAssignment cmdlet:
New-AzRoleAssignment ` -ObjectId $SA_PrincipalId ` -ResourceGroupName $resourceGroup ` -RoleDefinitionName "Contributor"
Assign a role to a user-assigned managed identity:
New-AzRoleAssignment ` -ObjectId $UAMI.PrincipalId ` -ResourceGroupName $resourceGroup ` -RoleDefinitionName "Contributor"
For the system-assigned managed identity, show
ClientIdand record the value for later use:$UAMI.ClientId
-
Import the
Az.ResourceGraphmodule. Ensure that the module is updated to ThreadJob with the module version 2.0.3.