Also, by "works fine during logon", if you are running the script as GPO Startup Script, it will run in a different context than if you run it manually by the user. GPO scripts run with permissions as System, where as when you run the script manually it runs with the permissions of the user.