I have a script to remove the default apps from the dock but I can't get it to execute when a user logs in. It only works when I run the policy in terminal or from Self Service. The policy is set to trigger at login but doesn't seem to run. Is there an issue with the script or a Jamf policy setting that should be modified?
#!/bin/bash
# Get currently logged in user
currentUser=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }')
# Path to dockutil tool
dockutilbin="/usr/local/bin/dockutil"
# Sleep for 30 seconds
sleep 30
# Remove default Mac apps from the dock
/bin/launchctl asuser $(id -u $currentUser) $dockutilbin --remove 'App Store' --restart $loggedInUserPlist
/bin/launchctl asuser $(id -u $currentUser) $dockutilbin --remove 'Contacts' --restart $loggedInUserPlist
/bin/launchctl asuser $(id -u $currentUser) $dockutilbin --remove 'FaceTime' --restart $loggedInUserPlist
/bin/launchctl asuser $(id -u $currentUser) $dockutilbin --remove 'Freeform' --restart $loggedInUserPlist
/bin/launchctl asuser $(id -u $currentUser) $dockutilbin --remove 'iTunes' --restart $loggedInUserPlist
/bin/launchctl asuser $(id -u $currentUser) $dockutilbin --remove 'Keynote' --restart $loggedInUserPlist
/bin/launchctl asuser $(id -u $currentUser) $dockutilbin --remove 'Mail' --restart $loggedInUserPlist
/bin/launchctl asuser $(id -u $currentUser) $dockutilbin --remove 'Maps' --restart $loggedInUserPlist
/bin/launchctl asuser $(id -u $currentUser) $dockutilbin --remove 'Messages' --restart $loggedInUserPlist
/bin/launchctl asuser $(id -u $currentUser) $dockutilbin --remove 'Music' --restart $loggedInUserPlist
/bin/launchctl asuser $(id -u $currentUser) $dockutilbin --remove 'News' --restart $loggedInUserPlist
/bin/launchctl asuser $(id -u $currentUser) $dockutilbin --remove 'Numbers' --restart $loggedInUserPlist
/bin/launchctl asuser $(id -u $currentUser) $dockutilbin --remove 'Pages' --restart $loggedInUserPlist
/bin/launchctl asuser $(id -u $currentUser) $dockutilbin --remove 'Safari' --restart $loggedInUserPlist
/bin/launchctl asuser $(id -u $currentUser) $dockutilbin --remove 'TV' --restart $loggedInUserPlist
# Add Google Chrome to the dock
dockutil --add '/Applications/Google Chrome.app'
# Restart the dock
killall cfprefsd
killall Dock
exit 0
