下面是我的powershell脚本:
$time = (Get-Date).ToShortTimeString()
Write-Host "Current Time: "$time
$destzone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Standard Time")
$desttime = [System.TimeZoneInfo]::ConvertTimeFromUtc((Get-Date).ToUniversalTime(), $destzone)
$desttime = $desttime.ToShortTimeString()
Write-Host "UTC-6 = "$desttime
$newtime = (Get-Date).ToShortTimeString()
Write-Host "Current Time: "$newtime
if($newtime -eq $desttime)
{
Write-Host "ok"
}else
{
C:\Windows\System32\tzutil.exe /s "Central Standard Time"
}我想要实现的是
一切正常,但一旦将系统时区设置为utc-6,当我检查当前的系统时间时,它将显示旧时间本身(尽管我可以看到系统时区已经更改)。
看起来像是缓存。有这样的东西吗?或者我的剧本有什么问题吗?
有人能带我穿过一条好路吗?
发布于 2015-03-21 00:26:24
这不是你的剧本的错。Powershell只在创建会话时读取时区。
Get-Date和.Net方法,如[System.TimeZoneInfo]::Local“缓存”这一点,所以最简单的解决方案是关闭窗口并启动一个新的Powershell会话。启动一个新的本地会话并导入它也“解决”了这个问题,但是相当笨重。
WMI对象不受相同的“缓存”问题的影响。将此视为一种选择:
$curzone = (Get-WMIObject –Class Win32_TimeZone).Caption
$destzone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Standard Time").DisplayName
if($curzone -eq $destzone)
{
Write-Host "Current Time Zone already set to:"$curzone
}else
{
C:\Windows\System32\tzutil.exe /s "Central Standard Time"
$newcurzone = (Get-WMIObject –Class Win32_TimeZone).Caption
Write-Host "Time Zone updated to:"$newcurzone
}如果他们以后修复了这个问题,这个版本目前可以重复使用Powershell 5.0的Windows 10技术预览2。
https://stackoverflow.com/questions/29161929
复制相似问题