Creating script to map drive

Add the default sh script

  #!/bin/sh

My script requirements is mapping a network drive after vpn connects so I have added a 5 second delay to wait for it to come up.

  sleep 5

Get current logged in user

  loggedInUser=$(/usr/bin/who | awk '/console/{ print $1 }')

Set the Azure directory name, azname.file,.core.windows.net

  AzName="azname"

Set the Azure password for the token found in the Azure share mac screen.

  AzPwd="ReallyLongStringEndingWith=="

Set the Azure folder inside the azname.file,.core.windows.net

  AzFolder="foldername"

Using the above variable we have created, we can set the full smb path as below

  AzShare="//$AzName:$AzPwd@$AzName.file.core.windows.net/$AzFolder/"

Make a directory for the mac to mount the network share

  mkdir /Users/$loggedInUser/$AzName

Mount command, using the above variables.

  mount_smbfs -d 777 -f 777 smb:$AzShare /Users/$loggedInUser/$AzName

Full script

  #!/bin/sh
  sleep 5

  loggedInUser=$(/usr/bin/who | awk '/console/{ print $1 }')
  AzName="azname" 
  AzPwd="ReallyLongStringEndingWith=="
  AzFolder="foldername"

  AzShare="//$AzName:$AzPwd@$AzName.file.core.windows.net/$AzFolder/"

  mkdir /Users/$loggedInUser/$AzName

  mount_smbfs -d 777 -f 777 smb:$AzShare /Users/$loggedInUser/$AzName

Save the script to /Library/Scripts/CompanyName/MapDrive.sh

Create the launch agent

Create a new plist file. The custom parts of this plist are.

  1. Label needs to be a unique string to register the plist against on the mac. This is being called com.companyname.mapdrive.

    <key>Label</key>
    <string>com.companyname.mapdrive</string>
    
  2. Creating the script that needs to run. The first line runs the sh command, the second is the path to the script

    <key>ProgramArguments</key>
    <array>
         <string>/bin/sh</string>
         <string>/Library/Scripts/CompanyName/MapDrive.sh</string>
    </array>
    
  3. Watch for the VPN connection, to run. When the VPN connects, the /Library/Preferences/SystemConfiguration folder gets updated.

    <key>WatchPaths</key>
    <array>
    <string>/Library/Preferences/SystemConfiguration</string>
    </array>
    
  4. Run on load. This starts when someone logs in.

    <key>RunAtLoad</key>
    <true/>    
    

Full script

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
  <dict>
       <key>Label</key>
       <string>com.companyname.mapdrive</string>
       <key>ProgramArguments</key>
       <array>
          <string>/bin/sh</string>
          <string>/Library/Scripts/CompanyName/MapDrive.sh</string>
       </array>
       <key>WatchPaths</key>
       <array>
       <string>/Library/Preferences/SystemConfiguration</string>
       </array>
       <key>RunAtLoad</key>
       <true/>
   </dict>
   </plist>
  1. Save the file as /Library/LaunchAgents/com.companyname.mapdrive.plist

Deploying with JAMF

We use JAMF to deploy to our Macs

Open composer, drag the /Library/LaunchAgents/com.companyname.mapdrive.plist and the /Library/Scripts/CompanyName/MapDrive.sh into the window.

Create a postflight script. Amend the ploc to the plist location, and the sloc to the script location

  #!/bin/sh
  ploc="/Library/LaunchAgents/com.companyname.mapdrive.plist"
  sloc="/Library/Scripts/CompanyName/MapDrive.sh"
  chown root $ploc
  chmod 755 $ploc
  chmod 755 $sloc

Package script as non-flat pkg. Move it into JAMF Admin and save.

Login to JAMF, create a policy, with the package as your created package, and set it to install at recurring check-in and once per computer. Deploy to a test Mac. It will need a restart once deployed.