Skip to main content
Solved

Make one common change to 100+ policies - best method?

  • January 12, 2022
  • 6 replies
  • 179 views

jbh
Forum|alt.badge.img
  • Pro
  • 18 replies

I have a couple hundred policies in Automox for patching.  If I want to make a change to all of them, there are multiple ways to do it via API.  I’m wondering what the most efficient method for doing it is.

 

For example, if I just want to change the patch severity on all of them from, say “high” to “low” - what’s the recommended method for doing that on 100+ policies?

Best answer by jbh

######################################################
$apiKey = 'yourApiKeyHere'
$orgID = "yourorIdHere"
$changefrom = "high"
$changeto = "low"

#####Only update the two Variables in this block######
######################################################

#Api Request
$headers = @{ "Authorization" = "Bearer $apiKey" }
$uri = "https://console.automox.com/api/policies?o=$clientID"
$responses = (Invoke-WebRequest -Method Get -Uri $uri -Headers $headers).Content | ConvertFrom-Json

$ids = $responses | ? {$_.name -match "Windows Policy"} | Select -ExpandProperty id
#Use this for one device# $ids = '123456'
#Use this for multiple devices#  $ids = @('xxxx','xxxx','xxxx')
$middle = '?o='

$ids | % {
$PolicyID = $_

$headers = @{ "Authorization" = "Bearer $apiKey" }
$url = "https://console.automox.com/api/policies/$policyID$middle$orgID"
$response = (Invoke-WebRequest -Method Get -Uri $url -Headers $headers).Content

$policyfinal = $response.replace("$changefrom","$changeto")


$body = @"
$policyfinal
"@


$headers = @{
    "Authorization" = "Bearer $apiKey"
    "Content-Type" = "application/json"
  }
$url = "https://console.automox.com/api/policies/$policyID$middle$orgID"
Invoke-WebRequest -Method Put -Uri $url -Headers $headers -Body $body
}

This iterates through any policy that has a name that includes “Windows Policy” and will update the priority of the policy updates from “high” to “low”.  While this is a very specific niche use-case, the overall code/script itself can easily be modified to update any other part of a policy through the API.

View original
How helpful was this post to you?

6 replies

Brittany
Forum|alt.badge.img
  • Pro
  • 40 replies
  • January 12, 2022

I checked with our support team and they mentioned that the best way to do this would be to loop them through the API, but unfortunately we don’t have an easier way. If you have any questions about accomplishing this via API, let me know and I can get back to you!


jbh
Forum|alt.badge.img
  • Author
  • Pro
  • 18 replies
  • January 13, 2022

Thanks.  Yes, I’d love to see the actual commands that one would use to do this.  That would be very helpful.


jbh
Forum|alt.badge.img
  • Author
  • Pro
  • 18 replies
  • January 18, 2022

Just wanted to follow up and say that Automox reached out to me to schedule a 1:1 call to get this done.  Props to Automox and their support.  I’m happy to put the code here if it would be useful for anyone else and doesn’t violate any rules.


  • 0 replies
  • January 20, 2022

@jbh -- absolutely! We’d like to get that added to the API documentation, for sure. Keep us posted!


jbh
Forum|alt.badge.img
  • Author
  • Pro
  • 18 replies
  • Answer
  • January 20, 2022
######################################################
$apiKey = 'yourApiKeyHere'
$orgID = "yourorIdHere"
$changefrom = "high"
$changeto = "low"

#####Only update the two Variables in this block######
######################################################

#Api Request
$headers = @{ "Authorization" = "Bearer $apiKey" }
$uri = "https://console.automox.com/api/policies?o=$clientID"
$responses = (Invoke-WebRequest -Method Get -Uri $uri -Headers $headers).Content | ConvertFrom-Json

$ids = $responses | ? {$_.name -match "Windows Policy"} | Select -ExpandProperty id
#Use this for one device# $ids = '123456'
#Use this for multiple devices#  $ids = @('xxxx','xxxx','xxxx')
$middle = '?o='

$ids | % {
$PolicyID = $_

$headers = @{ "Authorization" = "Bearer $apiKey" }
$url = "https://console.automox.com/api/policies/$policyID$middle$orgID"
$response = (Invoke-WebRequest -Method Get -Uri $url -Headers $headers).Content

$policyfinal = $response.replace("$changefrom","$changeto")


$body = @"
$policyfinal
"@


$headers = @{
    "Authorization" = "Bearer $apiKey"
    "Content-Type" = "application/json"
  }
$url = "https://console.automox.com/api/policies/$policyID$middle$orgID"
Invoke-WebRequest -Method Put -Uri $url -Headers $headers -Body $body
}

This iterates through any policy that has a name that includes “Windows Policy” and will update the priority of the policy updates from “high” to “low”.  While this is a very specific niche use-case, the overall code/script itself can easily be modified to update any other part of a policy through the API.


  • 0 replies
  • January 20, 2022

H*ck yeah, thanks!! :metal:


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings