请为此建议前进的方式,同样我必须为enddate,用户名等做。 样品:
$StartDate, $String = "", ""
$StartDate = Read-Host -Prompt 'Enter the start date of the logs, Ex: 17/07/2017 09:00:00 '
if ($StartDate -and ( $StartDate -ne " ") -and ($StartDate -ne "")) {
$StartDate = $StartDate -replace "`t|`n|`r", ""
$String += " -After '$StartDate'"
} else {
'You did not enter a valid Start date!'
}
echo "Get-EventLog -LogName Application $String"
Get-EventLog -LogName Application $String
输出:
Get-EventLog -LogName Application -After '19/07/2017' Get-EventLog : Cannot bind parameter 'InstanceId'. Cannot convert value " -After '19/07/2017'" to type "System.Int64". Error: "Input string was not in a correct format." At C:\Users\kumars2\Downloads\Santosh\Powershell scripts\Enhancements\View logs examples\small_test.ps1:17 char:13 + Get-EventLog <<<< -LogName Application $String + CategoryInfo : InvalidArgument: (:) [Get-EventLog], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetEventLogCommand
答案 0 :(得分:4)
如果要为cmdlet构造参数列表,则应使用splatting而不是构建(部分)字符串命令行。您收到的错误是因为PowerShell将整个字符串$SPARK_HOME/jars
作为参数传递给参数" -After '$StartDate'"
。此外,您的日期字符串的格式为-InstanceId
。 PowerShell无法自动将此字符串转换为dd/MM/yyyy
值,因此您需要自己执行此操作。
DateTime
答案 1 :(得分:1)
我不是100%肯定你正在做的一些代码在那里蹦蹦跳跳但是假设日期有效,这没有问题:
$After = read-host
Get-EventLog -LogName Application -After $After
您可以像这样验证输入:
$After = read-host
if ($After -as [DateTime]) {
Get-EventLog -LogName Application -After $After
} else {
Write-Host "Your input is not a valid date"
}