selectPolicy.php for Self Service - Start policies based on OS X version and space available

wakco
Contributor

Due to fact that the inventory in the JSS does not track free space on the startup drive, and Self Service does not double check the current OS X version or free space, I created this script I called selectPolicy.php to allow a nice user/admin friendly way of having items show in Self Service, but upon use both space and OS X version requirements are checked before running a custom event/trigger to actually perform the install.

When using you will need a set of basic policies for the installers with nothing but a custom event/trigger set on each of them.
The policy that will appear in Self Service will execute this script with the following variables:

4 - Should be the Application name i.e. What is being installed, this is used for error and notification messages as to the status of the install.
5 - Is the minimum space required in gigabytes to install.
6 - Is a comma separated list of minimum OS X versions required for each install policy.
7 - Is another comma separated list, but of the custom event/trigger names of each install policy setup in JSS.
8 - Is an optional message to tell the user who to contact if they have problems.

selectPolicy.php:

#!/usr/bin/php
<?php
// Version 1.0 created by Richard Smith, July 2015
// Support and Copyright is zip, zero, none, as it is available "as is" and can be changed to your hearts content.


if (count($argv) < 8) {
 print("Error, missing required input from JSS, Please add the Application Name, Space Required, OS Version(s) Required, JSS Custom Event(s), and Contact information for the error message
");
 exit(1);
}

// JSS auto filled
$mountPoint = $argv[1];
$computerName = $argv[2];
$username = $argv[3];

// JSS manual fill
$appName = $argv[4];
$spaceRequired = $argv[5];
$versionRequired = explode(",", $argv[6]); // comma separated list of OS versions required for each custom event
$policyEvent = explode(",", $argv[7]); // comma separated list of custom events to match OS versions required
$pleaseContact = "Please contact your local IT support for help";
if (isset($argv[8])) { $pleaseContact = $argv[8]; }

if (count($versionRequired) != count($policyEvent)) {
 print("Error, comma separated lists of OS Version(s) and JSS Custom Event(s) have a different number of items in them, please list the minimum version required for each custom event.
");
 exit(1);
}

// Messages for user
$errOS = "The computer requires an OS upgrade before $appName can be installed ($versionRequired[0] required).";
$errSpace = "The computer needs more space available to install $appName ($spaceRequired GB required).";

// Primary HD space
$GetDrive = preg_split('/s+/', trim(`df -g $mountPoint | grep "/dev/"`));
$spaceFree = $GetDrive[3]; // 3 contains available space

$spaceErr = ($spaceFree < $spaceRequired);


// OS Version
$osVersion = trim(`sw_vers -productVersion`);

function osConvert ($os) {
 $osBreakout = explode(".", $os);
 $newOS = ($osBreakout[0] * 10000) + ($osBreakout[1] * 100);
 if (isset($osBreakout[2])) {
  $newOS = $newOS + $osBreakout[2];
 }
 return $newOS;
}

$myOSVersion = osConvert($osVersion);

$versionPolicyIndex = -1; // we are looking for the first failure
foreach($versionRequired as $myVersion) {
 if (osConvert($myVersion) > $myOSVersion) {
  break;
 }
 $versionPolicyIndex++;
}
$versionErr = ($versionPolicyIndex == -1);


// If there is a problem, report it and error out
if ($spaceErr || $versionErr) {
 if ($spaceErr) { $myErr = $errSpace; }
 if (isset($myErr) && $versionErr) {
  $myErr = $myErr."
".$errOS;
 } elseif ($versionErr) {
  $myErr = $errOS;
 }
 $myErr = $myErr."
".$pleaseContact;
 print($myErr."
");
 `/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -startlaunchd -windowType utility -title "Error" -heading "Cannot install $appName" -description "$myErr" -icon /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertCautionIcon.icns -button1 "Cancel" -timeout 600`;
 exit(1);
}


// Tell JSS to execute the selected policy
print("Installing $appName with policy $policyEvent[$versionPolicyIndex]
");
`/Library/Application Support/JAMF/bin/Management Action.app/Contents/MacOS/Management Action -title "Self Service" -activate "com.jamfsoftware.selfservice" -subtitle "Installing $appName" -message "Another notification will be sent on completion."`;

passthru("jamf policy -event $policyEvent[$versionPolicyIndex] -forceNoRecon", $installErr);
// Forcing no recon to help reduce the number of times an inventory update is performed, when using this script it is
// best to use inventory update on the policy running this script, and not on the policy or policies this script is calling.
// Using passthru() to pass log messages we do not need on through, and to capture the return state in case of errors, such as install failed.

if ($installErr) { // Install Failed
 `/Library/Application Support/JAMF/bin/Management Action.app/Contents/MacOS/Management Action -title "Self Service" -activate "com.jamfsoftware.selfservice" -subtitle "Installation Failed" -message "$pleaseContact"`;
 print("$appName Not Installed
");
} else { // Install Succeeded
 `/Library/Application Support/JAMF/bin/Management Action.app/Contents/MacOS/Management Action -title "Self Service" -activate "com.jamfsoftware.selfservice" -subtitle "Installation Complete" -message "$appName is now ready to use."`;
 print("$appName Installed
");
}
exit($installErr);

?>

I also created this script because I have previously created 3 related feature requests and supported a 4th that this script helps to solve:
Add a tick box option to accept one package installed as successful
Mac Self Service field specific enforce inventory update when opened.
Mac Self Service option to have a policy grey out when exceptions met.
Add specialized software distribution policies

1 REPLY 1

wakco
Contributor

I'm just wondering if anyone is using this?