Can anyone let me know what I'm doing wrong with the script below? We use it to allow trusted site access for Chrome. The main problem is that if a user isn't logged in at the time the policy runs, instead of the policy failing it creates a root directory inside of /Users. Specifically, it writes the Chrome plist at /Users/root/Library/Preferences/com.google.Chrome.plist. What can we do to prevent that?
#!/bin/bash
## Variables
AuthServers=https://autologon.microsoftazuread-sso.com,https://aadg.windows.net.nsatc.net,*.testenvironment.com
currentUser=`stat -f "%Su" /dev/console`
## Modify Chrome settings
## We can write the prefs regardless of whether or not Chrome is open
defaults write /Users/"$currentUser"/Library/Preferences/com.google.Chrome AuthServerWhitelist "$AuthServers"
chown "$currentUser" /Users/"$currentUser"/Library/Preferences/com.google.Chrome.plist
echo "Chrome preferences written to /Users/$currentUser/Library/Preferences/com.google.Chrome"
echo "AuthServers set to:"
defaults read /Users/"$currentUser"/Library/Preferences/com.google.Chrome AuthServerWhitelist
ls -l ~/Library/Preferences/com.google.Chrome.plist
exit 0