Created
February 8, 2017 14:25
-
-
Save adamdriscoll/20438e72eee69b214d909a5d7aa168fa to your computer and use it in GitHub Desktop.
SetSystemDate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void SetSystemDate(Domain domain, DateTime dateTime) | |
{ | |
var connectionOptions = new ConnectionOptions(); | |
connectionOptions.Username = domain.UserName; | |
connectionOptions.Password = domain.Password; | |
connectionOptions.EnablePrivileges = true; | |
connectionOptions.Authentication = AuthenticationLevel.PacketPrivacy; | |
connectionOptions.Impersonation = ImpersonationLevel.Impersonate; | |
var offset = TimeZoneInfo.Local.GetUtcOffset(dateTime); | |
var minuteOffset = $"{Math.Round(offset.TotalMinutes):000}"; | |
var datetimeString = dateTime.ToString($"yyyyddMMHHmmss.000000" + minuteOffset); | |
var scope = new ManagementScope($"\\\\{domain.Controller}\\root\\cimv2", connectionOptions); | |
scope.Connect(); | |
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem"); | |
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query)) | |
{ | |
foreach (ManagementObject mobj in searcher.Get()) | |
{ | |
mobj.InvokeMethod("SetDateTime", new[] { datetimeString }); | |
mobj.Dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment