Running a powershell script as a service but in the context of the currently logged on user
I have a question that may be more of a design/windows question, but I figure if anyone has resolved this issue it would be a scripter ;)
I'm having an issue with a particular powershell script. The script runs perfectly when I run the script as me (with admin rights) and with my test account (user rights). I want to run the script as a service, but when the script runs as a service, it of course runs under the system account which in turn causes part of the script to fail. Actually it doesn't really fail, it just does not do what I want it to do... here's an explaination of the script.
The script is deigned to run a check, and based on the result of the check, toggle registry settings between two different values. The script then pauses for a bit and then re-runs so it is essentially monitoring and adjusting settings on the fly. I believe the problem has to do with the registry keys the script is modifying which are all located in the HKCU hive. When the script runs under the currently logged on user, the keys are changed as designed. When the script runs under the system account, the script changes the keys in HKU/S-1-5-18 which is the System account profile, instead of HKCU.
I read up on some ways to impersonate users inside powershell but none of these seem to work (maybe I am not implementing them properly). I also tried calling external files (other ps files, bat, vb, etc) but I still run into the same issue.
Does anyone know a way to run a script as the curretly logged on user OR edit the HKCU keys with the system account (or other service account)?
Also, I've read that running a powershell script as a service is not a good idea, but no one said WHY. Can anyone fill me in on this one?