param (
[ValidateSet("Install", "UnInstall")]
[string]$Action = "unInstall"
)
$ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$LogPath = "C:\ProgramData\LogFiles"
$LogFile = Join-Path $LogPath "TZSetAutomatically.log"
if (-not (Test-Path $LogPath)) { New-Item -Path $LogPath -ItemType Directory -Force }
function Write-Log {
param([string]$Message)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$timestamp - $Message" | Out-File -Append -FilePath $LogFile -Encoding ascii
}
Write-Log "===== Time Zone Automatically Selection-Disable SCRIPT STARTED ====="
$srvName = "Tzautoupdate"
$regMainPath = "HKLM:\SYSTEM\CurrentControlSet\Services"
$regkeyname = Join-Path $regMainPath $srvName
$renamedKey = $regkeyname + "_Install"
function Stop-TZ{
try{if (Get-Service -name $srvName -ErrorAction SilentlyContinue){
Write-Log "Stopping service $srvName."
Stop-Service -Name $srvName -Force -ErrorAction SilentlyContinue}
}
catch{Write-Log "Unable to Stop Service $srvName."}
}
function Start-TZ{
try{if (Get-Service -name $srvName -ErrorAction SilentlyContinue){
Write-Log "Starting service $srvName."
Start-Service -Name $srvName -Force -ErrorAction SilentlyContinue}
}
catch{Write-Log "Unable to Start Service $srvName."}
}
#Installation-Fix Issue
if($Action -eq "Install")
{Write-Log "Running in Installation mode...."
Try{Stop-TZ
If(Test-Path $regkeyname){
Write-Log "Renaming $srvName to $($srvName)_Install"
Copy-Item -Path $regkeyname -Destination $renamedKey -Recurse -Force
Write-Log "Deleting $srvName."
Remove-Item -Path $regkeyname -Recurse -Force
Write-Log "Time Zone Automatically Selection-Disable successful."
}
}catch{Write-Log "Installation failed. Error:$_"}
Start-Sleep -Seconds 3
}
#Restore TZ Auto Update Service back
elseif($Action -eq "UnInstall"){
Write-Log "Running in Uninstallation mode...."
try{Stop-TZ
if(Test-Path $regkeyname){
Write-Log "Deleting existing $srvName key..."
remove-item -Path $regkeyname -Recurse -Force}
Write-Log "Renaming $srvName_Install to $srvName"
Copy-Item -Path $renamedKey -Destination $regkeyname -Recurse -Force
Write-Log "Deleting the backup key $srvName_Install."
Remove-Item -Path $renamedKey -Recurse -Force
Write-Log "Starting Service $srvName."
Start-TZ
Write-Log "Time Zone Automatically Selection-Enable successful."
}catch{}}
No comments:
Post a Comment
Leave your valuable words here for improve better.