JamfHelper "newbie" Question

spotter
New Contributor III

I've crafted a very simple script which utilizes the jamfhelper to post a question to the logged in user which requires them to select Yes or No. My question is how or can I capture the user selection in some type of report???

i'm also wanting to capture LoggedInUser, Date, Hostname....

thanks in advance
::sp

3 REPLIES 3

mm2270
Legendary Contributor III

Logged in user can be captured a few different ways. A few popular ways-

loggedInUser=$( ls -l /dev/console | awk '{print $3}' )
loggedInUser=$( who | awk '/console/{print $1}' )

As for date, what format do you want the date in? There are lots of ways to display it. If you want something similar to how the Casper Suite displays date data, then this will work

DATE=$( date +"%Y/%m/%d" )

That outputs something like 2013/10/08. If you want the timestamp as well, try this-

DATE=$( date +"%Y/%m/%d %T" )

Do man date in Terminal for the run down on how to format the output from the date command. There are a lot of options.

For hostname, do you mean the Computer Name, as it would appear in the Sharing Pref Pane? If so, easy to do, but why would you need that since its already captured in the Casper Suite anyway?
If you still want it, then this is one way. As with everything, multiple ways to skin this cat-

COMPNAME=$( scutil --get ComputerName )

In regards to jamfHelper, if you take a look at jamfHelper's help page, you'll see that buttons, such as "-button1", or "-button2", return an exit code when pressed. -button1 returns "0" and -button2 returns "2" You can capture this in a script in a few differet ways. One way is to place your message into a variable and then echo back the variable or do a test function in the script.

#!/bin/sh

MyMSG=$( /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -description "Choose Yes or No" -button1 "Yes" -button2 "No" )

echo "$MyMSG"

In any script that's run from Casper, the lines that are echoed get uploaded into the policy log. I'm not sure if that's sufficient for your reporting though. If not, you can do something like place a file or folder somewhere on the system based on the response and then build an Extension Attribute that would report on it.
So putting this all together, you could do something like this. The file creation (touch) is just an example. You'd probably want to use something more specific than what I have below.

#!/bin/sh

loggedInUser=$( ls -l /dev/console | awk '{print $3}' )
DATE=$( date +"%Y/%m/%d %T" )
COMPNAME=$( scutil --get ComputerName )

MyMSG=$( sudo /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -description "Choose Yes or No" -button1 "Yes" -button2 "No" )

if [[ "$MyMSG" == "0" ]]; then
    echo "On $DATE, loggedInUser on $COMPNAME pressed Yes"
    touch /private/var/somefolder/yes
elif [[ "$MyMSG" == "2" ]]; then
    echo "On $DATE, $loggedInUser on $COMPNAME pressed No"
    touch /private/var/somefolder/no
else
    echo "Exit code was $MyMSG"
fi

exit

Then make an Extension Attribute that would just look for the "yes" or "no" file and report on it. Simple example below:

#!/bin/sh

if [ -f "/private/var/somefolder/yes" ]; then
    echo "<result>yes</result>"
elif [ -f "/private/var/somefolder/no" ]; then
    echo "<result>no</result>"
else
    echo "<result>N/A</result>"
fi

You'll probably get several different answers on all of the above. My way is by no means the only approach you can take to this, nor is it necessarily the best way.

richmac
New Contributor III

You can use the values returned by jamfhelper to trigger other actions. If you type

/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -help
in the terminal you will get its man page, at the bottom see the section that reads "Return Values". Button 1 clicked returns the value "0" Button 2 returns the value "2". Use these values to write a log or something.
eg

if [ $? == 0 ] then;
echo "User clicked Yes"
else
echo "User clicked No"
fi

spotter
New Contributor III

here is what I came up with....

#!/bin/bash

LoggedInUser=`who | grep console | awk '{print $1}'`
now=`date`

if [ "$LoggedInUser" == "" ]; then
    echo "No Logged in User" > /Library/Application Support/JAMF/Attest.txt
else
        RESPONSE=`/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType hud -lockhud -heading "Childrens Medical Center - Attest REQUIRED" -description "Childrens Medical Center requires that you attest all devices, Have you completed the attestation form?" -button1 "YES" -button2 "NO" -startlaunchd`

        if [ "$RESPONSE" == "0" ]; then
            echo "$LoggedInUser chose YES on $HOSTNAME at $now" > /Library/Application Support/JAMF/Attest.txt
            exit 0
        else
            echo "$LoggedInUser chose NO on $HOSTNAME at $now" > /Library/Application Support/JAMF/Attest.txt
        fi
fi
exit

and here is the Extension Attribute...

#!/bin/sh
value=`cat /Library/Application Support/JAMF/Attest.txt`
echo "<result>$value</result>"

so far it seems to do what i need...

I do appreciate the replies