Time Zone update script pushed from Intune runs but does not do anything

Matias G. Keib 0 Reputation points
2024-05-14T18:22:19.7433333+00:00

Hi guys, I hope you all are having a nice week so far!

We have an Autopilot provisioning package that has been working great for a long time now on win 10, but some of the configs and policies did not work well on Win 11 so we modified some of them, others we replaced with scripts. Among the things that do not work as expected is the automatic Time Zone setting. We push a script that sets TZ to be automatic, and it does impact as expected (during OOBE first login). Problem is, the TZ is NOT set properly until you restart the device or force the service to refresh.

Since the client tasked us to provide a ready to use device, making the end user restart their computer is not an option. After manually restarting tzautoupdate service with positive results (the TZ corrected to the right one without having to restart the computer), we then crafted a simple script that would do the same during provisioning...but it does not work.

We added logging to make sure the script was actually running. The log even indicates the service was successfully stopped and started again, but the Time Zone does not change to the right one.

Here's the script, very simple and rudimentary but it is just what we intend to do with automation:

# Define log file path
$LogFilePath = "C:\Intune\logs\tzautoupdate_log.txt"

# Ensure log directory exists
if (-Not (Test-Path "C:\Intune\logs"))
{
    New-Item -ItemType Directory -Path "C:\Intune\logs"
}

# Function to log messages
function Log-Message {
    param ([string]$Message)
    Add-Content -Path $LogFilePath -Value "$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) - $Message"
}

# Log current time zone
$currentTimeZone = (Get-TimeZone).Id
Log-Message "Current Time Zone: $currentTimeZone"

# Stop the tzautoupdate service
try
{
    Stop-Service -Name tzautoupdate -Force
    Log-Message "tzautoupdate service stopped successfully."
    Start-Sleep -Seconds 10  
}
catch
{
    Log-Message "Failed to stop tzautoupdate service: $_"
}

# Start the tzautoupdate service
try
{
    Start-Service -Name tzautoupdate
    Log-Message "tzautoupdate service started successfully."
}
catch
{
    Log-Message "Failed to start tzautoupdate service: $_"
}

# Log time zone after restart
$updatedTimeZone = (Get-TimeZone).Id
Log-Message "Updated Time Zone: $updatedTimeZone"

Is there a better option? Or a simpler one to achieve the same?

Thank you very much in advance for your answers and suggestions!

Microsoft Intune
Microsoft Intune
A Microsoft cloud-based management solution that offers mobile device management, mobile application management, and PC management capabilities.
4,503 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Crystal-MSFT 44,421 Reputation points Microsoft Vendor
    2024-05-15T01:56:21.1366667+00:00

    @Matias G. Keib. Thanks for posting in Q&A. From your decryption, it seems the script runs but the "set time zone automatically" not turned on. If so, it seems the script itself has some issue. You can manually test the script to double confirm.

    After researching, I find a link has a script to set time zone automatically. You can deploy it to the Autopilot device group to see if it works.

    https://deviceadvice.io/2021/10/21/set-time-zone-automatically-during-autopilot/

    Note: Non-Microsoft link, just for the reference.

    Please try and if there's any update, feel free to let us know.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.