Posted on 08-20-2020 09:30 AM
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
Solved! Go to Solution.
Posted on 08-24-2020 03:09 PM
@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...
Posted on 08-20-2020 10:13 AM
Maybe you can grep it from JAMF.log, you can use the Policy 2 to grep the name of Policy 1 from jamf.log
Posted on 08-20-2020 10:22 AM
Can you explain what you're trying to accomplish (not how)? There may be a better way to handle what you're after.
Posted on 08-20-2020 11:23 AM
@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!
Posted on 08-20-2020 08:40 PM
@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.
Posted on 08-24-2020 12:45 PM
@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.
Posted on 08-24-2020 03:09 PM
@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...