Skip to content

Instantly share code, notes, and snippets.

@athiththan11
Last active June 10, 2023 05:50
Show Gist options
  • Save athiththan11/eff6d2da7da74c1b8f08614580c238db to your computer and use it in GitHub Desktop.
Save athiththan11/eff6d2da7da74c1b8f08614580c238db to your computer and use it in GitHub Desktop.
Environment Start Stop Automation Runbooks | Powershell
# Following automation variables should be created under respective automation account
$AksClusterName = Get-AutomationVariable -Name 'av-aks-cluster-name'
$AksClusterResourceGroup = Get-AutomationVariable -Name 'av-aks-cluster-resource-group'
function Login-ToAzure {
Disable-AzContextAutosave -Scope Process | Out-Null
try {
$AzureContext = (Connect-AzAccount -Identity).Context
} catch {
Write-Error "There is no system-assigned user identity. Aborting!"
exit
}
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription.Name -DefaultProfile $AzureContext
}
function Start-Aks {
Start-Sleep -s 30
Write-Output "Starting AKS: $AksClusterName"
Start-AzAksCluster -Name $AksClusterName -ResourceGroupName $AksClusterResourceGroup
}
function Get-AksStatus {
$RetryCount = 0
do{
# Get AKS status.
$AksGetResult = Get-AzAksCluster -Name $AksClusterName -ResourceGroupName $AksClusterResourceGroup
$AksStatus = $AksGetResult.ProvisioningState
Write-Output "Checking AKS status..."
Write-Output "AKS Status: $AksStatus"
# Sleep 5 seconds before the next status check.
Write-Output "Sleep 5 seconds before the next check."
Write-Output ""
Start-Sleep -s 5
# Limit the waiting time. It will be 2 minutes after the 'Stop-AzAksCluster' command responded.
$RetryCount ++
} until(($AksStatus.ToString() -eq "Succeeded") -or ($RetryCount -gt 24))
# Output the AKS cluster status.
Write-Output "AKS Status: $AksStatus"
if($RetryCount -gt 24)
{
Write-Output "There is an issue at stopping the AKS cluster. Hence, stopping the process!"
Exit
}
}
Login-ToAzure
Start-Aks
Get-AksStatus
# Following automation variables should be created under respective automation account
$AksClusterName = Get-AutomationVariable -Name 'av-aks-cluster-name'
$AksClusterResourceGroup = Get-AutomationVariable -Name 'av-aks-cluster-resource-group'
function Login-ToAzure {
Disable-AzContextAutosave -Scope Process | Out-Null
try {
$AzureContext = (Connect-AzAccount -Identity).Context
} catch {
Write-Error "There is no system-assigned user identity. Aborting!"
exit
}
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription.Name -DefaultProfile $AzureContext
}
function Stop-Aks {
Write-Output "Stopping AKS: $AksClusterName"
Stop-AzAksCluster -Name $AksClusterName -ResourceGroupName $AksClusterResourceGroup
}
function Get-AksStatus {
$RetryCount = 0
do{
# Get AKS status.
$AksGetResult = Get-AzAksCluster -Name $AksClusterName -ResourceGroupName $AksClusterResourceGroup
$AksStatus = $AksGetResult.ProvisioningState
Write-Output "Checking AKS status..."
Write-Output "AKS Status: $AksStatus"
# Sleep 5 seconds before the next status check.
Write-Output "Sleep 5 seconds before the next check."
Write-Output ""
Start-Sleep -s 5
# Limit the waiting time. It will be 2 minutes after the 'Stop-AzAksCluster' command responded.
$RetryCount ++
} until(($AksStatus.ToString() -eq "Succeeded") -or ($RetryCount -gt 24))
# Output the AKS cluster status.
Write-Output "AKS Status: $AksStatus"
if($RetryCount -gt 24)
{
Write-Output "There is an issue at stopping the AKS cluster. Hence, stopping the process!"
Exit
}
}
Login-ToAzure
Stop-Aks
Get-AksStatus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment