dsccommunity / xTimeZone

THIS MODULE HAS BEEN DEPRECATED Please use ComputerManagementDsc instead: https://github.com/PowerShell/ComputerManagementDsc
MIT License
7 stars 11 forks source link

xTimeZone module should use .NET APIs instead of tzutil.exe #5

Closed pcgeek86 closed 8 years ago

pcgeek86 commented 9 years ago

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

hahndorf commented 9 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?

pcgeek86 commented 9 years ago

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

jbruettcva commented 8 years ago

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

PlagueHO commented 8 years ago

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

TravisEz13 commented 8 years ago

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

PlagueHO commented 8 years ago

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

2016-04-16_10-09-40

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.

TravisEz13 commented 8 years ago

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.

PlagueHO commented 8 years ago

@TravisEz13 - Good idea's. I'll get it done this week.

pcgeek86 commented 8 years ago

@TravisEz13 @PlagueHO very nice -- thanks for your efforts.