Script: Set Mac OS X Server Serial Number

Continuing with the concept of automated server deployment, here is a small script that changes the serial number in a Mac OS X Sever installation.

A server system that has been deployed via disk imaging or automated deployment systems will boot, but to use the server features, a valid and unique serial number must be entered.  Luckily, Apple provides a command line utility to set the serial number.

The script, as written, is intended for use with The Casper Suite.  Replacing $4 with a static value or using another method of passing a value would be necessary if you are not using The Casper Suite.

#!/bin/bash

##### HEADER BEGINS #####
# scr_sys_setServerSerial.bash
#
# Created 20081231 by Miles A. Leacy IV
# miles.leacy@themacadmin.com
# Modified 20081231 by Miles A. Leacy IV
# Copyright 2008 Miles A. Leacy IV
#
# This script may be copied and distributed freely as long as this header
# remains intact.
#
# This script is provided "as is".  The author offers no warranty or
# guarantee of any kind.
# Use of this script is at your own risk.  The author takes no responsibility
# for loss of use,
# loss of data, loss of job, loss of socks, the onset of armageddon, or any
# other negative effects.
#
# Test thoroughly in a lab environment before use on production systems.
# When you think it's ok, test again.  When you're certain it's ok, test
# twice more.
#
# This script sets a Mac OS X Server installation's serial number to the value
# provided in $4 by Casper.
# Run as an "at reboot" script when imaging with Casper, making sure to type
# double-check the serial in the script parameters before imaging.
#
##### HEADER ENDS #####

# Change serial to $4 from JSS

/System/Library/ServerSetup/serversetup -setServerSerialNumber "$4"

exit 0

 

Timed Automatic Shutdown With Notification and Client Opt-out

Note: I realize that parts of this script extend beyond the viewable area.  I am considering changes to the site formatting to prevent this in the future, but I have found that if you select the script text in Safari and copy it, the text beyond the viewable area will be captured.

I was recently confronted with a scenario…

  • Client Macs are required to shut down or restart daily at a predetermined time.
  • Clients who are still working at this time should be able to opt out of the shut down.
  • If a client opts out, the event should be cancelled and not recur until the following day.

I solved this problem with a shell script that uses osascript to display the GUI dialog.  The script can be delivered via a policy using The Casper Suite, or by creating a launchd item.

Note that Casper Suite users can declare parameter variables instead of static values if they wish to handle variable assignment through the Casper Suite.

The script follows…

#!/bin/sh

##### HEADER BEGINS #####
# timedForcedShutdown.sh
#
# Created 20050508 by Miles A. Leacy IV
# miles.leacy@themacadmin.com
# Last modified 20050508 by Miles A. Leacy IV
# Copyright 2009 Miles A. Leacy IV
#
# This script may be copied and distributed freely
# as long as this header remains intact.
#
# This script is provided "as is".  The author offers no warranty
# or guarantee of any kind.
# Use of this script is at your own risk.  The author takes no
# responsibility for loss of use, loss of data, loss of job,
# loss of socks, the onset of armageddon, or any other
# negative effects.
#
# Test thoroughly in a lab environment before use on production systems.
# When you think it's ok, test again.  When you're certain it's ok,
# test twice more.
#
# This script will help to enforce a mandatory reboot or shut down.
#
# If no console user is logged in, the script will execute the command
# stored in the $shutdownAction variable.
#
# If a console user is logged in, a dialog is displayed informing the user
# of the number of minutes until shutdown followed by a configurable
# message stored in $notificationMessage.  The dialog contains two buttons.
#
# Clicking the "Postpone" button will cancel shutdown/reboot.
#
# Clicking the "Shut Down" button will execute the command
# stored in the $shutdownAction variable.
#
###########

###########
# Declare Variables
# Edit this section to change the script parameters
###########

minutesN=30
# Number of minutes to count down before shutdown

shutdownAction="echo The system would shut down now."
# The default echo command above is for testing purposes.
# Change to "shutdown -r now" to reboot
# Change to "shutdown -h now" to shut down

notificationMessage="Please save any files you are working on.nn
Click Shut Down to shut down immediatelyn
Click Postpone to postpone shut down until tomorrow evening."
# This message will appear in the initial dialog box following
# This computer is scheduled to $shutdownPhrase in $minutesN minutes.

shutdownPhrase="Shut Down"
# This variable should contain either "Shut Down" or "Restart"
# depending on the value of $shutdownAction.  This string will appear
# in the dialog and will determine the name of the button that causes
# $shutdownAction to be executed.

postponeAlert="Automatic shutdown has been postponed until tomorrow."

###########
# Script Body
# Do not edit below this line
###########

# If no user is logged in at the console, shut down immediately
consoleUser=`/usr/bin/w | grep console | awk '{print $1}'`
if test "$consoleUser"  == ""; then
$shutdownAction
fi

function timedShutdown {
button=`/usr/bin/osascript << EOT
tell application "System Events"
	activate
	set shutdowndate to (current date) + "$minutesN" * minutes
	repeat
		set todaydate to current date
		set todayday to day of todaydate
		set todaytime to time of todaydate
		set todayyear to year of todaydate
		set shutdownday to day of shutdowndate
		set shutdownTime to time of shutdowndate
		set shutdownyear to year of shutdowndate
		set yearsleft to shutdownyear - todayyear
		set daysleft to shutdownday - todayday
		set timeleft to shutdownTime - todaytime
		set totaltimeleft to timeleft + {86400 * daysleft}
		set totaltotaltimeleft to totaltimeleft + {yearsleft * 31536000}
		set unroundedminutesleft to totaltotaltimeleft / 60
		set totalminutesleft to {round unroundedminutesleft}
		if totalminutesleft is less than 2 then
			set timeUnit to "minute"
		else
			set timeUnit to "minutes"
		end if
		if totaltotaltimeleft is less than or equal to 0 then
			exit repeat
		else
			display dialog "This computer is scheduled to " & "$shutdownPhrase" & " in " & totalminutesleft & " " & timeUnit & ". " & "$notificationMessage" & " " giving up after 60 buttons {"Postpone", "$shutdownPhrase"} default button "$shutdownPhrase"
			set choice to button returned of result
			if choice is not "" then
				exit repeat
			end if
		end if
	end repeat

end tell
return choice
EOT`
if test "$button" == "Postpone"; then
	`osascript << EOT
          tell application "System Events"
	  activate
	  display alert "$postponeAlert" as warning buttons "I understand" default button "I understand"
          end tell`
        else
        $shutdownAction
	exit 0
fi
}

timedShutdown