FredBainbridge

Set Timezone based on A.D Site

function Set-TimeZone {
    param( 
    [parameter(Mandatory=$true)]
    [string]$TimeZone

    ) 
    $osVersion = (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValue("CurrentVersion")
    $proc = New-Object System.Diagnostics.Process
    $proc.StartInfo.WindowStyle = "Hidden"
    if ($osVersion -ge 6.0)
    {
        # OS is newer than XP 
        $proc.StartInfo.FileName = "tzutil.exe"
        $proc.StartInfo.Arguments = "/s `"$TimeZone`"" 
    } 
    else 
    {
        # XP or earlier
        $proc.StartInfo.FileName = $env:comspec
        $proc.StartInfo.Arguments = "/c start /min control.exe TIMEDATE.CPL,,/z $TimeZone"
    } 
    $proc.Start() | Out-Null
}
#available timezones http://technet.microsoft.com/en-us/library/cc749073(v=ws.10).aspx
 
$sites = @{ 
"ADSiteName" = "Central Standard Time" 
"ADSiteName2" = "Pacific Standard Time"
"ADSiteName3" = "Eastern Standard Time"
}

#determine what site this machine is currently in
$obj = New-Object -ComObject ADSystemInfo 
$type = $obj.gettype()
$adsite = $type.InvokeMember("sitename","getProperty",$null,$obj,$null)
 
if($adsite.Length -gt 0){ 
    #we do not want to set the time zone unless we find the site. 
    if($sites[$adsite] -ne $null) 
    {
        Set-TimeZone $sites[$adsite]
    }
}

Share