@@ -310,6 +310,155 @@ function Test-DscObjectHasProperty
310310 return $false
311311}
312312
313+ <#
314+ . SYNOPSIS
315+ Get the of the current time zone Id.
316+ #>
317+ function Get-TimeZoneId
318+ {
319+ [CmdletBinding ()]
320+ param
321+ (
322+ )
323+
324+ if (Test-Command - Name ' Get-TimeZone' - Module ' Microsoft.PowerShell.Management' )
325+ {
326+ Write-Verbose - Message ($LocalizedData.GettingTimeZoneMessage -f ' Cmdlets' )
327+
328+ $timeZone = (Get-TimeZone ).StandardName
329+ }
330+ else
331+ {
332+ Write-Verbose - Message ($LocalizedData.GettingTimeZoneMessage -f ' CIM' )
333+
334+ $timeZone = (Get-CimInstance `
335+ - ClassName Win32_TimeZone `
336+ - Namespace root\cimv2).StandardName
337+ }
338+
339+ Write-Verbose - Message ($LocalizedData.CurrentTimeZoneMessage `
340+ -f $timeZone )
341+
342+ $timeZoneInfo = [System.TimeZoneInfo ]::GetSystemTimeZones() |
343+ Where-Object StandardName -eq $timeZone
344+
345+ return $timeZoneInfo.Id
346+ } # function Get-TimeZoneId
347+
348+ <#
349+ . SYNOPSIS
350+ Compare a time zone Id with the current time zone Id.
351+
352+ . PARAMETER TimeZoneId
353+ The Id of the time zone to compare with the current time zone.
354+ #>
355+ function Test-TimeZoneId
356+ {
357+ [CmdletBinding ()]
358+ param
359+ (
360+ [Parameter (Mandatory = $true )]
361+ [System.String ]
362+ $TimeZoneId
363+ )
364+
365+ # Test if the expected value is the same as the current value.
366+ $currentTimeZoneId = Get-TimeZoneId
367+
368+ return $TimeZoneId -eq $currentTimeZoneId
369+ } # function Test-TimeZoneId
370+
371+ <#
372+ . SYNOPSIS
373+ Sets the current time zone using a time zone Id.
374+
375+ . PARAMETER TimeZoneId
376+ The Id of the time zone to set.
377+ #>
378+ function Set-TimeZoneId
379+ {
380+ [CmdletBinding ()]
381+ param
382+ (
383+ [Parameter (Mandatory = $true )]
384+ [System.String ]
385+ $TimeZoneId
386+ )
387+
388+ if (Test-Command - Name ' Set-TimeZone' - Module ' Microsoft.PowerShell.Management' )
389+ {
390+ Set-TimeZone - Id $TimeZoneId
391+ }
392+ else
393+ {
394+ if (Test-Command - Name ' Add-Type' - Module ' Microsoft.Powershell.Utility' )
395+ {
396+ # We can use reflection to modify the time zone.
397+ Write-Verbose - Message ($LocalizedData.SettingTimeZoneMessage `
398+ -f $TimeZoneId , ' .NET' )
399+
400+ Set-TimeZoneUsingDotNet - TimeZoneId $TimeZoneId
401+ }
402+ else
403+ {
404+ # For anything else use TZUTIL.EXE.
405+ Write-Verbose - Message ($LocalizedData.SettingTimeZoneMessage `
406+ -f $TimeZoneId , ' TZUTIL.EXE' )
407+
408+ try
409+ {
410+ & tzutil.exe @ (' /s' , $TimeZoneId )
411+ }
412+ catch
413+ {
414+ Write-Verbose - Message $_.Exception.Message
415+ } # try
416+ } # if
417+ } # if
418+
419+ Write-Verbose - Message ($LocalizedData.TimeZoneUpdatedMessage `
420+ -f $TimeZoneId )
421+ } # function Set-TimeZoneId
422+
423+ <#
424+ . SYNOPSIS
425+ This function sets the time zone on the machine using .NET reflection.
426+ It exists so that the ::Set method can be mocked by Pester.
427+
428+ . PARAMETER TimeZoneId
429+ The Id of the time zone to set using .NET.
430+ #>
431+ function Set-TimeZoneUsingDotNet
432+ {
433+ [CmdletBinding ()]
434+ param
435+ (
436+ [Parameter (Mandatory = $true )]
437+ [System.String ]
438+ $TimeZoneId
439+ )
440+
441+ # Add the [TimeZoneHelper.TimeZone] type if it is not defined.
442+ if (-not ([System.Management.Automation.PSTypeName ] ' TimeZoneHelper.TimeZone' ).Type)
443+ {
444+ Write-Verbose - Message ($LocalizedData.AddingSetTimeZoneDotNetTypeMessage )
445+
446+ $setTimeZoneCs = Get-Content `
447+ - Path (Join-Path - Path $PSScriptRoot - ChildPath ' SetTimeZone.cs' ) `
448+ - Raw
449+
450+ Add-Type `
451+ - Language CSharp `
452+ - TypeDefinition $setTimeZoneCs
453+ } # if
454+
455+ [Microsoft.PowerShell.TimeZone.TimeZone ]::Set($TimeZoneId )
456+ } # function Set-TimeZoneUsingDotNet
457+
313458Export-ModuleMember - Function `
314459 Test-DscParameterState , `
315- Test-DscObjectHasProperty
460+ Test-DscObjectHasProperty , `
461+ Get-TimeZoneId , `
462+ Test-TimeZoneId , `
463+ Set-TimeZoneId , `
464+ Set-TimeZoneUsingDotNet
0 commit comments