How to Fix Azure Start and Stop VM using Runbook in Automation Account?

Guzzu, Navya X 25 Reputation points
2024-02-07T18:25:56.84+00:00

Hi All, I have a requirement to auto startup and shutdown the Azure VM in Business hours(i.e 9 AM- 5 PM ) Mon-Fri. Have created a Automation Accounts, Configured Runbook and created a schedule. I'm unable to link a runbook to schedule as it got depreciated in sep2023. On Attempt-1, Throwing error as this.client.subscriptionId is null. However, have tried to hardcoded the subscription Id, still same error. In Below snippet, "Get Subscription" also throws the same error. ###################Attempt-1:##########################

$resourceGroupName = "XXX" $vmName = "YYY" 
try 
{
 "Logging in to Azure..." 
Connect-AzAccount -Identity 
} 
catch 
{ 
Write-Error -Message $.Exception throw $.Exception 
}
#Get subscription details
$subscriptions = Get-AzSubscription
#Output subscription details
$subscriptions | Select-Object SubscriptionName, SubscriptionId 
Write-Output ("Subscription"+$subscriptions) 
Stop-AzVM -ResourceGroupName "XXX" -Name $vmName -Force

Result: It gives error as Subscription is null.

---On Attempt-2, We have tried to authenticate the certificate by providing the tenant id, app_id unable to provide the thumbprint as I have no permissions to fetch. Still it throws the same error "Subscription is null." Used the authentication by Managed identity approach. #######################Attempt-2:#####################################

$resourceGroupName = "XXX" 
$vmName = "YYY" 

$Cert = Get-AzAutomationCertificate -ResourceGroupName "XXX" -AutomationAccountName "VM-AutoStartandStop" -Name "AzureRunAsCertificate" 

Write-Output ("Cert"+$Cert) 
try 
{
 "Logging in to Azure..." 
Connect-AzAccount -Identity 
}
 catch 
{
 Write-Error -Message $.Exception throw $.Exception 
}
Connect-AzAccount -ServicePrincipal -Tenant "
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,366 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jackson Martins 10,606 Reputation points MVP Volunteer Moderator
    2024-02-09T18:11:49.53+00:00

    There is a simpler script with the command just to startup the vm, you can use the script below:

    Param(
     [string]$VmName,
     [string]$ResourceGroupName,
     [ValidateSet("Startup", "Shutdown")]
     [string]$VmAction
    )
    $Conn = Get-AutomationConnection -Name AzureRunAsConnection
    Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID `
    -ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
    Start-AzureRmVM -Name VMNAME -ResourceGroupName RGNAME
    

    Put it in the schedule to turn on the VM And to shut down the vm you use the auto shutdown feature on Virtual Machine User's image

    Get in touch if you need more help with this issue. --please don't forget to "[Accept the answer]" if the reply is helpful--

    2 people found this answer helpful.

5 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

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.