读取用户输入并传递给 Get-EventLog 函数
read userinput and pass to Get-EventLog function
请为此提出前进的方向,同样我必须为结束日期、用户名等做。
样本:
$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
我不是 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"
}
如果您想为 cmdlet 构建参数列表,您应该使用 splatting 而不是构建(部分)字符串命令行。您收到观察到的错误,因为 PowerShell 将整个字符串 " -After '$StartDate'"
作为参数传递给参数 -InstanceId
。此外,您的日期字符串的格式为 dd/MM/yyyy
。 PowerShell 无法自动将此字符串转换为 DateTime
值,因此您需要自己完成此操作。
$culture = [Globalization.CultureInfo]::InvariantCulture
$pattern = 'dd\/MM\/yyyy'
$StartDate = $StartDate -replace '\s' # remove all whitespace from date string
$EndDate = $EndDate -replace '\s' # remove all whitespace from date string
$params = @{
'LogName' = 'Application'
}
if ($StartDate) {
$params['After'] = [DateTime]::ParseExact($StartDate, $pattern, $culture)
}
if ($EndDate) {
$params['Before'] = [DateTime]::ParseExact($EndDate, $pattern, $culture)
}
Get-EventLog @params
请为此提出前进的方向,同样我必须为结束日期、用户名等做。 样本:
$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
我不是 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"
}
如果您想为 cmdlet 构建参数列表,您应该使用 splatting 而不是构建(部分)字符串命令行。您收到观察到的错误,因为 PowerShell 将整个字符串 " -After '$StartDate'"
作为参数传递给参数 -InstanceId
。此外,您的日期字符串的格式为 dd/MM/yyyy
。 PowerShell 无法自动将此字符串转换为 DateTime
值,因此您需要自己完成此操作。
$culture = [Globalization.CultureInfo]::InvariantCulture
$pattern = 'dd\/MM\/yyyy'
$StartDate = $StartDate -replace '\s' # remove all whitespace from date string
$EndDate = $EndDate -replace '\s' # remove all whitespace from date string
$params = @{
'LogName' = 'Application'
}
if ($StartDate) {
$params['After'] = [DateTime]::ParseExact($StartDate, $pattern, $culture)
}
if ($EndDate) {
$params['Before'] = [DateTime]::ParseExact($EndDate, $pattern, $culture)
}
Get-EventLog @params