Use API to run multiple policies on device by name

I created a fun and friendly API script to run multiple policies on a device by using device name and policy names. You can customize it so that $Policylist is a parameter if you like and execute different policies as needed. Even though I am only executing on one device, I scripted it to pull all device names and ID’s so that I can customize it to run on a list of devices if we need to. Pieces of this script were used from many different posts here as well as the KB. Thanks to all that posted and shared with the group. I think this brings a lot of it together and gives some flexibility to automate just about whatever needs automating. Enjoy.

####Process Params
#### Example .\baseimage.ps1 -DeviceName "tec006" -apiKey "xxxxx-xxxxx-xxxxxxx"
param ([String]$DeviceName, [String]$apiKey)



#### List of software to install as base image
$Policylist = "Apply Dell Driver Updates","Install Sophos", "Install Mozilla Firefox", "Install Google Chrome", "Install Adobe Reader", "Install EgnyteWebEdit", "Install Java"
#### Org ID Settings (put your Org ID here)
$orgID = 'XXXXX'


###### Get Policies
$headers = @{ "Authorization" = "Bearer $apiKey" }
$url = "https://console.automox.com/api/policies?o=$orgID"

$AutomoxPolicies = (Invoke-RestMethod -Method Get -Uri $url -Headers $headers)


###### Get Devices
$pageindex = 0
$Items = New-Object -TypeName "System.Collections.ArrayList"
$headers = @{ "Authorization" = "Bearer $apiKey" }

Do {
	$url = "https://console.automox.com/api/servers?o=$orgID&page=$pageindex"
	$Devices =(Invoke-restmethod -Method Get -Uri $url -Headers $headers)
	foreach ($Item in $Devices) {$Items += $Item}
	$pageindex++
}
Until ($Devices.count -lt 500)
###


########Install Policies
$serverID = $Items | where {$_.name -match $DeviceName} | Select -ExpandProperty Id

$policies = $AutomoxPolicies | where {$_.name -in $Policylist} | Select -ExpandProperty Id


foreach ($policy in $policies) {

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

    $url = "https://console.automox.com/api/policies/$policy/action?o=$orgID&action=remediateServer&serverId=$serverID"

    Invoke-WebRequest -Method POST -Uri $url -Headers $headers

}
Write-Host "Base Image Deploying, check machine in Automox for status"
2 Likes