Closed pcgeek86 closed 8 years ago
I don't think there is a .NET API to set the system time zone. All examples out there are using either tzutil.exe
or unmanaged code via pinvoke. That brings up the question what is preferred, calling an existing native exe or a Win32 function directly?
@hahndorf I don't know if there's a Win32 API, but generally it's considered bad PowerShell form to call external executables. I would personally prefer Win32 APIs over external executables.
This looks like the solution, but it's over my head on how to convert this to powershell, or maybe you don't have to and can just use the native code?
https://channel9.msdn.com/coding4fun/articles/Changing-time-zones
@pcgeek86, @hahndorf, @jbruettcva:
I took a brief look at this while I was updating the tests on the Resource and the only way I could see this being done was using Win32 via GetDynamicTimeZoneInformation and SetDynamicTimeZoneInformation. Will require using reflection to create the DYNAMIC_TIME_ZONE_INFORMATION structure.
WMI and .NET will allow us to read the Timezone, but it can't be set - so neither of these will work.
I'll take a look in a few weeks when I get time, unless someone else (with more experience defining structs using reflection) wants to have a go.
@PlagueHO I found a sample and converted it to a class, using it via add-type and put it here. It still needs work (all console writes needs to be removed): https://gist.github.com/TravisEz13/209b59c0fb7c919b977a64b6d2d47ac3
But it may help...
Note: I have serious concerns about this working on nano.
@TravisEz13 - That is some awesome work there! :smiley: I wasn't looking forward to attacking this one.
You're right about it probably not working on Nano though: I gave it a try, but Add-Type isn't even a supported cmdlet on Nano in TP4.
That said, could we detect for Nano and fall back to tzutil only on Nano? Not an ideal solution by no means, but still better than the current state perhaps.
Yeah, my only addition would be to wrap what I wrote in a helper function. And, perhaps you should vote for this issue: User Voice - Implement cmdlets for manipulating Timezone on workstations and servers
Once you have the wrapper done, you could even add a comment pointing to that.
@TravisEz13 - Good idea's. I'll get it done this week.
@TravisEz13 @PlagueHO very nice -- thanks for your efforts.
The xTimeZone module should use .NET APIs instead of the tzutil.exe program to perform automation tasks. Wrapping exe's is obviously an option, but should be used as a last resort, as it's less PowerShell-friendly, compared to using native APIs and cmdlets.
Thanks to @joeyaiello for this idea.
Cheers, Trevor Sullivan Microsoft MVP: PowerShell http://trevorsullivan.net http://twitter.com/pcgeek86