Skip to main content
Question

Mount and Unmount SMB script

  • November 12, 2025
  • 1 reply
  • 36 views

Kel11
Forum|alt.badge.img

Hi Everyone,

Need your help if you have a script that can mount and unmount SMB the can be use as repository of screenshots

Thank you.

1 reply

h1431532403240
Forum|alt.badge.img+6

Hi Kel11,

Happy to help with SMB mount/unmount scripts! Below are scripts you can use with Jamf Pro to mount SMB shares for storing screenshots or any other files.

Mount SMB Share Script

This script can be deployed via Jamf Pro policy (at login, via Self Service, or triggered by custom event):

#!/bin/bash
#####################################
# Mount SMB Share Script for Jamf Pro
# Use with policy parameters or modify variables directly
#####################################

# Variables - modify these or use Jamf Pro script parameters
protocol="smb"
serverName="${4:-your.server.com}" # Parameter 4 in Jamf Pro
shareName="${5:-Screenshots}" # Parameter 5 in Jamf Pro

# Get current logged-in user
currentUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
uid=$(id -u "${currentUser}")

# Build mount path
mountPath="${protocol}://${serverName}/${shareName}"

# Function to run commands as logged-in user
runAsUser() {
/usr/bin/launchctl asuser "${uid}" sudo -u "${currentUser}" "$@"
}

# Allow connections to unknown servers (prevents authentication prompts on first connect)
defaults write /Library/Preferences/com.apple.NetworkAuthorization AllowUnknownServers -bool YES

# Check if already mounted, if not, mount the share
runAsUser osascript <<EOT
on listMountedDisks()
list disks
end listMountedDisks

if (listMountedDisks() does not contain "${shareName}") then
try
mount volume "${mountPath}"
on error errMsg
display dialog "Failed to mount ${shareName}: " & errMsg buttons {"OK"} default button "OK"
end try
end if
EOT

# Verify mount was successful
if mount | grep -q "/Volumes/${shareName}"; then
echo "Successfully mounted ${shareName}"
exit 0
else
echo "Failed to mount ${shareName}"
exit 1
fi

Unmount SMB Share Script

#!/bin/bash
#####################################
# Unmount SMB Share Script for Jamf Pro
#####################################

# Variables
shareName="${4:-Screenshots}" # Parameter 4 in Jamf Pro

# Get current logged-in user
currentUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
uid=$(id -u "${currentUser}")

# Function to run commands as logged-in user
runAsUser() {
/usr/bin/launchctl asuser "${uid}" sudo -u "${currentUser}" "$@"
}

# Check if share is mounted
if mount | grep -q "/Volumes/${shareName}"; then
# Unmount using diskutil (cleaner method)
/usr/sbin/diskutil unmount "/Volumes/${shareName}"

# Alternative: Force unmount if needed
# /usr/sbin/diskutil unmount force "/Volumes/${shareName}"

if [ $? -eq 0 ]; then
echo "Successfully unmounted ${shareName}"
exit 0
else
echo "Failed to unmount ${shareName}"
exit 1
fi
else
echo "${shareName} is not currently mounted"
exit 0
fi

Combined Mount/Unmount Script with Toggle

If you want a single script that can do both:

#!/bin/bash
#####################################
# Mount/Unmount SMB Share Toggle Script
# Parameter 4: action (mount/unmount)
# Parameter 5: server name
# Parameter 6: share name
#####################################

action="${4:-mount}"
serverName="${5:-your.server.com}"
shareName="${6:-Screenshots}"
protocol="smb"

currentUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')
uid=$(id -u "${currentUser}")
mountPath="${protocol}://${serverName}/${shareName}"

runAsUser() {
/usr/bin/launchctl asuser "${uid}" sudo -u "${currentUser}" "$@"
}

case "${action}" in
mount)
defaults write /Library/Preferences/com.apple.NetworkAuthorization AllowUnknownServers -bool YES

if mount | grep -q "/Volumes/${shareName}"; then
echo "${shareName} is already mounted"
exit 0
fi

runAsUser osascript -e "mount volume \"${mountPath}\""

sleep 2
if mount | grep -q "/Volumes/${shareName}"; then
echo "Successfully mounted ${shareName}"
exit 0
else
echo "Failed to mount ${shareName}"
exit 1
fi
;;
unmount)
if mount | grep -q "/Volumes/${shareName}"; then
/usr/sbin/diskutil unmount "/Volumes/${shareName}"
echo "Successfully unmounted ${shareName}"
exit 0
else
echo "${shareName} is not mounted"
exit 0
fi
;;
*)
echo "Invalid action. Use 'mount' or 'unmount'"
exit 1
;;
esac

Deployment Tips:

  1. In Jamf Pro, go to Settings > Computer Management > Scripts and add your script
  2. Create a Policy and attach the script
  3. Set parameters:
    • Parameter 4: Server name (e.g., fileserver.company.com)
    • Parameter 5: Share name (e.g., Screenshots)
  4. For screenshot repository usage, trigger the mount policy at Login or via Self Service

Authentication Options:

  • Kerberos (AD-bound Macs): The script will automatically use the user's Kerberos ticket if the Mac is bound to AD
  • Keychain: If the user connects manually once and saves credentials to Keychain, subsequent mounts will be automatic
  • Hardcoded credentials (not recommended for security): You can use mount_smbfs //username:password@server/share /Volumes/ShareName

For macOS Screenshot Settings:

To automatically save screenshots to the mounted SMB share, you can add this to your script after mounting:

# Set screenshot location to the mounted share
runAsUser defaults write com.apple.screencapture location "/Volumes/${shareName}"
runAsUser killall SystemUIServer

Let me know if you need any modifications or have questions about the implementation!