Skip to main content

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"

I made a change to the script to allow for multiple devices. Enjoy!

####Process Params
#### Example .\Fullimage.ps1 -DeviceNames "tec006", "tec081", "adm004" -apiKey "xxxxx-xxxxx-xxxxxxx"
param ([System.Collections.ArrayList]$DeviceNames, [String]$apiKey)


#### List of software to install as base image
$Policylist = "Install Google Chrome", "Uninstall Bloatware", "Apply Dell Driver Updates", "Install Sophos", "Install Mozilla Firefox", "Install Adobe Reader", "Install EgnyteWebEdit", "Install Java", "Apply All Patches Except - Weekly Sweep"
#### Org ID Settings (put your Org ID here)
$orgID = '41220'


###### 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"
$AutomoxDevices =(Invoke-restmethod -Method Get -Uri $url -Headers $headers)
foreach ($Item in $AutomoxDevices) {$Items += $Item}
$pageindex++
}
Until ($AutomoxDevices.count -lt 500)
###


########Install Policies
$ServerIDs = $Items | where {$DeviceNames -contains $_.name} | Select -ExpandProperty Id

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


foreach ($policy in $policies) {

$headers = @{ "Authorization" = "Bearer $apiKey" }
foreach ($server in $ServerIDs) {

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

Invoke-restmethod -Method POST -Uri $url -Headers $headers
}
}
Write-Host "Base Image Deploying, check machine in Automox for status"

 


I noticed an issue with the first line of my updated script caused by declaring it as an array. If you only specify a single device, you will get this error. 

Cannot convert the "test" value of type "System.String" to type "System.Collections.ArrayList".
At line:1 char:1
+ System.Collections.ArrayList]$DeviceNames = "test"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) o], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

 

To fix this I let the parameter auto declare. In testing, it is working as expected. See below for the update. 

param ($DeviceNames, eString]$apiKey)

 


Reply