Friday, 29 March 2013

Lync for mac removal script

Hello all,
   Here's a little script that cleans out Lync for mac from OS X, this has been tested on 10.8 and 10.7 and found to be working very well, this basically automates the steps listed here:

http://mac2.microsoft.com/help/office/14/en-us/lyncdeploy/item/b4b71856-a7a5-467e-8425-238b3eaaa993?category=8cd683fd-c4a6-4520-a360-ac81632dadcb

#!/bin/bash

# See Microsoft Article ID: 2691870 How to perform a clean uninstall of Lync for Mac 2011
ASROOT=${ASROOT:-sudo}
# Variables and Functions #
VISIBLEUSERS=$(sudo /usr/bin/dscl . list /Users UniqueID | awk '$2 >= 500 && $2 < 100000000000000000 { print $1; }')
function LyncRunningCheck {
ps ax | grep -v grep | grep -i "/Applications/Microsoft Lync.app/Contents/MacOS/Microsoft Lync" > /dev/null
result=$?
if [ "${result}" -eq "1" ] ; then
     echo "`date`: Microsoft Lync is not running."
else
     echo "`date`: Killing Microsoft Lync."
 LyncPID=$(ps -A | grep -m1 "Microsoft Lync" | awk '{print $1}')
 sudo kill -9 $LyncPID
fi
}
# Main Process #

# Check if Lync is running, kill if it is:
LyncRunningCheck
# Delete the application:
rm -rf /Applications/Microsoft\ Lync.app/
echo "`date`: Removed /Applications/Microsoft\ Lync.app/"
rm -f /Library/Preferences/MicrosoftLyncRegistrationDB.plist

# Remove Dock Icon:
for username in $VISIBLEUSERS
do
HOMEFOLDER=$(sudo dscl . -read /Users/$username NFSHomeDirectory | awk '{print $2}')
DOCKSLOTS=$(sudo defaults read $HOMEFOLDER/Library/Preferences/com.apple.dock persistent-apps | grep tile-type | awk '/file-tile/ {print NR}')
for  slot in $DOCKSLOTS
do
DOCKPATH=`sudo /usr/libexec/PlistBuddy -c "print persistent-apps:$[$slot-1]:tile-data:file-data:_CFURLString" $HOMEFOLDER/Library/Preferences/com.apple.dock.plist`
if [[ $DOCKPATH =~ "Microsoft%20Lync.app" ]];
then
sudo /usr/libexec/PlistBuddy -c "Delete persistent-apps:$[$slot-1]" $HOMEFOLDER/Library/Preferences/com.apple.dock.plist
fi
done
echo "`date`: Removed Lync from $username dock"
done
sleep 5
sudo killall Dock -HUP
# Check if Lync is running, kill if it is:
LyncRunningCheck
# Delete the Application:
sudo rm -rf /Applications/Microsoft\ Lync.app/
echo "`date`: Removed /Applications/Microsoft\ Lync.app/"
# Remove Dock Icon:
for username in $VISIBLEUSERS
do
HOMEFOLDER=$(sudo dscl . -read /Users/$username NFSHomeDirectory | awk '{print $2}')
DOCKSLOTS=$(sudo defaults read $HOMEFOLDER/Library/Preferences/com.apple.dock persistent-apps | grep tile-type | awk '/file-tile/ {print NR}')
for  slot in $DOCKSLOTS
do
DOCKPATH=`sudo /usr/libexec/PlistBuddy -c "print persistent-apps:$[$slot-1]:tile-data:file-data:_CFURLString" $HOMEFOLDER/Library/Preferences/com.apple.dock.plist`
if [[ $DOCKPATH =~ "Microsoft%20Lync.app" ]];
then
sudo /usr/libexec/PlistBuddy -c "Delete persistent-apps:$[$slot-1]" $HOMEFOLDER/Library/Preferences/com.apple.dock.plist
fi
done
echo "`date`: Removed Lync from $username dock"
# Remove User Data:
rm -rf $HOMEFOLDER/Library/Preferences/ByHost/MicrosoftLyncRegistrationDB.*.plist
rm -rf $HOMEFOLDER/Library/Preferences/MicrosoftLyncRegistrationDB.plist
rm -rf $HOMEFOLDER/Library/Preferences/com.microsoft.Lync.plist
rm -rf $HOMEFOLDER/Library/Logs/Microsoft-Lync-*.log
rm -rf $HOMEFOLDER/Documents/Microsoft\ User\ Data/Microsoft\ Lync\ Data/
rm -rf $HOMEFOLDER/Documents/Microsoft\ User\ Data/Microsoft\ Lync\ History/
rm -rf $HOMEFOLDER/Keychains/OC_KeyContainer*
echo "`date`: Removed $username's preferences, logs, history and keychains"
# Remove Presence Handler:
HANDLERS=$(sudo /usr/libexec/PlistBuddy -c "print" $HOMEFOLDER/Library/Preferences/com.apple.LaunchServices.plist | grep Dict | awk '{print NR}')
for  handler in $HANDLERS
do
HandlerCheck=`sudo /usr/libexec/PlistBuddy -c "print LSHandlers:$[$handler-1]:LSHandlerRollAll" $HOMEFOLDER/Library/Preferences/com.apple.LaunchServices.plist`
if [[ $HandlerCheck =~ "com.microsoft.lync" ]];
then
sudo /usr/libexec/PlistBuddy -c "Delete LSHandlers:$[$handler-1]" $HOMEFOLDER/Library/Preferences/com.apple.LaunchServices.plist
echo "`date`: $username x-mspresence handler removed."
fi
done
done
sleep 5
sudo killall Dock -HUP
exit 0

                                                                                                                                                                                                                  Run the script by pasting it into a text editor, saving it as .sh and then making it executable by chmod +x lync_removal.sh, run it using: sh lync_removal.sh from the terminal.
This works silently so there won't be any popup window at all.
I am still working on removing keychain entries for Lync, but have found no way of doing this without serious risk on nuking the keychain.

5 comments:

  1. Cleaned up script:

    #!/bin/bash

    LYNC_PID=$(ps ax | grep "/Applications/Microsoft Lync.app/Contents/MacOS/Microsoft Lync" | grep -v grep | awk '{ print $1 }')
    kill -9 ${LYNC_PID} 2>/dev/null

    USER_LIST=`dscl . list /Users UniqueID | awk '$2 > 500 { print $1 }'`
    for USER in ${USER_LIST};
    do
    rm -rf "/Users/$USER/Library/Preferences/com.microsoft.Lync.plist" 2>/dev/null
    rm -rf "/Users/$USER/Library/Preferences/ByHost"/MicrosoftLyncRegistrationDB.*.plist 2>/dev/null
    rm -rf "/Users/$USER/Library/Logs"/Microsoft-Lync*.log* 2>/dev/null
    rm -rf "/Users/$USER/Documents/Microsoft User Data/Microsoft Lync Data" 2>/dev/null
    rm -rf "/Users/$USER/Documents/Microsoft User Data/Microsoft Lync History" 2>/dev/null
    rm -rf "/Users/$USER/Library/Keychains/"OC* 2>/dev/null
    done

    rm -R "/Applications/Microsoft Lync.app" 2>/dev/null
    rm -R "/Library/Internet Plug-Ins/MeetingJoinPlugin.plugin" 2>/dev/null

    exit 0

    ReplyDelete
  2. This is perfect. thanks for posting

    ReplyDelete
  3. This is perfect. thanks for posting

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Apologies for the double post -- it's always after you post you find the errors in your script! Thanks for the work Anon & WWF-dude!

    Updated:
    * added nuke stuff out of the receipts database (helpful for endpoint management systems, e.g. munki)
    * added removal of ~/Library/Cache/com.microsoft.Lync

    ====== script update =====

    #!/bin/bash

    PS="/bin/ps"
    GREP="/usr/bin/grep"
    AWK="/usr/bin/awk"
    KILL="/bin/kill"
    RM="/bin/rm"
    PKGUTIL="/usr/sbin/pkgutil"

    # Delete Lync
    LYNC_PID=$($PS ax | $GREP "/Applications/Microsoft Lync.app/Contents/MacOS/Microsoft Lync" | $GREP -v $GREP | $AWK '{ print $1 }')
    $KILL -9 ${LYNC_PID} 2>/dev/null

    USER_LIST=`/usr/bin/dscl . list /Users UniqueID | /usr/bin/awk '$2 > 500 { print $1 }'`
    for USER in ${USER_LIST};
    do
    $RM -rf "/Users/$USER/Library/Preferences/com.microsoft.Lync.plist" 2>/dev/null
    $RM -rf "/Users/$USER/Library/Caches/com.microsoft.Lync" 2>/dev/null
    $RM -rf "/Users/$USER/Library/Preferences/ByHost"/MicrosoftLyncRegistrationDB.*.plist 2>/dev/null
    $RM -rf "/Users/$USER/Library/Logs"/Microsoft-Lync*.log* 2>/dev/null
    $RM -rf "/Users/$USER/Documents/Microsoft User Data/Microsoft Lync Data" 2>/dev/null
    $RM -rf "/Users/$USER/Documents/Microsoft User Data/Microsoft Lync History" 2>/dev/null
    $RM -rf "/Users/$USER/Library/Keychains/"OC* 2>/dev/null
    done

    $RM -R "/Applications/Microsoft Lync.app" 2>/dev/null
    $RM -R "/Library/Internet Plug-Ins/MeetingJoinPlugin.plugin" 2>/dev/null

    $PKGUTIL --pkgs | $GREP com.microsoft.lync | /usr/bin/xargs -L1 $PKGUTIL --forget

    exit 0

    ReplyDelete

Detect autopilot session

  Ensuring that some apps only install during autopilot is not easily accomplished, you can use the below powershell script as a requiremen...