Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Enables the location setting and turns on the "Set the timezone automatically" switch in Time & Language > Date & Time.
- .NOTES
- Original Author: James Robinson | SkipToTheEndpoint | https://skiptotheendpoint.co.uk
- Version v2.1 modifications by: MIDItheKID
- Modifications by: Ismael "PAITUWIN" Espinosa
- Version: v2.2
- Modified Date: 2025-03-26
- Intune Info:
- Script type - Platform Script
- Assign to - Devices
- Script Settings:
- Run this script using the logged on credentials - No
- Enforce script signature check - No
- Run script in 64-bit PowerShell Host - Yes
- #>
- #### Logging Variables ####
- $Script:ScriptName = "tzautoupdate-remediation"
- $Script:LogFile = "$ScriptName.log"
- $Script:LogsFolder = "$env:ProgramData\Microsoft\IntuneManagementExtension\Logs"
- #### Script Variables ####
- $ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
- $Host.UI.RawUI.WindowTitle = '$ScriptName'
- $LocationValue = "Allow"
- $AutoTZValue = "3"
- $LFSVCValue = "1"
- $SensorValue = "1"
- #### Functions ####
- function Start-Logging {
- Start-Transcript -Path "$LogsFolder\$LogFile" -Append
- Write-Host "Current script timestamp: $(Get-Date -Format yyyy-MM-dd_HH-mm)"
- }
- function Set-RegistryValue {
- param (
- [string]$Path,
- [string]$Name,
- [string]$Value
- )
- # Determine property type: if the value can be cast to an integer, use DWord; otherwise, use String.
- $propertyType = "String"
- if ([int]::TryParse($Value, [ref]$null)) {
- $propertyType = "DWord"
- $Value = [int]$Value
- }
- # Create the registry key if it does not exist.
- if (-not (Test-Path -Path $Path)) {
- Write-Host "Registry key $Path does not exist. Creating..."
- New-Item -Path $Path -Force | Out-Null
- }
- try {
- $currentValue = (Get-ItemProperty -Path $Path -Name $Name -ErrorAction SilentlyContinue).$Name
- if ($null -eq $currentValue) {
- Write-Host "Registry property $Name does not exist at $Path. Creating with value $Value..."
- New-ItemProperty -Path $Path -Name $Name -Value $Value -PropertyType $propertyType -Force | Out-Null
- }
- elseif ($currentValue -ne $Value) {
- Write-Host "Setting $Name to $Value at $Path"
- Set-ItemProperty -Path $Path -Name $Name -Value $Value
- }
- else {
- Write-Host "$Name is already set to $Value at $Path"
- }
- }
- catch {
- Write-Error "$($_.Exception.Message)"
- }
- }
- #### Script ####
- Start-Logging
- try {
- # Set the location value
- Set-RegistryValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\location" -Name "Value" -Value $LocationValue
- # 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
- Set-Service -Name tzautoupdate -StartupType Automatic
- $tzautoupdate = Get-Service -Name tzautoupdate
- if ($tzautoupdate.Status -ne "Running") {
- Start-Service -Name tzautoupdate
- }
- else {
- Restart-Service -Name tzautoupdate -Force
- }
- Set-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\tzautoupdate" -Name "Start" -Value $AutoTZValue
- #Set geolocation registry key and (re)start geolocation service
- Set-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\lfsvc\Service\Configuration" -Name "Status" -Value $LFSVCValue
- Write-Host "(Re)Starting geolocation service"
- $lfsvc = Get-Service -Name lfsvc
- if ($lfsvc.Status -ne "Running") {
- Start-Service -Name lfsvc
- }
- else {
- Restart-Service -Name lfsvc -Force
- }
- # Set sensor value
- Set-RegistryValue -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Sensor\Overrides\{BFA794E4-F964-4FDB-90F6-51056BFE4B44}" -Name "SensorPermissionState" -Value $SensorValue
- Exit 0
- }
- catch {
- Write-Error "$($_.Exception.Message)"
- Exit 1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement