Script | Export all applied patches from all company machines to CSV

Hello!

Thought I would share this API script I wrote that exports all applied patches on all machines to a CSV.

UseCase: For audits this allows an easy way to export every single patch on a machine to prove that machines have been patched during a time period.

Notes:

  • This is meant to be run on a machine NOT through a worklet.
  • Make sure to add in your own API Key for this to work.
  • For a full list of objects you can pull from the API see Automox API Documentation under “Response”.
  • This script is not perfect. If run multiple times it will keep adding to the same CSV, so make sure to either clear the CSV and save or just delete it.

Powershell Code:

#Set Execution policy to allow for 3rd party modules
Set-ExecutionPolicy Unrestricted -Force

#Define API Key and URL to get initial Machine list
$apiKey = "*INSERT YOUR API KEY HERE*"
$apiUrl = "https://console.automox.com/api/servers/"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $apiKey")
#$headers.Add("Cookie", "ax_session=eyJpdiI6IlpqM3NxdU03Mk81dHg0RjBkOGZRQmc9PSIsInZhbHVlIjoiL0Z2QVljTVF3SkV2L0Jod0lKMUFHQ0dMalMxQVNNdkswNWRRb2hkN1NiTHBFcWk4S2pXempXU2hpQzhybHE4b3loREhXRUxKSHZPMDBmMHVHNERzVlU2enphQm1sTTA3U2FCSmNlU0lJSjdEKzdDRHRqZXBlNXFSUThWb01zNk5UdXZheXpUNlExeDJKRXdONlZBbFZsV0tYc1hVMkl1Sm1QMFBvcGxKSEZYMkJNS3FkOXpPSStVV1JwNGVEN3ZlYkl5dHhvZnNHNURzR1FjMEUvZCthVyt4S09ZQmt1R2xaQUFzOGJpTmozTVcrdWtMNVdNQUR1cUNwUi9lU0lDamJVVkoydm84UEdsR2NldEVENVQ2TTdweWVkeGJtdjJaeHp6MlVpSUZXUE92MXZqbU0vSjRFcG9CWDVjZkYzeGEycnN5RGZKbDJReFIvbzdKdXRoZ0VVRnEweXhGSHd1dWozWlhTSUhNMVNVMEVGV0Faakw3ZVVWUFI5NDRSSDJKR3BhdkZ5Yk9EeklkNGtPOEhLb1B0T3pJci9JMFpDMUk5dExXZmJINHZmSDk1c3c5QlcyTWwxZEs0V1orVlNmWjFYdVRUZTg1WVNFMVhsdG9STThYMmduWEc4T1A5VGpoU2d6WVRlWXY1N3ByVkFlRVlOcExqT1hxdnpIRHN1RzMiLCJtYWMiOiIzNzc3MzM2MWM0MmQxMTA4NjliZTk2MzEwOTg1NWQ5NWNmZTZhODBlZTQyZGQwMTM3YTI3YTJhMTgwYTg5NzcwIn0%3D")

$response = Invoke-RestMethod $apiUrl -Method 'GET' -Headers $headers -Body $body
$response | ConvertTo-Json

$name = $server.name;
#For each machine get the patch info according to machine ID and Org.
foreach($server in $response){
    #Define Server ID, Org ID, and Group ID.
    $id = $server.id
    $organization_id = $server.organization_id
    $server_group_id = $server.server_group_id

    $apiUrl = "https://console.automox.com/api/servers/$($id)/packages?o=$($organization_id)"
    $response = Invoke-RestMethod $apiUrl -Method 'GET' -Headers $headers -Body $body
    #Define which objects you want to be included in the CSV. See (Under "Response") for full list:	https://docs.automox.com/api/endpoints/list-software-packages-for-a-device
    $DesiredProperties = $response | Select-Object $server.name, installed, create_time, display_name, package_id, software_id, cve_score
    #Add the information to the CSV.
    $DesiredProperties | ConvertTo-Csv -NoTypeInformation | Add-Content -Path "C:\temp\patch_output.csv"
}
1 Like