Script to shutdown Virtual Machine for users

Anga Dcruz 40 Reputation points
2023-09-14T10:32:15.95+00:00

Is there any script that help to shutdown the VM in azure
for users

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,813 questions
Azure Virtual Desktop
Azure Virtual Desktop
A Microsoft desktop and app virtualization service that runs on Azure. Previously known as Windows Virtual Desktop.
1,523 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,472 questions
0 comments No comments
{count} votes

Accepted answer
  1. dashanan13 930 Reputation points
    2023-09-18T08:54:17.7233333+00:00

    Hei Anga Dcruz,

    Thank you for contacting Microsoft community.

    A user will need "VM Contributor role" or a custom role with privileges to power off\on, if they need to shutdown a VM via a portal or powershell or azure command line.

    It is also possible to schedule a shutdown through "auto shutdown" feature, available in the left menu for vm under "Operations" > "Auto shutdown".

    Connect-AzAccount   #Authenticate to Azure 
    Set-AzContext -Subscription <subscription name>   # set the correct subscription
    Stop-AzVM -ResourceGroupName <Resource group> -Name <Virtual machnie name>
    

    Read more about build in VM roles: https://learn.microsoft.com/en-us/azure/role-based-access-control/built-in-roles

    Read about setting custom roles: https://learn.microsoft.com/en-us/azure/role-based-access-control/custom-roles

    Read about assigning roles to an account: https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-portal

    Read about auto shutdown: https://learn.microsoft.com/en-us/azure/automation/automation-solution-vm-management

    Please mark this as "answer" if it helped.

    acceptasAnswer

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Johan Vanneuville 86 Reputation points MVP
    2023-09-14T11:14:22.3966667+00:00

    Do you need it for normal virtual machines or for AVD session hosts?

    For normal vm's you can use the auto shutdown feature. https://learn.microsoft.com/en-us/azure/devtest-labs/devtest-lab-auto-shutdown

    Otherwise you can use scaling plans for AVD: https://learn.microsoft.com/en-us/azure/virtual-desktop/autoscale-scaling-plan?tabs=pooled-autoscale

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.