Advertisement
PAITUWIN

tzautoupdate

Mar 26th, 2025 (edited)
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.     .SYNOPSIS
  3.         Enables the location setting and turns on the "Set the timezone automatically" switch in Time & Language > Date & Time.
  4.  
  5.     .NOTES
  6.         Original Author: James Robinson | SkipToTheEndpoint | https://skiptotheendpoint.co.uk
  7.         Version v2.1 modifications by: MIDItheKID
  8.         Modifications by: Ismael "PAITUWIN" Espinosa
  9.         Version: v2.2
  10.         Modified Date: 2025-03-26
  11.  
  12.         Intune Info:
  13.         Script type - Platform Script
  14.         Assign to - Devices
  15.         Script Settings:
  16.             Run this script using the logged on credentials - No
  17.             Enforce script signature check - No
  18.             Run script in 64-bit PowerShell Host - Yes
  19. #>
  20.  
  21. #### Logging Variables ####
  22. $Script:ScriptName = "tzautoupdate-remediation"
  23. $Script:LogFile = "$ScriptName.log"
  24. $Script:LogsFolder = "$env:ProgramData\Microsoft\IntuneManagementExtension\Logs"
  25.  
  26. #### Script Variables ####
  27. $ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
  28. $Host.UI.RawUI.WindowTitle = '$ScriptName'
  29.  
  30. $LocationValue = "Allow"
  31. $AutoTZValue = "3"
  32. $LFSVCValue = "1"
  33. $SensorValue = "1"
  34.  
  35. #### Functions ####
  36. function Start-Logging {
  37.     Start-Transcript -Path "$LogsFolder\$LogFile" -Append
  38.     Write-Host "Current script timestamp: $(Get-Date -Format yyyy-MM-dd_HH-mm)"
  39. }
  40.  
  41. function Set-RegistryValue {
  42.     param (
  43.         [string]$Path,
  44.         [string]$Name,
  45.         [string]$Value
  46.     )
  47.  
  48.     # Determine property type: if the value can be cast to an integer, use DWord; otherwise, use String.
  49.     $propertyType = "String"
  50.     if ([int]::TryParse($Value, [ref]$null)) {
  51.         $propertyType = "DWord"
  52.         $Value = [int]$Value
  53.     }
  54.  
  55.     # Create the registry key if it does not exist.
  56.     if (-not (Test-Path -Path $Path)) {
  57.         Write-Host "Registry key $Path does not exist. Creating..."
  58.         New-Item -Path $Path -Force | Out-Null
  59.     }
  60.  
  61.     try {
  62.         $currentValue = (Get-ItemProperty -Path $Path -Name $Name -ErrorAction SilentlyContinue).$Name
  63.         if ($null -eq $currentValue) {
  64.             Write-Host "Registry property $Name does not exist at $Path. Creating with value $Value..."
  65.             New-ItemProperty -Path $Path -Name $Name -Value $Value -PropertyType $propertyType -Force | Out-Null
  66.         }
  67.         elseif ($currentValue -ne $Value) {
  68.             Write-Host "Setting $Name to $Value at $Path"
  69.             Set-ItemProperty -Path $Path -Name $Name -Value $Value
  70.         }
  71.         else {
  72.             Write-Host "$Name is already set to $Value at $Path"
  73.         }
  74.     }
  75.     catch {
  76.         Write-Error "$($_.Exception.Message)"
  77.     }
  78. }
  79.  
  80. #### Script ####
  81. Start-Logging
  82.  
  83. try {
  84.     # Set the location value
  85.     Set-RegistryValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\location" -Name "Value" -Value $LocationValue
  86.  
  87.     # Enable tzautoupdate Automatic Startup Type, Enable Auto Timezone value and (re)start tzautoupdate service. With this enabled when changing tzautoupdate value it allows to change the timezone without reboot
  88.     Set-Service -Name tzautoupdate -StartupType Automatic
  89.     $tzautoupdate = Get-Service -Name tzautoupdate
  90.     if ($tzautoupdate.Status -ne "Running") {
  91.         Start-Service -Name tzautoupdate
  92.     }
  93.     else {
  94.         Restart-Service -Name tzautoupdate -Force
  95.     }
  96.    
  97.     Set-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\tzautoupdate" -Name "Start" -Value $AutoTZValue
  98.  
  99.     #Set geolocation registry key and (re)start geolocation service
  100.     Set-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\lfsvc\Service\Configuration" -Name "Status" -Value $LFSVCValue
  101.     Write-Host "(Re)Starting geolocation service"
  102.     $lfsvc = Get-Service -Name lfsvc
  103.     if ($lfsvc.Status -ne "Running") {
  104.         Start-Service -Name lfsvc
  105.     }
  106.     else {
  107.         Restart-Service -Name lfsvc -Force
  108.     }
  109.  
  110.     # Set sensor value
  111.     Set-RegistryValue -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Sensor\Overrides\{BFA794E4-F964-4FDB-90F6-51056BFE4B44}" -Name "SensorPermissionState" -Value $SensorValue
  112.     Exit 0
  113. }
  114. catch {
  115.     Write-Error "$($_.Exception.Message)"
  116.     Exit 1
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement