首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell显示更改时区后的旧时间

Powershell显示更改时区后的旧时间
EN

Stack Overflow用户
提问于 2015-03-20 08:00:13
回答 1查看 1.2K关注 0票数 3

下面是我的powershell脚本:

代码语言:javascript
复制
$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"
}

我想要实现的是

  1. 获得当前系统时间
  2. 将它与世界时(utc-6)进行比较。
  3. 如果是一样的话,那就没什么可做的了。如果没有,请将系统时区更改为utc-6。

一切正常,但一旦将系统时区设置为utc-6,当我检查当前的系统时间时,它将显示旧时间本身(尽管我可以看到系统时区已经更改)。

看起来像是缓存。有这样的东西吗?或者我的剧本有什么问题吗?

有人能带我穿过一条好路吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-21 00:26:24

这不是你的剧本的错。Powershell只在创建会话时读取时区。

Get-Date和.Net方法,如[System.TimeZoneInfo]::Local“缓存”这一点,所以最简单的解决方案是关闭窗口并启动一个新的Powershell会话。启动一个新的本地会话并导入它也“解决”了这个问题,但是相当笨重。

WMI对象不受相同的“缓存”问题的影响。将此视为一种选择:

代码语言:javascript
复制
$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。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29161929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档