ninja-one/old/enable-auto-timezone.ps1

#Requires -Version 5.1

<#
.SYNOPSIS
    Adds a shared drive from a server on the network.
.DESCRIPTION
    Adds a shared drive from a server on the network.
#>


begin {
      
}
process {
    try {
        # Enable script to run with administrative privileges
        if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
            Write-Host "Please run this script as an Administrator."
            exit 1
        }

        Write-Host "Starting to enable 'Set time zone automatically'..."

        # Step 1: Enable the Windows Location Service
        Write-Host "Enabling the Windows Location Service..."
        Set-Service -Name "lfsvc" -StartupType Automatic
        Start-Service -Name "lfsvc"

        # Step 2: Enable 'Let apps access your location'
        Write-Host "Verifying and enabling 'Let apps access your location'..."
        $locationAccessKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\location"
        $appAccessValue = (Get-ItemProperty -Path $locationAccessKey -Name "Value" -ErrorAction SilentlyContinue).Value
        $hkcu_locationAccessKey = "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\location"
        $hkcu_appAccessValue = (Get-ItemProperty -Path $hkcu_locationAccessKey -Name "Value" -ErrorAction SilentlyContinue).Value
        
        if ($appAccessValue -ne "Allow") {
            Write-Host "'Let apps access your location' is disabled. Enabling it now..."
            Set-ItemProperty -Path $locationAccessKey -Name "Value" -Value "Allow"
        }
        else {
            Write-Host "'Let apps access your location' is already enabled."
        }
        
        if ($hkcu_appAccessValue -ne "Allow") {
            Write-Host "'Let apps access your location' is disabled. Enabling it now..."
            Set-ItemProperty -Path $hkcu_locationAccessKey -Name "Value" -Value "Allow"
        }
        else {
            Write-Host "'Let apps access your location' is already enabled."
        }

        # Step 3: enable automatic time zone setting service
        Write-Host "Setting the startup type for automatic time zone update..."
        Set-Service -Name "tzautoupdate" -StartupType Automatic

        # Step 4: Restart the Time Zone Auto-Update Service
        Write-Host "Restarting the Time Zone Auto-Update Service..."
        Stop-Service -Name "tzautoupdate" -Force
        Start-Service -Name "tzautoupdate"
        Write-Host "Restarting the Windows Time Service..."
        Stop-Service -Name "w32time" -Force
        Start-Service -Name "w32time"

        # Step 5: Verify the current time zone and auto-update status
        Write-Host "Verifying the current time zone..."
        $timeZone = Get-TimeZone
        Write-Host "Current Time Zone: $($timeZone.Id)"
        Write-Host "'Set time zone automatically' has been enabled successfully."
    }
    catch {
        Write-Error $_
        Write-Host "[Error] Failed to add or remove the following registry key: $Path\$Name"

        exit 1
    }
    exit 0
}
end {

}