NSouth
NSouth

Reputation: 5276

How to set system date/time without being prompted with UAC

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

Answers (3)

Knuckle-Dragger
Knuckle-Dragger

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.

https://superuser.com/questions/693620/how-to-restore-the-ownership-of-a-folder-to-trusted-installer-using-silent-com/693627#693627

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

mjolinor
mjolinor

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

JaredPar
JaredPar

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).

  1. You could embed the administrator password in a script, have it create a new process as admin and change the time from there. Embedding an admit password in tests though would be a giant red flag.
  2. It's possible there is a security setting somewhere that you could change to make system time alterable by non-admin accounts. I don't actually know if this can be done. If you did this it would require the change on every single computer you run the tests on. Another red flag.

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

Related Questions