Connect to Server / Mount Share Script

bigmacattack
New Contributor II

I was looking for a way to mount our HOME share without any warnings. So through my research I began to write my own Apple Script. I wanted to share this with everyone, as I had success with it so far. It is mainly to be used on computers not connected to AD, or laptops connected to AD with a mobile user account that do not have the UNC option selected (I found this option to be troublesome, as the share would not remap automatically when the user disconnected from the network and reconnected, https://jamfnation.jamfsoftware.com/discussion.html?id=10643). Eventually I would like to use something like Sleep Watcher to run this script. Right now I exported this script from Script Editor as an application and dropped it into composer from the Desktop, creating a .dmg. I enabled FUT and FEU so it would show up on every users desktop. My policy is simple, run mountHOMEshare.dmg and then execute command "defaults write /Library/Preferences/com.apple.finder ShowMountedServersOnDesktop -bool true". This forces the Finder Preferences to show Connected Servers on the desktop. When the user runs the application, it checks to see if it is on our network (WiFi, Hardwired directly or by a Thunderbolt adapter). If it isn't, it will give the user a simple warning. The warning may be removed when I decide to incorporate it into something like Sleep Watcher. I am currently deploying this policy through Self Service. Enjoy! Please feel free to share how you modified it to make it better, always room for improvement!

--Mount Share--
--Made by Ian Lau--
--Modified 9/21/2015--

--Variables
property shareAddr : "smb://yourServer/yourShare"

--Eject yourShare if it is already mounted, to protect from mounting a duplicate--
tell application "Finder"
    try
        eject disk "yourShare"
    end try
end tell

--Set SSID Variable to current network
--Set ethernet or thunderbolt IP instead
try
    set SSID to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'"
    set ethernetDirectIP to do shell script "/usr/sbin/ipconfig getifaddr en0 2>&1 &"
    set thunderboltIP to do shell script "/usr/sbin/ipconfig getifaddr en4 2>&1 &"
end try

--Check to see if you are on your company's Wifi network(s) or hardwired directly or through thunderbolt adapter
if SSID contains {"yourWifiNetworkName1"} then
    tell application "Finder"
        mount volume shareAddr
    end tell
else if SSID contains {"yourWifiNetworkName2"} then
    tell application "Finder"
        mount volume shareAddr
    end tell
else if ethernetDirectIP begins with "172." then
    tell application "Finder"
        mount volume shareAddr
    end tell
else if thunderboltIP begins with "172." then
    tell application "Finder"
        mount volume shareAddr
    end tell
else
    display dialog "You are not connected to your company's network" with icon caution
end if
1 REPLY 1

davidacland
Honored Contributor II
Honored Contributor II

Totally agree about the "use UNC path" option. It's troublesome at best.

I take it you are just using a single share for all users? We often read a value in the users record in AD to ensure we're getting the SMBHome attribute (Home mounting example script here). That being said, you want this to work when they are off the domain so you'd either need to read something local to the Mac, user s variable like $USER, or use a static value.

On the eject disk bit, I'd probably adjust it to exit the script as there shouldn't be anything else to do at that point.