Retrieve Policy List and Schedules Using PowerShell

Use a PowerShell script to retrieve a list of all policies and schedules for your organization and put them in an Excel spreadsheet.

Note: Due to the way Excel auto-formats numbers, the “schedule months” column may show in scientific notation due to the length. To fix the display of them, highlight the column, right-click on the column, click Format Column, choose Custom, then set the Type to 0.

There are two areas in the code that you’ll need to update to get the script to function:

  1. $orgID = 'YOUR_ORG_ID'
    You will need your Org ID, which can be found by looking at the URL. Select the value after the "?o="

    Note: The user must be a Global or Zone administrator to manage API keys for a zone. See Managing Keys for more information.

  2. $apiKey = 'YOUR_API_KEY'
    In your console, go to Settings > Keys and select the API key. Note that the API key is per user, so that another user will have different API keys.

Policy List and Schedules Script

Use this script to retrieve a list of all your policies in your organization and put them in an Excel spreadsheet.

See also Scheduling Examples and Trailing Zeros description.

# Replace the two variables below with your Org ID and your API key
$orgID = 'YOUR_ORG_ID'
$apiKey = 'YOUR_API_KEY'


# Replace if you'd like the output in another location
$expDir = 'C:\Temp\Policy_Schedules.csv'


# --------------------------------------


$servers = @()
$output = @()
$page = 0
$limit = 500


$headers = @{ "Authorization" = "Bearer $apiKey" }


while($true) {


    $url = "https://console.automox.com/api/policies?o=$orgID&l=$limit&p=$page"
    $resp = (Invoke-WebRequest -Method Get -Uri $url -Headers $headers).Content | ConvertFrom-Json


    $output =  $resp | Select-Object id, name, policy_type_name, `
        @{ Name = 'schedule days'; Expression = { [Convert]::ToString($_.schedule_days,2) }}, `
        @{ Name = 'schedule weeks'; Expression = { [Convert]::ToString($_.schedule_weeks_of_month,2) }}, `
        @{ Name = 'schedule months'; Expression = { [Convert]::ToString($_.schedule_months,2) }}, `
        schedule_time, notes, create_time, server_count


    $servers += $Output
    $page += 1


    if($resp.Count -lt $limit) {
        break
    }


}


$servers | Export-Csv -Path $expDir -NoTypeInformation -Force

Scheduling Examples and Trailing Zeros

Example days per week :

For scheduling days of the week, there will be 7 digits with a trailing zero to mark the end. This starts on Sunday and ends on Monday. 1 represents ON and 0 represents OFF for each day of the week.

Sun|Sat|Fri|Thu|Wed|Tue|Mon|TrailingZero

Every Day : 11111110

Mon/Wed/Fri only : 00101010

Example weeks per month:

Weeks are scheduled similarly to days, except there are 5 digits with a trailing zero. This starts on the 5th week of the month and ends with the 1st week of the month.

5th|4th|3rd|2nd|1st|TrailingZero

Every Week: 111110

2nd/4th weeks only: 010100

Example months per year:

Months continue the trend with 12 digits with a trailing zero. Starting with December and ending with January.

Dec|Nov|Oct|Sep|Aug|Jul|Jun|May|Apr|Mar|Feb|Jan|TrailingZero

Every Month: 1111111111110

Mar/Jun/Sep/Dec = 1001001001000

Related Topics:

Community: Get A List Of All Policies For Your Organization and Their Schedules