Posted on 03-07-2025 07:38 AM
Hello everyone,
Now when we have left our old eDirectory and are up en running with our Entra en AD I put together some Apple Scripts for mounting network volumes - home and shared. The scripts work okej but I think it would work even better if you made them as bash script instead. But I'm not good att bash at all. Maybe someone's willing to help me out? One of the scripts I've done look like follows:
set userName to short user name of (system info)
set Share_Path to "smb://" & userName & "@our_server_address/home$/" & userName
tell application "Finder"
mount volume Share_Path
open userName
end tell
Thanks in advance,
Jonas
Solved! Go to Solution.
a month ago
Hey Jonas,
You’re right—using a Bash script for this is a cleaner approach, especially when deploying via Jamf. Below is a Bash script that replicates your AppleScript functionality. It dynamically retrieves the currently logged-in user and mounts the SMB share accordingly:
#!/bin/bash
# Get the currently logged-in user
currentUser=$(stat -f%Su /dev/console)
# Define the SMB share path
serverAddress="our_server_address"
sharePath="smb://$serverAddress/homes/$currentUser"
# Check if the share is already mounted
if mount | grep "$sharePath" > /dev/null; then
echo "Share already mounted."
exit 0
fi
# Create mount point if it doesn't exist
mountPoint="/Volumes/$currentUser"
if [ ! -d "$mountPoint" ]; then
mkdir -p "$mountPoint"
fi
# Mount the SMB share
osascript -e "try" -e "mount volume \"$sharePath\"" -e "end try"
# Verify if the mount was successful
if mount | grep "$serverAddress"; then
echo "Successfully mounted $sharePath"
else
echo "Failed to mount $sharePath"
exit 1
fi
Explanation:
This should work seamlessly in Jamf deployments. Let me know if you need any tweaks!
Posted on 03-07-2025 09:46 AM
This is a bash script I deploy from Jamf to mount DFS shares
#!/bin/sh
# Get current logged in user
curUser="$(stat -f"%Su" /dev/console)"
# Mount the drive
mount_script=`/usr/bin/osascript > /dev/null << EOT
delay 5
tell application "Finder"
mount volume "smb://PATH_TO_SHARE"
end tell
EOT`
echo "Opening Network shared drive..."
# open network drive in Finder
sudo -u $curUser open /Volumes/PATH_TO_SHARE/
exit 0
If your file share requires authentication, then the user will get prompted to login
a month ago
Thanks @junjishimazaki,
Like a mention before, I'm not so good at this with bash. :) I've tried to edit the script you shared a bit to fit our needs but I don't think I get it really right. This is what I'm trying to accomplish.
#!/bin/sh
# Get current logged in user
curUser="$(stat -f"%Su" /dev/console)"
# Mount the drive
mount_script=`/usr/bin/osascript > /dev/null << EOT
delay 5
set userName to short user name of (system info)
set sharePath to "smb://" & userName & "our_server_address/home$/" & userName
tell application "Finder"
mount volume sharePath
end tell
EOT`
echo "Opening Network shared drive..."
# open network drive in Finder
sudo -u $curUser open /Volumes/$curUser/
exit 0
Maybe it's not necessary to declare variable for both bash and apple script? :)
a month ago
Hey Jonas,
You’re right—using a Bash script for this is a cleaner approach, especially when deploying via Jamf. Below is a Bash script that replicates your AppleScript functionality. It dynamically retrieves the currently logged-in user and mounts the SMB share accordingly:
#!/bin/bash
# Get the currently logged-in user
currentUser=$(stat -f%Su /dev/console)
# Define the SMB share path
serverAddress="our_server_address"
sharePath="smb://$serverAddress/homes/$currentUser"
# Check if the share is already mounted
if mount | grep "$sharePath" > /dev/null; then
echo "Share already mounted."
exit 0
fi
# Create mount point if it doesn't exist
mountPoint="/Volumes/$currentUser"
if [ ! -d "$mountPoint" ]; then
mkdir -p "$mountPoint"
fi
# Mount the SMB share
osascript -e "try" -e "mount volume \"$sharePath\"" -e "end try"
# Verify if the mount was successful
if mount | grep "$serverAddress"; then
echo "Successfully mounted $sharePath"
else
echo "Failed to mount $sharePath"
exit 1
fi
Explanation:
This should work seamlessly in Jamf deployments. Let me know if you need any tweaks!
a month ago
Thank you very much @Valcovish,
This script just works and very good as well. Now when I've been testing a lot, it's easy to understand the code as well. 😊👍
a month ago
Glad to hear that, please consider marking the topic as resolved.