Oh dear God, either the user id is already loaded by the time the script starts running or the function is not working at all, because the logon is not being held up for 300 seconds. But thanks for that. I didn't realise that. I have updated my script accordingly.