Posted on 10-16-2019 01:38 PM
My deployment method involves a policy triggered by enrollment. That policy runs a script that names my computers based on a CSV stored online, and at the end of the script, calls the next policy that begins installing software.
Recently, the call to the second policy has failed completely. The first script runs fine still, but it will never call the second policy. I have been racking my brain over this for two weeks and I cannot figure out why it no longer works.
I have not edited the scripts at all, though we did upgrade our server version recently.
Below is the script that runs upon enrollment. The only really important part is the if statement at the end, which does come back positive (confirmed by the echo in the logs) but still does not run the final command before the exit 0.
Please help!
#!/bin/bash
# Rename Computer from Index File
# Written by KClose.
# Created 02/20/2019.
# Updated 02/21/2019 - Added reporting log and ARD Info flag.
# Updated 05/07/2019 - Fixed success/failure testing. additional failure check included. Success triggers next imaging script.
# Updated 05/13/2019 - Added Presorting Flag.
# Updated 10/09/2019 - Changed -trigger to -event and found a missing $.
# Updated 10/15/2019 - Extended the Recon call with the exact path to the jamf command and extended the sleep.
#
# Based on a script by Yonathan Khoe
# Updated to not need any user interaction.
# Index file must be updated prior to script running on new computer.
# Index file can be found at:
# [file reference removed]
#
# There must be a domain user with access to the network resource.
# The login credentials should be added in the options of any policy that uses this script.
#
#########################################################################
### *NECESSARY FUNCTIONS* ###
# Function to convert all special characters in the password.
urlencode() {
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c"
esac
done
}
# Function to report success or failure to logs and ARD Info 4.
ReportLog() {
# Check for Log File location.
if [ ! -d /tmp/JSSLogs/ ]; then
mkdir /tmp/JSSLogs/
fi
# Collect variables for report.
PassFail=$1
ErrorLog=$2
TimeStamp=$(date)
# Report to Install Log.
echo $TimeStamp " - Automated Rename Computer" $PassFail $ErrorLog >> /tmp/JSSLogs/InstallLog.txt
# Report to ARD Info and submit to JSS.
defaults write /Library/Preferences/com.apple.RemoteDesktop Text4 "Rename$PassFail"
/usr/local/jamf/bin/jamf recon
# Wait for the jamf command to complete.
sleep 30
}
### *MOUNT THE NETWORK SHARE* ###
# Collect all necessary variables for mounting the drive.
# First collect dynamic variables from policy options.
# Fetch the AD Username. If unavailable, exit.
if [ "$4" != "" ]; then
DomainUser=$4
else
echo "No Username Specified. Exiting."
ReportLog "Failure" "No Username"
exit 1
fi
# Fetch the AD User's password. If unavailable, exit.
if [ "$5" != "" ]; then
DomainPass=$5
else
echo "No Password Specified. Exiting."
ReportLog "Failure" "No Password"
exit 1
fi
# Fetch the AD User's Domain. If unavailable, exit.
if [ "$6" != "" ]; then
DomainTree=$6
else
echo "No Domain Specified. Exiting."
ReportLog "Failure" "No Domain"
exit 1
fi
# Collect static variables.
ShareSource="filestore.nas.untsystem.edu/cvad/RESOURCE/MacPrestageImaging/ComputerNames"
ShareDestination="/tmp/resource"
InputFile=$ShareDestination/NameIndex.csv
# Encode the password.
DomainPass_Enc=$(urlencode $DomainPass)
# Check for a share destination. If it does not exist, create it.
if [ ! -d "$ShareDestination" ]; then
mkdir $ShareDestination
fi
# Build the logon credentials.
DomainLogon="${DomainTree};${DomainUser}:${DomainPass_Enc}"
# Build the full mounting command.
mountcommand="mount_smbfs //'${DomainLogon}'@${ShareSource} ${ShareDestination}"
# Run the mounting command.
eval $mountcommand
# Check for successful mount. If mounting failed, exit.
if [ ! -f $InputFile ]; then
echo "Index file not found. Exiting."
ReportLog "Failure" "Unable to find network share"
exit 1
fi
### *GET COMPUTER NAME* ###
# Get the serial number of the computer. This will be found in the systen index file.
SerNumber=$(ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}')
# Clean up the output from ioreg.
temp="${SerNumber%"}" #Remove the leading quote
SerNumber="${temp#"}" #Remove the tailing quote
# Getting the Desired Computer Name based on the Serial Number.
# First read in the row that matches the Serial Number.
DesiredRow=($(grep $SerNumber $InputFile))
# Then get the value in Column 2, which is the desired computer name.
DesiredName=$(awk -F"," '{print $2}' <<< $DesiredRow)
# Check to see that the Desired Name was found and defined.
if [ "$DesiredName" == "" ]; then
echo "Desired Name not Specified. Exiting."
ReportLog "Failure" "Desired Name Not Found"
exit 1
fi
### *SET COMPUTER NAME* ###
# Set the computer name to all applicable instances.
sudo scutil --set ComputerName "$DesiredName"
sudo scutil --set HostName "$DesiredName"
sudo scutil --set LocalHostName "$DesiredName"
defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName -string "$DesiredName"
# Wait for all names to change.
sleep 5
# Set the computer name in the JSS.
jamf setComputerName -name "$DesiredName"
# Wait for the JAMF command to complete.
sleep 5
### *GET JAMF PREORTING FLAG* ###
# Using the row determined during naming, get the Presorting Flag from Column 3.
PresortFlag=$(awk -F"," '{print $3}' <<< $DesiredRow)
# Check to see that the Desired Name was found and defined.
if [ "$PresortFlag" == "" ]; then
echo "Presort Flag not Specified. Exiting."
ReportLog "Failure" "Presort Flag not Found"
exit 1
fi
### *SET PRESORTING FLAG* ###
# Set the presort flag into ARD Info 3 and report to JAMF.
defaults write /Library/Preferences/com.apple.RemoteDesktop Text3 "$PresortFlag"
### *CLEANUP* ###
# Unmount the network share.
umount $ShareDestination
# Delete the sharing destination
rm -r $ShareDestination
### *CHECK THE RESULTS AND REPORT BACK* ###
# Check Host Name against Desired Name. Report success or failure to log and ARD Info 4.
if [[ "$(scutil --get ComputerName)" == "$DesiredName" ]]; then
# If everything was successful, report the success and move on to the next script.
echo "Name Change script completed, moving on to the next imaging script"
ReportLog "Success" ""
/usr/local/jamf/bin/jamf policy -event DEP.Imaging01 &
exit 0
else
# Else, report the failure and end.
echo "Failure: Computer name does not match."
ReportLog "Failure" "Computer name does not match"
exit 1
fi