Last active
September 20, 2023 16:30
-
-
Save AX-AMaxwell/a658ead6bf490b7fcbc740197e5a95ce to your computer and use it in GitHub Desktop.
Get Unique Software Packages and Automox Patching Support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Retrieves all software packages present in your organization and outputs a formatted table indicating whether patching via Automox is supported. | |
.NOTES | |
HISTORY | |
Author : Anthony Maxwell | |
Date : 09/06/2023 | |
Version : 1.0.0 | |
Release Notes : | |
- Initial release. | |
#> | |
######################################### | |
# PARAMETERS | |
# define our Automox API token | |
$token = '' | |
# define our Automox Organization ID | |
$orgID = '' | |
# define our csv filepath | |
# optional - if this is commented | |
# out or an empty string, the CSV | |
# will not be generated | |
$filePath = '' | |
######################################### | |
# VARS | |
# define our api base url | |
$base = 'https://console.automox.com/api' | |
# define our api endpoint | |
$endpoint = "/orgs/${orgID}/packages" | |
# define our query params | |
$query = ( | |
"o=${orgID}", | |
'limit=500' | |
) | |
# define our request headers | |
$headers = ( | |
( 'Authorization', "Bearer ${token}" ), | |
( 'Accept', 'application/json' ) | |
) | |
# create the webclient | |
$client = [ System.Net.WebClient ]::new() | |
# define the arraylist to hold our software package objects | |
$results = [ System.Collections.ArrayList ]::new() | |
# define our parameter formatters | |
$name = @{ n = 'Name'; e = { $_.display_name } } | |
$managed = @{ n = 'Managed'; e = { ( 'NO', 'YES' )[ $_.is_managed ] } } | |
######################################### | |
# API Scrape | |
# enumerate our defined headers | |
foreach ( $header in $headers ) | |
{ | |
# assign the header to the webclient instance | |
$client.Headers.Add( $header[ 0 ], $header[ 1 ] ) | |
} | |
# construct the full url | |
$url = "${base}${endpoint}?$( $query -join '&' )" | |
# create an iterator ( $i ) to navigate through paged responses | |
for ( $i = 0; $i -lt 1000; $i++ ) | |
{ | |
# define our current page url | |
$currentPage = "${url}&page=${i}" | |
Write-Host "Requesting result page $( $i + 1 ) from: ${currentPage}" | |
try | |
{ | |
# interpolate our page number and fire the web request | |
$rawJSON = $client.DownloadString( $currentPage ) | |
# check for an empty response | |
# if the response is null, empty or an empty json list - we've reached the end of our results | |
# break the loop | |
if ( $null -eq $rawJSON -or $rawJSON -eq [ System.String ]::Empty -or $rawJSON -eq '[]' ) { break } | |
} | |
catch [ System.Net.WebException ] | |
{ | |
# retrieve the failure status code | |
# cast to integer to retrieve the numerical status code ( ex: 401 ) | |
[ System.Int32 ] $statusCode = $_.Exception.Response.StatusCode | |
# output the error | |
[ System.Console ]::Error.WriteLine( @" | |
ERROR : Web request failed. | |
REQUESTED URL : ${currentPage} | |
HTTP RESPONSE : ${statusCode} | |
"@ ) | |
exit 1 | |
} | |
# convert our json software to pscustomobjects | |
$software = $rawJSON | ConvertFrom-Json | |
# enumerate software | |
# format our properties | |
# add each software package to our arraylist | |
$software | | |
Select-Object $name, $managed | | |
ForEach-Object { $results.Add( $_ ) | Out-Null } | |
} | |
# filter down to unique items by Name and output | |
$results | Sort-Object -Property Name -Unique | Format-Table * | |
# evaluate if we have a file path | |
if ( $null -ne $filePath -and $filePath -ne [ System.String ]::Empty ) | |
{ | |
# output the csv file | |
$results | Export-Csv -Path $filePath | Out-Null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment