首页 > 解决方案 > 外出时的 Powershell 脚本

问题描述

我正在运行以下 PS1 脚本:

$userIdentity = "Firstname.Lastname"
$startTime = "06/13/18 13:00:00"
$endTime = "06/14/18 18:00:00"
$internalMessage = "Internal Message"
$externalMessage = "External Message"
Set-MailboxAutoReplyConfiguration -Identity $userIdentity -InternalMessage $internalMessage -ExternalMessage $externalMessage -AutoReplyState Scheduled -StartTime "$startTime" -EndTime "$endTime"

但它失败并出现以下错误:

Cmdlet error with following error message:
System.Management.Automation.ParentContainsErrorRecordException: 
Cannot process argument transformation on parameter 'StartTime'
Cannot convert value "13/06/2018 13:00:00" to type "System.DateTime". 
Error: "String was not recognized as a valid DateTime."

它看起来像一个 system.datetime 格式问题,即英国而不是美国格式或类似的格式,但如下:

[datetime] "06/13/18 13:00:00"

返回

13 June 2018 13:00:00

所以系统显然可以翻译它 - 任何帮助将不胜感激!

谢谢

标签: powershell

解决方案


我们最近也遇到了这个问题。

我们注意到的是,如果 Set-MailboxAutoReplyConfiguration 命令用于文化在“en-us”之外的 shell,它将无法正确解析日期。

在你的 powershell 会话中使用它来检查文化;

[System.Threading.Thread]::CurrentThread.CurrentCulture

如果不是“en-us”,请尝试将您的操作系统显示语言设置为“en-us”(确保区域设置为“匹配 Windows 显示语言”)。您应该在更改语言后重新加载 shell。

一旦我们在 en-us 地区,日期解析得很好。


推荐阅读