- 05 Sep 2024
- 5 Minutes to read
- Print
- PDF
Install DMG Files using Shell Script
- Updated on 05 Sep 2024
- 5 Minutes to read
- Print
- PDF
Scalefusion offers support to distribute Apple VPP apps and PKG (product archive packages) natively using the Dashboard. Most Enterprise apps use either of the above mechanisms for distribution in an enterprise scenario. However, there might be certain cases where the developer has made the installer available in the form of a DMG (Disk Image File).
In such cases where only a DMG file is available, IT Admins can use the script described below and deploy it via Scalefusion MDM agent to remotely install the DMG. Please follow the steps below to use this method.
- This method uses a script to install the DMG file; though Scalefusion has tested these scripts, please validate the scripts on a test machine before deploying them on all your managed devices.
- Scalefusion will not be responsible for any loss of data or system malfunction that may arise due to the usage of these scripts
Prerequisites
- Deploy Scalefusion MDM Agent.
- Scalefusion subscription plan with access to Content ManagementDMG files that wrap a PKG file that, when extracted, contains a PKG file cannot be distributed by this mechanism. We suggest to use the PKG file directly in such cases.
Step 1: Generate a Download URL for the DMG file
The first step is to generate a download URL for the DMG file that needs to be installed. You can use any of your servers (if you have one already) or a storage provider like Amazon S3 or Cloudflare R2 to upload your DMG and generate a public download URL.
In case you don't have a storage provider that you use, you can also use Scalefusion's Content Management to upload your DMG files and obtain a DMG URL. Follow the steps below to upload the file to Scalefusion's content management and generate the download URL,
- Navigate to Content Management > Content and click Upload File to upload the DMG.
- Wait for the DMG file to be uploaded and to appear in the list of files. Once the file appears, click on the file to view the file details window.
- Click on the 3 dots, and from the menu, select Copy Download Link
- Now you can open TextEdit (or any text editor) on your Mac/PC and paste the copied URL to your local machine. The URL would look like below,URL
https://mobilock.s3.amazonaws.com/uploads/mobile_file/content/5947/rustdesk-1.1.9.dmg
Step 2: Create the Script
- Create a file on your Mac, for example, download_install_dmg.sh and open it to a text editor of your choice like TextEdit
- Copy the contents below.Shell
#!/bin/bash # Insert the DMG download url below DownloadUrl="DMG_download_url" ##### DONOT EDIT BELOW CODE ##### regex='^https.*.dmg$' if [[ $DownloadUrl =~ $regex ]]; then echo "URL points to direct DMG download" validLink="True" else echo "Searching headers for download links" urlHead=$(curl -s --head "$DownloadUrl") locationSearch=$(echo "$urlHead" | grep https:) if [ -n "$locationSearch" ]; then locationRaw=$(echo "$locationSearch" | awk '{print $2}') locationFormatted="$(echo "${locationRaw}" | tr -d '[:space:]')" regex='^https.*' if [[ $locationFormatted =~ $regex ]]; then echo "Download link found" DownloadUrl="$locationFormatted" else echo "No https location download link found in headers" exit 1 fi else echo "No location download link found in headers" exit 1 fi fi # Create Temp Folder DATE=$(date '+%Y-%m-%d-%H-%M-%S') TempFolder="Download-$DATE" mkdir -p "/tmp/$TempFolder" # Navigate to Temp Folder cd "/tmp/$TempFolder" || exit # Download File into Temp Folder curl -s -O "$DownloadUrl" # Capture name of Download File DownloadFile="$(ls)" echo "Downloaded $DownloadFile to /tmp/$TempFolder" # Verify DMG File regex='\.dmg$' if [[ $DownloadFile =~ $regex ]]; then DMGFile="$DownloadFile" echo "DMG File Found: $DMGFile" else echo "File: $DownloadFile is not a DMG" rm -r "/tmp/$TempFolder" echo "Deleted /tmp/$TempFolder" exit 1 fi # Mount DMG File (-nobrowse prevents the volume from popping up in Finder) hdiutilAttach=$(hdiutil attach "/tmp/$TempFolder/$DMGFile" -nobrowse) echo "Used hdiutil to mount $DMGFile" err=$? if [ ${err} -ne 0 ]; then echo "Could not mount $DMGFile Error: ${err}" rm -r "/tmp/$TempFolder" echo "Deleted /tmp/$TempFolder" exit 1 fi regex='\/Volumes\/.*' if [[ $hdiutilAttach =~ $regex ]]; then DMGVolume="${BASH_REMATCH[0]}" echo "Located DMG Volume: $DMGVolume" else echo "DMG Volume not found" rm -r "/tmp/$TempFolder" echo "Deleted /tmp/$TempFolder" exit 1 fi # Identify the mount point for the DMG file DMGMountPoint=$(hdiutil info | grep "$DMGVolume" | awk '{ print $1 }') echo "Located DMG Mount Point: $DMGMountPoint" # Capture name of App file cd "$DMGVolume" || exit AppName=$(ls | grep '.app') cd ~ || exit echo "Located App: $AppName" # Test to ensure App is not already installed ExistingSearch=$(find "/Applications/" -name "$AppName" -depth 1) if [ -n "$ExistingSearch" ]; then echo "$AppName already present in /Applications folder" hdiutil detach "$DMGMountPoint" echo "Used hdiutil to detach $DMGFile from $DMGMountPoint" rm -r "/tmp/$TempFolder" echo "Deleted /tmp/$TempFolder" exit 1 else echo "$AppName not present in /Applications folder" fi DMGAppPath=$(find "$DMGVolume" -name "*.app" -depth 1) # Copy the contents of the DMG file to /Applications/ # Preserves all file attributes and ACLs cp -pPR "$DMGAppPath" /Applications/ err=$? if [ ${err} -ne 0 ]; then echo "Could not copy $DMGAppPath Error: ${err}" hdiutil detach "$DMGMountPoint" echo "Used hdiutil to detach $DMGFile from $DMGMountPoint" rm -r "/tmp/$TempFolder" echo "Deleted /tmp/$TempFolder" exit 1 fi echo "Copied $DMGAppPath to /Applications" # Unmount the DMG file hdiutil detach "$DMGMountPoint" echo "Used hdiutil to detach $DMGFile from $DMGMountPoint" err=$? if [ ${err} -ne 0 ]; then echo "Could not detach DMG: $DMGMountPoint Error: ${err}" fi # Remove Temp Folder and download rm -r "/tmp/$TempFolder" echo "Deleted /tmp/$TempFolder"
- Now replace the phrase "DMG_download_url" with the download URL generated in Step 1.Shell
# Insert the DMG download url below DownloadUrl="DMG_download_url"
- Save the file on your Mac.
Step 3: Deploy the Script
Please use our help document on deploying scripts on macOS devices to upload and publish the script to your managed Mac devices. For easy reference, here are the steps,
- Navigate to Application Management > Enterprise Store& upload the script as shown below,
- Enter Script Name: Enter a name for the script file
- Upload Script File: Upload the script file created in Step 2
- Run Script as signed-in user: Select Yes from the dropdown
- Click Save
- Now you can Publish the script to the device profiles where you want to install this application.
- Once the script has been deployed, you can View Status- from the side panel that appears once you click on the uploaded script file. Here are sample screenshots,
- Successful Installation
- Invalid Download URL
- Failed to Install DMG File