Microsoft Company Portal removal script.

KyleEricson
Valued Contributor II

I have this info from Microsoft. I know how to remove the files or folders, but the keychain is something I want to script to remove.

See the steps below from Microsoft:

Uninstall Company Portal App from Mac
Delete these files on the machine if they exist:
/Library/Application Support/com.microsoft.CompanyPortal.usercontext.info
/Library/Application Support/com.microsoft.CompanyPortal
/Library/Application Support/com.jamfsoftware.selfservice.mac
/Library/Saved Application State/com.jamfsoftware.selfservice.mac.savedState
/Library/Saved Application State/com.microsoft.CompanyPortal.savedState
/Library/Preferences/com.microsoft.CompanyPortal.plist
/Library/Preferences/com.jamfsoftware.selfservice.mac.plist
/Library/Preferences/com.jamfsoftware.management.jamfAAD.plist
/Users/<username>/Library/Cookies/com.microsoft.CompanyPortal.binarycookies
/Users/<username>/Library/Cookes/com.jamf.management.jamfAAD.binarycookies
Remove anything from the keychain on the machine that references Microsoft, Intune, or Company Portal
Including DeviceLogin.microsoft.com Certificates
Remove Jamf references except for Jamf public and Private key
Removing the public and private key will break enrollment and will be severed from Jamf
Reboot Mac

Read My Blog: https://www.ericsontech.com
3 REPLIES 3

dan-snelson
Valued Contributor II

@kericson See if the following helps:

#!/bin/bash
####################################################################################################
#
# ABOUT
#
#   Removes Keychain entries as specified in JSS script parameters
#
####################################################################################################
#
# HISTORY
#
#   Version 1.0, 6-Jun-2018, Dan K. Snelson
#       Original version
#
####################################################################################################


### Variables
loggedInUser=$( /usr/bin/stat -f %Su "/dev/console" )

entryName1="$4"       # Keychain Entry Name (i.e., "com.microsoft.SkypeForBusiness.HockeySDK")
entryName2="$5"       # Keychain Entry Name (i.e., "skype")
entryName3="$6"       # Keychain Entry Name (i.e., "Skype for Business")
entryName4="$7"       # Keychain Entry Name
entryName5="$8"       # Keychain Entry Name
entryName6="$9"       # Keychain Entry Name



### Functions
removeKeychainEntry() {

    echo " " # Blank line for readability

    echo "* Keychain entry to remove: ${1}"

    /usr/bin/security delete-generic-password -l "${1}" /Users/${loggedInUser}/Library/Keychains/login.keychain-db
    echo "* Removed ${1}."

}



### Command

echo " "
echo "### Removing Keychain Entries ###"
echo " "


# Keychain Entry Name 1 to remove
if [ ! -z "${entryName1}" ]; then
    removeKeychainEntry "${entryName1}"
fi

# Keychain Entry Name 2 to remove
if [ ! -z "${entryName2}" ]; then
    removeKeychainEntry "${entryName2}"
fi

# Keychain Entry Name 3 to remove
if [ ! -z "${entryName3}" ]; then
    removeKeychainEntry "${entryName3}"
fi

# Keychain Entry Name 4 to remove
if [ ! -z "${entryName4}" ]; then
    removeKeychainEntry "${entryName4}"
fi

# Keychain Entry Name 5 to remove
if [ ! -z "${entryName5}" ]; then
    removeKeychainEntry "${entryName5}"
fi

# Keychain Entry Name 6 to remove
if [ ! -z "${entryName6}" ]; then
    removeKeychainEntry "${entryName6}"
fi


exit 0      ## Success
exit 1      ## Failure

KyleEricson
Valued Contributor II

Thanks, I'll look into this.

Read My Blog: https://www.ericsontech.com

KyleEricson
Valued Contributor II

How could I perform a search for items and remove? see below:

Remove anything from the keychain on the machine that references Microsoft, Intune, or Company Portal
Including DeviceLogin.microsoft.com Certificates
Remove Jamf references except for Jamf public and Private key
Removing the public and private key will break enrollment and will be severed from Jamf

Read My Blog: https://www.ericsontech.com