Passing the name of a policy into another policy

aparten
New Contributor III

I know this is a bit obscure, but does anyone know if there is a way for a policy to pull in the name of another policy, like as a variable.

For example, policy 1 runs installing some package, does inventory, and then custom triggers policy 2. Policy 2 then outputs the name of policy 1 as a variable (or any way really) as info to be used in the policy.

Again, kind of random and may not be doable, but figured I'd check.

Thanks!
Alex

1 ACCEPTED SOLUTION

talkingmoose
Moderator
Moderator

@aparten, if we go the route @shyam.sm suggested of reading the jamf.log file, something like this should get the name of the last policy run:

/usr/bin/grep "Executing Policy" /private/var/log/jamf.log | /usr/bin/tail -n 1 | /usr/bin/awk -F "Executing Policy " '{ print $2 }'

That may get you the current policy and not the one run before it. If so, this should get the one above it:

/usr/bin/grep "Executing Policy" /private/var/log/jamf.log | /usr/bin/tail -n 2 | /usr/bin/head -n 1 | /usr/bin/awk -F "Executing Policy " '{ print $2 }'

Neither is pretty and there may be easier ways of doing what you need, but if it works...

View solution in original post

6 REPLIES 6

shyam_sm
New Contributor II

Maybe you can grep it from JAMF.log, you can use the Policy 2 to grep the name of Policy 1 from jamf.log

talkingmoose
Moderator
Moderator

Can you explain what you're trying to accomplish (not how)? There may be a better way to handle what you're after.

aparten
New Contributor III

@shyam.sm - was thinking the same thing and started testing with that, seems possible but not sure if it's entirely reliable... (I'll def keep testing it out though.)

@talkingmoose - I have a few policies that could benefit from this idea. One of the more useful ones is where the last policy being called is basically just sending an alert to a Slack channel once the policy has ran. This allows us to know exactly when a policy has completed on a select few that we'd need to know right away. Being able to make this one single policy and output info from the referring policy (for example, its name) would allow me to not have to script something for every single policy to be sent to Slack. Hope that makes sense!

talkingmoose
Moderator
Moderator

@aparten, have you considered a script and a script parameter in a policy?

If your script includes something like this:

#!/bin/bash
policyName="$4"
echo "$policyName"

and then in your policy put its name in the Parameter 4 field, it should echo whatever you put there. Anything echoed in a script run via policy is included in the policy's log.

f39e5c11c5f14328b9deebf453bcbaa0

aparten
New Contributor III

@talkingmoose - I did try that, but the info I need is from the first policy and the actual script is ran separately in the last policy being triggered.

talkingmoose
Moderator
Moderator

@aparten, if we go the route @shyam.sm suggested of reading the jamf.log file, something like this should get the name of the last policy run:

/usr/bin/grep "Executing Policy" /private/var/log/jamf.log | /usr/bin/tail -n 1 | /usr/bin/awk -F "Executing Policy " '{ print $2 }'

That may get you the current policy and not the one run before it. If so, this should get the one above it:

/usr/bin/grep "Executing Policy" /private/var/log/jamf.log | /usr/bin/tail -n 2 | /usr/bin/head -n 1 | /usr/bin/awk -F "Executing Policy " '{ print $2 }'

Neither is pretty and there may be easier ways of doing what you need, but if it works...