在powershell中输入不同的日期方式

sun*_*nny 3 powershell

我正在编写一个脚本来根据用户输入获取结果,这里用户可以给出日期或日期时间...我需要根据输入(日期或日期时间)获取结果。

我试过如下:

$StartDate  = Read-Host -Prompt 'Enter the start date of the logs, Ex: 17/07/2017 or 17/07/2017 09:00:00'

$culture = [Globalization.CultureInfo]::InvariantCulture

$pattern = 'dd\/MM\/yyyy HH:mm:ss', 'dd\/MM\/yyyy'

$params['After'] = [DateTime]::ParseExact($StartDate, $pattern, $culture)
Run Code Online (Sandbox Code Playgroud)

出现以下错误:

Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
+     $params['After'] = [DateTime]::ParseExact <<<< ($StartDate, $pattern, $culture)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
Run Code Online (Sandbox Code Playgroud)

请建议,我在这里错过了什么吗?

Chr*_*s N 5

我在日期上使用 PowerShell 的默认 Get-Date 函数很幸运。我会尝试只使用以下内容:

$StartDate = Get-Date (Read-Host -Prompt 'Enter the start date of the logs, Ex: 17/07/2017 or 17/07/2017 09:00:00')
Run Code Online (Sandbox Code Playgroud)