In order to simplify the problem and since your are getting the variable sitename, why don't you read a username variable?
With that you'd make sure that the script execution is not dependent on the environmental variables made available the way the script is executed.