Mount AD Folders

These AppleScripts are all roughly the same, two pairs of scrips slightly modified to read different AD fields froma user record;

The first pair is for mounting and unmounting an AD user's home folder; This is acheived by prompting the user for their AD username and password, and then looking up the homeDirectory field in their AD user record. The requirement of this is that the machine on which these scripts are ran is that they must be bound to the domain in question. I use them for domain bound machines that also have local logons allowing a local user to still accessa folder on the server should they need it.

Mount home folder;

-- This script mounts a users home folder by grabbing the path from the
-- field in their AD user record (this is not secure)

-- Get their AD username
set dialog_1 to display dialog "Please enter your username" default answer ""
set the user_name to the text returned of dialog_1

-- Get their AD password
set dialog_2 to display dialog "Please enter your password" default answer "" with hidden answer
set the pass_word to the text returned of dialog_2

-- All lower case, domain name, not including tld part
property domain_name : "localaddomain" # Change to your domain name (NOT FQDN, just domain name all lower case)

-- By having this here we can include it via 'quoted form of' to escape the with the double-escaped-backslashes ;)
set awkParams to "{path = substr($NF, 4); gsub(/\\\\/, \"/\",path); printf path}"
-- By using 'set uncPath to(do shell script...)' we get the username and escape the double back slashing with the space in the dscl command and include our 'quoted form of'
set uncPath to (do shell script "/usr/bin/dscl localhost read /Active\\ Directory/All\\ Domains/Users/" & user_name & " homeDirectory | awk -F: " & (quoted form of awkParams))

-- Build the end command to run
set command to "smb://" & domain_name & ";" & user_name & ":" & pass_word & "@" & uncPath

-- Now begin a tell with Finder to try and mount the share
tell application "Finder"
	try
		mount volume command -- Give it a whirl earl!
		display dialog "Your server folder has been mounted! Hurray for technology"
	on error
		display dialog "There was an error mounting the Volume." & return & return & "The server may be unavailable at this time or you entered an incorrect username or password" & return & return & "Please inform somebody who has had wheatabix for breakfast if the problem continues." buttons {"Sorry sir"} default button 1
	end try
end tell

Unmount home folder;

-- This script unmounts a users home folder by prompting the user for
-- their AD username to know which volume needs to be unmounted

-- Get their AD username
set dialog_1 to display dialog "Please enter your username so I can dismount the corresponding volume" default answer ""
set the user_name to the text returned of dialog_1

-- Now begin a tell with Finder to try and unmount the share
tell application "Finder"
	try
		-- Set the disk property
		set remoteDisk to disk user_name
		-- Give her the boot
		eject user_name
	on error
		display dialog "Oh dear, I couldn't find a volume of that name (" & user_name & ") to unmount!" & return & return & "Have you had enough vitamin c today?" buttons {"No"} default button 1
	end try
end tell

The second pair of scrips is a slight variation on the first pair which mounts and unmounts a group folder. We use a spare field in AD, ipPhone to store the name of an extra group folder. With these scripts, the user is again prompted for their AD username and password, and then the ipPhone field is queried and the full unc path is built to the group share and finally mounted.

Mount group folder;

-- This script mounts a users group folder by grabbing the name of the group folder from a spare
-- field in their AD user record (this is not secure)

-- Get their AD username
set dialog_1 to display dialog "Please enter your username" default answer ""
set the user_name to the text returned of dialog_1

-- Get their AD password
set dialog_2 to display dialog "Please enter your password" default answer "" with hidden answer
set the pass_word to the text returned of dialog_2

-- Location of group shares
property share_root : "server/Group Shares/"
-- All lower case, domain name, not including tld part
property domain_name : "localaddomain"

-- By having this here we can include it via 'quoted form of' to escape the with the double-escaped-backslashes ;)
set awkParams to "{path = substr($NF, 2); gsub(/\\\\/, \"/\",path); print path}"
-- By using 'set uncPath to(do shell script...)' we get the username and escape the double back slashing with the space in the dscl command and include our 'quoted form of'
set uncPath to (do shell script "/usr/bin/dscl localhost read /Active\\ Directory/All\\ Domains/Users/" & user_name & " ipPhone | awk -F: " & (quoted form of awkParams))

-- Build the end command to run
set command to "smb://" & domain_name & ";" & user_name & ":" & pass_word & "@" & share_root & uncPath

-- Now begin a tell with Finder to try and mount the share
tell application "Finder"
	try
		mount volume command -- Give it a whirl earl!
		display dialog "Your server folder has been mounted! Hurray for technology" -- I feel hurray for boobies would be a better title but hey, I don't make the rules
	on error
		display dialog "There was an error mounting the Volume." & return & return & "The server may be unavailable at this time or you entered an incorrect username or password" & return & return & "Please inform somebody who has had wheatabix for breakfast if the problem continues." buttons {"Sorry sir"} default button 1
	end try
end tell

Unmount group folder

-- This script unmounts a users group folder by grabbing the path from the
-- field in their AD user record to know which volume needs to be unmounted
-- (this is not secure)

-- Get their AD username
set dialog_1 to display dialog "Please enter your username so I can dismount the corresponding volume" default answer ""
set the user_name to the text returned of dialog_1

-- We need to get the name of this users group folder by looking it up in AD

-- By having this here we can include it via 'quoted form of' to escape the with the double-escaped-backslashes ;)
set awkParams to "{path = substr($NF, 2); gsub(/\\\\/, \"/\",path); print path}"
-- By using 'set uncPath to(do shell script...)' we get the username and escape the double back slashing with the space in the dscl command and include our 'quoted form of'
set uncPath to (do shell script "/usr/bin/dscl localhost read /Active\\ Directory/All\\ Domains/Users/" & user_name & " ipPhone | awk -F: " & (quoted form of awkParams))

-- Now begin a tell with Finder to try and unmount the share
tell application "Finder"
	try
		-- Set the disk property
		set remoteDisk to disk uncPath
		-- Give her the boot
		eject uncPath
	on error
		display dialog "Oh dear, I couldn't find a volume of that name (" & uncPath & ") to unmount!" & return & return & "Have you had enough vitamin c today?" buttons {"No"} default button 1
	end try
end tell

Ultimately these scripts could be merged into one prompting you for a username, password and then asking if you want to mount your home or group folder. Also, you could have one script that prompts for a users details and check if their home folder is mounted, if its not then mount it and if it is then unmount it making things a bit more efficient perhaps?