Scripts run from Casper run as the root user. not the logged in user. Since you don't specify a property list location to modify, the command is trying to make the change for the root account. You have to get the logged in user information and then target their plist file.
loggedInUser=$(ls -l /dev/console | awk '{print $3}')
defaults write /Users/$loggedInUser/Library/Preferences/com.apple.systemuiserver menuExtras -array-add "/System/Library/CoreServices/Menu Extras/vpn.menu"
## Fix the permissions
chown "$loggedInUser" "/Users/$loggedInUser/Library/Preferences/com.apple.systemuiserver.plist"
killall SystemUIServer -HUP
@mm2270 Thanks for the quick response! Unfortunately, It is still not working though. I tried running the killall SystemUIServer -HUP afterwards as a separate script and still nothing.
You may be running into a sandboxing issue, especially if this is under Mavericks. Less so with older OSes, but still a possibility.
The other thing you can try, again, especially if doing this with Mavericks, is to kill cfprefsd before restarting SystemUIServer. Its possible Mavericks is simply pulling cached information in when SystemUIServer restarts, instead of the new settings from disk. Add this before the killall SystemUIServer line
killall cfprefsd
Use the domain is the correct way. Better then writing it directly to the file. Try to run as root in user context with 'su - "${USERNAME}" -c "YOUR_COMMAND". Should work.
@mm2270 Its working now, thank you for your help!! Only problem is that if the VPN icon was already there, there are now two appearing.
@jstine, you can do this via MCX. You can then scope it to all computers macbook*
No scripting need & stops it duplicating.
@bentoms that worked! thank you!