Powershell script to auto shutdown virtual machines.

Mitchell Darry 20 Reputation points
2024-02-22T05:56:07.77+00:00

I want to shutdown the virtual machines in azure for users through a powershell script. Can someone provide please.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,144 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,120 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,069 questions
0 comments No comments
{count} votes

Accepted answer
  1. v-vvellanki-MSFT 3,735 Reputation points Microsoft Vendor
    2024-02-22T06:04:17.7666667+00:00

    Hi @Mitchell Darry,
    Thanks for contacting Microsoft Q&A platform.
    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

    Hope this helps you.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful