Skip to content

Instantly share code, notes, and snippets.

@neilmartin83
Last active April 23, 2023 06:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neilmartin83/00fb2d045272219736d29e244b717527 to your computer and use it in GitHub Desktop.
Save neilmartin83/00fb2d045272219736d29e244b717527 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Add a Microsoft Remote Desktop bookmark entry for the specified computer.
#
# Neil Martin - https://soundmacguy.wordpress.com - @neilmartin83
#
############################
#
# Use Parameter 4 in Jamf to specify the hostname or IP address of the bookmark you wish to add e.g. myawesomepc.com (required)
#
# Use Parameter 5 in Jamf to specify the friendly name of the bookmark you wish to add, without quotes e.g. My Awesome PC (optional)
#
# Use Parameter 6 in Jamf to specify the resolution, without quotes e.g: 1024 768 (optional)
#
# Use Parameter 7 in Jamf to specify the group, if needed, without quotes e.g. Work PCs (optional)
#
# Use Parameter 8 in Jamf to specify the username, if needed, without quotes. Enter loggedInUser to use the current user's username! (optional)
#
# Use Parameter 9 in Jamf to specify the domain, if needed, without quotes e.g. myorg.com (optional)
#
# Use Parameter 10 in Jamf to specify other arguments as needed. Avoid specifying parameters that use quotes. (optional)
# For more information on available arguments, run:
# "/Applications/Microsoft Remote Desktop.app/Contents/MacOS/Microsoft Remote Desktop" --script bookmark add help
#
# Set Parameter 11 in Jamf to YES if you want to REPLACE all existing bookmarks that have the same hostname, if present. (optional)
# If this is not set, an additional bookmark for this hostname will be created if it already exists.
#
############################
hostname="${4}"
friendlyname="${5}"
resolution="${6}"
group="${7}"
username="${8}"
domain="${9}"
extraArgs=${10}
replace="${11}"
loggedInUser=$( echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name :/ && ! /loginwindow/ { print $3 }' )
msrd="/Applications/Microsoft Remote Desktop.app/Contents/MacOS/Microsoft Remote Desktop"
if [ -z "${loggedInUser}" ]; then
/bin/echo "Nobody logged in, exiting..."
exit 1
else
/bin/echo "User: ${loggedInUser} is logged in..."
fi
if [ ! -f "${msrd}" ]; then
/bin/echo "Microsoft Remote Desktop not installed, exiting..."
exit 1
fi
if [ -z "${hostname}" ]; then
/bin/echo Hostname not specified, exiting...
exit 1
fi
if [ -z "${friendlyname}" ]; then
/bin/echo "Friendly name not specified, using ${hostname}"
friendlyname="${hostname}"
fi
if [ "${replace}" == "YES" ]; then
/bin/echo "Existing bookmarks for ${friendlyname} will be removed!"
entrylist=($(sudo -u "${loggedInUser}" "${msrd}" --script bookmark list 2>/dev/null | /usr/bin/grep -w "${friendlyname}" | /usr/bin/awk -F '", ' '{print $2}'))
if [ -z "${entrylist}" ]; then
/bin/echo "No existing entries found for ${friendlyname}"
else
for entry in "${entrylist[@]}"; do
/bin/echo "Deleting entry for ${friendlyname} and unique ID ${entry}"
sudo -u "${loggedInUser}" "${msrd}" --script bookmark delete "${entry}" 2>/dev/null
done
fi
fi
if [ -z "${username}" ]; then
/bin/echo "Username not specified, Creating bookmark: ${friendlyname}"
sudo -u "${loggedInUser}" "${msrd}" --script bookmark write $(uuidgen) --hostname "${hostname}" --friendlyname "${friendlyname}" --resolution "${resolution}" --group "${group}" ${extraArgs} 2>/dev/null
else
if [ "${username}" == "loggedInUser" ]; then
/bin/echo "Username specified, creating bookmark using logged in user's username..."
username="${loggedInUser}"
else
/bin/echo "Username specified, Creating bookmark for: ${username}"
fi
if [ -z "${domain}" ]; then
/bin/echo "Domain not specified, username will not be prefixed..."
else
username="${domain}"'\'"${username}"
/bin/echo "Domain specified, Username will be ${username}"
fi
/bin/echo "Creating bookmark: ${friendlyname}"
sudo -u "${loggedInUser}" "${msrd}" --script bookmark write $(uuidgen) --hostname "${hostname}" --friendlyname "${friendlyname}" --resolution "${resolution}" --group "${group}" --username "${username}" ${extraArgs} 2>/dev/null
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment