Skip to content

Instantly share code, notes, and snippets.

@neilmartin83
Last active June 14, 2020 18:13
Show Gist options
  • Save neilmartin83/a35db3288c1a6e0f24db88df8dcde65c to your computer and use it in GitHub Desktop.
Save neilmartin83/a35db3288c1a6e0f24db88df8dcde65c to your computer and use it in GitHub Desktop.
#!/bin/sh
# Delete all Microsoft Remote Desktop bookmark entries for the specified hostname
#
# Neil Martin - https://soundmacguy.wordpress.com - @neilmartin83
#
############################
#
# Use Parameter 4 in Jamf to specify the friendly name or hostname of the bookmarks you wish to delete
# Friendly name always takes precedence. If the bookmark has no friendly name set, the hostname will be used instead.
#
############################
name="${4}"
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 "${name}" ]; then
/bin/echo Bookmark name not specified, exiting...
exit 1
fi
entrylist=($(sudo -u "${loggedInUser}" "${msrd}" --script bookmark list 2>/dev/null | /usr/bin/grep -w "${name}" | /usr/bin/awk -F '", ' '{print $2}'))
if [ -z "${entrylist}" ]; then
/bin/echo "No bookmarks found for ${name}, exiting..."
exit 0
else
for entry in "${entrylist[@]}"; do
/bin/echo "Deleting entry for ${name} and unique ID: ${entry}"
sudo -u "${loggedInUser}" "${msrd}" --script bookmark delete "${entry}" 2>/dev/null
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment