Skip to content

Instantly share code, notes, and snippets.

@dderusha
Created October 29, 2015 20:27
Show Gist options
  • Save dderusha/c32294dcbd3d643b327c to your computer and use it in GitHub Desktop.
Save dderusha/c32294dcbd3d643b327c to your computer and use it in GitHub Desktop.
Set OS X 10.10.x Time Zone using a script and Casper parameter 4
#!/bin/sh
# NAME
# setTimeZone.sh -- Set the time zone
#
# SYNOPSIS
# sudo setTimeZone.sh
# sudo setTimeZone.sh <mountPoint> <computerName> <currentUsername> <timeZone>
#
# If the $timeZone parameter is specified (parameter 4), this is the time zone that will be set.
#
# If no parameter is specified for parameter 4, the hardcoded value in the script will be used.
#
# DESCRIPTION
# This script sets the system time zone as reflected in the Date & Time preference pane with the
# System Preferences application. It has been designed to work on Mac OS X 10.3 and higher.
# HISTORY
#
# Version: 1.0
#
# - Created by Nick Amundsen on August 5th, 2008
#
# Version: 2.0
#
# - Updated by Brock Walters October 28 2014
#
# Version: 3.0
#
# - Updated by Dan DeRusha 2015-28-10
####################################################################################################
#
# DEFINE VARIABLES & READ IN PARAMETERS
#
####################################################################################################
# A HARDCODED VALUE FOR "timeZone" CAN BE SET BELOW.
# A list of accepted time zone values can be generated using the following command in Terminal:
#
# sudo systemsetup -listtimezones
#
# Delete the double quotes and replace with the desired time zone name, e.g. timeZone=Pacific/Honolulu
# If this script is to be deployed via policy using the JSS leave the next line as is.
# timeZone=""
set -euo pipefail
# "e" and "o pipefail" let the script exit immediately on errors, "u" exits on undefined variables
# IFS=$'\n\t' # remove "Space" as a field delimiter, helps prevent string splitting.
startTime=$(date "+%Y-%m-%d_%H:%M:%S")
scriptName=$(basename -- "$0") # $0 is the full path to the script. basename removes the path. dirname would return only the path
logDate=$(date "+%Y-%m-%d")
logDir=$(echo "/var/log/$scriptName" | cut -d . -f 1)
logName="$(echo "$scriptName" | cut -d . -f 1).$logDate.log"
logPath="$logDir/$logName"
OS=$(/usr/bin/defaults read /System/Library/CoreServices/SystemVersion ProductVersion | awk '{print substr($1,1)}')
maj=$(/usr/bin/defaults read /System/Library/CoreServices/SystemVersion ProductVersion | awk '{print substr($1,1,2)}')
ref=$(/usr/bin/defaults read /System/Library/CoreServices/SystemVersion ProductVersion | awk '{print substr($1,4,2)}' | tr -dc '0-9')
timeZone="$4"
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 4 AND, IF SO, ASSIGN TO "timeZone"
if [ "$4" != "" ] && [ "$timeZone" == "" ]; then
timeZone=$4
fi
####################################################################################################
#
# SCRIPT CONTENTS - DO NOT MODIFY BELOW THIS LINE
#
####################################################################################################
touchPath() {
argT=${1:-}
if [ -z "$argT" ]; then
echo "no argument provided to touchPath";
exit 1;
fi
for path in "$@"; do
if [ ! -d "$(dirname -- "$path")" ]; then
mkdir -p -- "$(dirname -- "$path")"
fi
touch -m -- "$path"
done
}
# EchoLog writes an argument or piped data to a syslog-style line in stdout and a logfile
EchoLog(){
logTime=$(date "+%Y-%m-%d_%H:%M:%S") # set date and time here to update on each write
export BASHPID=$(sh -c 'echo $PPID')
printLogLine(){
printf "\n %s \n" "$logTime $(hostname -s) $scriptName s[$$]f[$BASHPID] -- $logMessage" 2>&1 | tee -a "$logPath"
logger -t "$scriptName s[$$]f[$BASHPID]L" -- "$logMessage" # also write to syslog
}
argE=${1:-}
if [ -n "$argE" ]
then
logMessage="$argE"
printLogLine
else
while read -r logMessage; do printLogLine; done
fi
}
touchPath "$logPath" # check/create log path, touch logfile
EchoLog "##### STARTING $scriptName - $startTime #####"
if [ $maj -gt 10 ]; then
echo
EchoLog "Check OS string format & OS X systemsetup utility for script compatibility with OS X versions higher than 10.x.x"
echo
exit
fi
if [ "$timeZone" != "" ]; then
if [ $ref -lt 5 ]; then
echo
EchoLog "Setting $timeZone for OS X $OS..."
echo
systemsetup -settimezone "$timeZone"
echo
/usr/bin/killall SystemUIServer
EchoLog "Refreshing the clock in the Menu Bar..."
echo
else
echo
EchoLog "Setting $timeZone for OS X $OS..."
echo
systemsetup -settimezone "$timeZone"
echo
/usr/bin/killall SystemUIServer
EchoLog "Refreshing the clock in the Menu Bar..."
echo
fi
else
echo
EchoLog "Error: The timeZone variable is not populated. Please specify a valid time zone. A list "
EchoLog "of accepted time zone values can be generated using the following command in Terminal: "
echo
EchoLog " sudo systemsetup -listtimezones"
echo
exit
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment