Search code examples
azurepowershellautomationschedule

Powershell: Get AzureRM automation schedule recurrence info


I'm doing some inventory trying to gather all my start/stop VM schedules from Azure. I'm strugling with extracting the days selected for weekly recurrence schedules. I can extract all the data from single schedules with:

Select-AzureRmSubscription <name>
$schedule = Get-AzureRmAutomationSchedule -AutomationAccountName <name)-ResourceGroupName <name> -Name <name>

And then get all the days:

$schedule.WeeklyScheduleOptions.DaysOfWeek -join ","

Which outputs: Monday,Tuesday,Wednesday,Thursday,Friday

But if I loop through all my subscriptions and build a psobject with all schedule data this data comes up empty:

$AzSubs = Get-AzureRmSubscription

$objs = @()
foreach ($AzSub in $AzSubs){

    Get-AzureRmSubscription -SubscriptionName $AzSub.Name | Select-AzureRmSubscription
    $azAutAccs = Get-AzureRmAutomationAccount 
    foreach ($azAutAcc in $azAutAccs){
        $AzAutScheds = Get-AzureRmAutomationSchedule -AutomationAccountName $azAutAcc.AutomationAccountName -ResourceGroupName $azAutAcc.ResourceGroupName
        $AzAutScheds = $AzAutScheds | where{$_.IsEnabled -eq "True"}
        foreach ($AzAutSched in $AzAutScheds){

        $DOW = $azAutSched.WeeklyScheduleOptions.DaysOfWeek -join "," | out-string
        $DOM = $azAutSched.MonthlyScheduleOptions.DaysOfMonth -join "," | out-string

            $obj = new-object psobject -Property @{

            SchedName = $AzAutSched.Name
            LastModifiedTime = (get-date ([DateTime]::Parse($AzAutSched.LastModifiedTime)) -Format "dd-MM-yyyy HH:mm (zzz)")
            IsEnabled = $AzAutSched.IsEnabled
            AutomationAccount = $azAutAcc.AutomationAccountName
            ResourceGroup = $azAutAcc.ResourceGroupName
            NextRun = ([DateTime]::Parse($azAutSched.NextRun))
            StartTime = (get-date ([DateTime]::Parse($azAutSched.StartTime)) -Format "HH:mm (zzz)")
            TimeZone = $azAutSched.TimeZone
            Interval = $azAutSched.Interval
            Frequency = $azAutSched.Frequency
            WeekSchedule = $DOW
            MonthSchedule = $DOM

            }

            $objs += $obj
            }

        }
    }
$objs | sort SchedName | ft -Property SchedName,LastModifiedTime,StartTime,TimeZone,Interval,Frequency,WeekSchedule,MonthSchedule

Then my table ends up with just blank columns for WeekSchedule/MonthSchedule.

I have tried different combos of leaving out the out-string parameter, leaving out the join, setting the property directly in the property line, and as quoted building the variable above the object and referencing it on the property line. None of them work.

Anyone can shed some light as to what I am missing? Or other hints on how to accomplish this are most welcome.

AzureRM module is up to date.


Solution

  • According to my test you need to get individual schedule, not all the schedules in the resource group, it will work in this case:

    foreach ($azAutAcc in $azAutAccs){
        $AzAutScheds = Get-AzAutomationSchedule -AutomationAccountName $azAutAcc.AutomationAccountName -ResourceGroupName $azAutAcc.ResourceGroupName
        $AzAutScheds = $AzAutScheds | Where-Object {$_.IsEnabled -eq "True"}
        foreach ($AzAutSched in $AzAutScheds){
            $AzAutSched = Get-AzAutomationSchedule -AutomationAccountName $azAutAcc.AutomationAccountName -ResourceGroupName $azAutAcc.ResourceGroupName -Name $AzAutSched.Name
            $DOW = $azAutSched.WeeklyScheduleOptions.DaysOfWeek -join "," | out-string
            $DOM = $azAutSched.MonthlyScheduleOptions.DaysOfMonth -join "," | out-string
    
            $objs += new-object psobject -Property @{
                SchedName = $AzAutSched.Name
                LastModifiedTime = (get-date ([DateTime]::Parse($AzAutSched.LastModifiedTime)) -Format "dd-MM-yyyy HH:mm (zzz)")
                IsEnabled = $AzAutSched.IsEnabled
                AutomationAccount = $azAutAcc.AutomationAccountName
                ResourceGroup = $azAutAcc.ResourceGroupName
                NextRun = ([DateTime]::Parse($azAutSched.NextRun))
                StartTime = (get-date ([DateTime]::Parse($azAutSched.StartTime)) -Format "HH:mm (zzz)")
                TimeZone = $azAutSched.TimeZone
                Interval = $azAutSched.Interval
                Frequency = $azAutSched.Frequency
                WeekSchedule = $DOW
                MonthSchedule = $DOM
            }
        }
    }