Reputation: 5276
For testing purposes, I often need to go back and forth between the current date and a past date. I wrote a quick powershell script so that I can just double-click a desktop shortcut to change system time to the desired hard-coded date. It works great, but I have to r-click and run as administrator. I'd like to successfully run without r-clicking.
I've tried editing security settings for the service in Local Security Policy, but the result is I don't see a UAC prompt, but the script does not succeed either. It still says
Set-Date : A required privilege is not held by the client
Any thoughts on how to give this privilege to the client? I want the script to run without being prompted for UAC. Thanks.
Upvotes: 3
Views: 9666
Reputation: 7046
Maybe you can manipulate the token and grant yourself SeSystemtimePrivilege
http://technet.microsoft.com/en-us/library/cc976700.aspx
There is a script floating around the web for doing so here.
Using that script you can run this command to enable the privilege (temporarily) then issue your Set-Date to travel backwards in time.
Enable-Privilege SeSystemtimePrivilege
Set-Date "November 5, 1955"
Write-Host "Are You Telling Me You Built A Time Machine Out Of A Delorean"
Upvotes: 3
Reputation: 68273
One possible option might be to use a scheduled task (set to run with hightest privileges) that reads a datetime from a file location, and use your script to write to that file and then run the task.
Upvotes: 2
Reputation: 754715
There really isn't a good way to actual change the time without a prompt. Changing the system time is a privileged operation and doing that without a prompt is tricky (if possible at all).
I think a much smoother approach would be to simply abstract away the idea of time in your code behind an interface. Let's say you were using C# you could define the following
interface ITimeUtil {
DateTime SystemTime { get; }
}
In the actual running program you would implement this interface by querying the system time directly as you do today. For the purpose of testing you would mock this interface to return times in the past.
Upvotes: 3