Skip to content

Instantly share code, notes, and snippets.

@homotechsual
Created January 30, 2025 22:11
Show Gist options
  • Save homotechsual/3de46f9aad0f18211f97ff6de331083c to your computer and use it in GitHub Desktop.
Save homotechsual/3de46f9aad0f18211f97ff6de331083c to your computer and use it in GitHub Desktop.
Install NinjaOne Agent and Rosetta2
#!/bin/bash
# SYNOPSIS
## Software Deployment - MDM - NinjaOne Agent
# DESCRIPTION
## Uses documentation fields to pull client specific Printix information to download that client's installer from Printix and install it on the endpoint.
# NOTES
## 2024-01-10: Initial version
# LINK
## Blog post: https://homotechsual.dev/2024/01/10/Deploy-Printix-NinjaOne/
# Define initial variables
softwareName="NinjaOne Agent"
logMetaDir="/Library/Logs/RMM/$softwareName"
log="$logMetaDir/$softwareName.log"
softwarePKGDownloadURL="<PUT YOUR NINJA AGENT INSTALLER PKG URL HERE>"
## Check if the log directory has been created
if [ -d "$logMetaDir" ]; then
## Already created
echo "$(date) | Log directory already exists at: $logMetaDir"
else
## Create the log directory
echo "$(date) | Creating log directory at: $logMetaDir"
mkdir -p "$logMetaDir"
fi
# function to check if softwareupdate is running to prevent us from installing Rosetta at the same time as another script
isSoftwareUpdateRunning() {
while /usr/bin/pgrep -f "/usr/sbin/softwareupdate" >/dev/null; do
echo "$(date) | [/usr/sbin/softwareupdate] running, waiting..."
sleep 60
done
echo "$(date) | [/usr/sbin/softwareupdate] isn't running, lets carry on"
}
# function to delay script if the specified process is running
waitForProcess() {
processName=$1
fixedDelay=$2
terminate=$3
echo "$(date) | Waiting for other [$processName] processes to end"
while /usr/bin/pgrep -f "$processName" >/dev/null; do
if [[ $terminate == "true" ]]; then
echo "$(date) | + [$processName] running, terminating [$processName]..."
pkill -f "$processName"
return
fi
# If we've been passed a delay we should use it, otherwise we'll create a random delay each run
if [[ ! $fixedDelay ]]; then
delay=$((RANDOM % 50 + 10))
else
delay=$fixedDelay
fi
echo "$(date) | + Another instance of $processName is running, waiting [$delay] seconds"
sleep "$delay"
done
echo "$(date) | No instances of [$processName] found, safe to proceed"
}
# function to check if we need Rosetta 2
checkForRosetta2() {
echo "$(date) | Checking if we need Rosetta 2 or not"
# if Software update is already running, we need to wait...
waitForProcess "/usr/sbin/softwareupdate"
## Note, Rosetta detection code from https://derflounder.wordpress.com/2020/11/17/installing-rosetta-2-on-apple-silicon-macs/
OLDIFS=$IFS
IFS='.' read -r osvers_major osvers_minor osvers_dot_version <<<"$(/usr/bin/sw_vers -productVersion)"
IFS=$OLDIFS
if [[ ${osvers_major} -ge 11 ]]; then
# Check to see if the Mac needs Rosetta installed by testing the processor
processor=$(/usr/sbin/sysctl -n machdep.cpu.brand_string | grep -o "Intel")
if [[ -n "$processor" ]]; then
echo "$(date) | $processor processor installed. No need to install Rosetta."
else
# Check for Rosetta "oahd" process. If not found,
# perform a non-interactive install of Rosetta.
if /usr/bin/pgrep oahd >/dev/null 2>&1; then
echo "$(date) | Rosetta is already installed and running. Nothing to do."
else
if /usr/sbin/softwareupdate --install-rosetta --agree-to-license; then
echo "$(date) | Rosetta has been successfully installed."
else
echo "$(date) | Rosetta installation failed!"
exit 1
fi
fi
fi
else
echo "$(date) | Mac is running macOS $osvers_major.$osvers_minor.$osvers_dot_version."
echo "$(date) | No need to install Rosetta on this version of macOS."
fi
}
# start logging
exec &> >(tee -a "$log")
# Begin Script Body
echo ""
echo "##############################################################"
echo "# $(date) | Starting install of $softwareName"
echo "############################################################"
echo ""
# If another softwareupdate process is running, we should wait
isSoftwareUpdateRunning
# Let's check to see if we need Rosetta 2
checkForRosetta2
curl -o "/Users/Shared/$softwareName.pkg" "$softwarePKGDownloadURL"
sudo installer -pkg "/Users/Shared/$softwareName.pkg" -target /
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment