Create a gist now

Instantly share code, notes, and snippets.

#!/bin/bash
# v 0.9, released January 28th, 2017
# This script is designed to evaluate a current ManagedInstalls.plist file, then make a change to the specified key. The script will then log a success message to a Slack webhook.
# Original Script authored by Tom Bridge, with advice from Richard Purves, Ben Toms and others.
# You will need to supply your own preference keys to check, as well as preferred values.
# Make sure to get your own Slack Web Hook URL from the Customizations and Apps section of your Slack Control Panel.
# Copyright 2017 Tom Bridge, Technolutionary LLC
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# This script makes a huge assumption: that all your munki settings live in /Library/Preferences/ManagedInstalls.plist and not in the root-accessible preferences file. If you are specifying some of the settings you want to change in the root-accessible preferences file, you will need to alter this script.
MPL="/Library/Preferences/ManagedInstalls.plist"
DEFAULTS="/usr/bin/defaults"
PREFTOCHECK="SoftwareRepoURL"
SHOULDBE="https://path/to/your/munki_repo"
CHANNEL="#yourchannel"
SLACKHOOK="put your slack hook url here"
echo "First up: Are you an admin user? Enter your password below:"
#Let's see if this works...
#This isn't bulletproof, but this is a basic test.
# Thanks JD!
if [[ $(sudo -v) == 'Sorry'* ]]; then
echo "You must have root privileges to run $0"
exit 1;
fi
echo "Hi there. We're going to check your current Munki settings and see if they need changing."
CHECK=$( $DEFAULTS read $MPL $PREFTOCHECK )
echo "Current status of $PREFTOCHECK is set to: $CHECK"
# Let's check the value.
if
[[ $CHECK == $SHOULDBE ]]; then
echo "Hey, cool, this value is what it's supposed to be! You're all done here."
exit 0
else
echo "Okay, the current value doesn't match what we want it to be. Let's get that changed over to our desired value."
fi
sudo $DEFAULTS write $MPL $PREFTOCHECK $SHOULDBE
# We've made a change, now let's tell Slack."
IDENT=$( $DEFAULTS read $MPL ClientIdentifier )
read -d '' SLACK_PAYLOAD_DATA << EOF
{
"channel": "$CHANNEL",
"username": "MunkiBot",
"icon_emoji": ":monkey-face:",
"attachments": [
{
"fields": [
{
"title": "Munki Client Update",
"value": "This is a client update from $IDENT. I have updated ${PREFTOCHECK} to be ${SHOULDBE} successfully." ,
"short": false
}
]
}
]
}
EOF
SLACK_COMMAND="curl -X POST --data-urlencode 'payload=${SLACK_PAYLOAD_DATA}' ${SLACKHOOK}"
eval ${SLACK_COMMAND}
echo "All Set. I've reported this to $CHANNEL on your Slack."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment