Home > Software engineering >  If/then statement for Powershell to check Azure Automation Runbook Schedule
If/then statement for Powershell to check Azure Automation Runbook Schedule

Time:11-29

I am stumped on what exactly I am missing here. I am trying to author a script to check if a schedule is enabled or not enabled in an Azure Automation Account, and then take an action with an if/then statement.

I have tried a couple different iterations but essentially I am not able to pull the results of my "Get" cmdlet and pipe them into my Set cmdlet. If the "IsEnabled" = True I want to enable the schedule, if = false I want to write output that it is already enabled.

https://learn.microsoft.com/en-us/powershell/module/az.automation/get-azautomationschedule?view=azps-9.1.0 https://learn.microsoft.com/en-us/powershell/module/az.automation/set-azautomationschedule?view=azps-9.1.0

#Check and Enable Start Jobs
Get-AzAutomationSchedule -AutomationAccountName "XXXXXX" -Name "XXXXX" -ResourceGroupName "XXXXX"

Write-Output "IsEnabled" | Set-AzAutomationSchedule

if ($IsEnabled -eq "False") {

Set-AzAutomationSchedule -AutomationAccountName "XXXXX" -Name "XXXX" -IsEnabled "True" -ResourceGroupName "XXXX"
}
elseif ($IsEnabled -eq "True") {
    Write-Host "Scheduled jobs are already enabled" -ForegroundColor Green
}
#Check and Enable Stop Jobs
Get-AzAutomationSchedule -AutomationAccountName "XXXXX" -Name "Scheduled-StopVM" -ResourceGroupName "XXXXXX"

if ($IsEnabled -eq "False") {

Write-Output "IsEnabled" | Set-AzAutomationSchedule 

Set-AzAutomationSchedule -AutomationAccountName "XXXX" -Name "XXXXX" -IsEnabled "True" -ResourceGroupName "XXXXX"
}
elseif ($IsEnabled -eq "True") {
    Write-Host "Scheduled jobs are already enabled" -ForegroundColor Green

CodePudding user response:

I've tried to check if a schedule is enabled or not by connecting to Azureautomationaccount from PowerShell and it worked for me as shown below:

Script:

$infoo = Get-AzAutomationSchedule -AutomationAccountName "<AutomationAccountName>" -Name "new" -ResourceGroupName "<ResourceGroupName>"
if ($infoo.IsEnabled -eq $true){
Write-Host "Scheduled jobs are already enabled" -ForegroundColor Green 
}
if ($infoo.IsEnabled -eq $False) {
Set-AzAutomationSchedule -AutomationAccountName "<AutomationAccountName>" -Name "new" -IsEnabled $True -ResourceGroupName "<ResourceGroupName>"
}

If the schedule is enabled, I got the following output:

enter image description here

If it is not enabled, then use set-azautomationschedule -IsEnabled $true enables the schedule.

enter image description here

Previously, a schedule's status in the portal was disabled:

enter image description here

Status is set to "On" Azure Automation Account in portal after enabling it.

enter image description here

  • Related