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 "