未找到Powershell Get-EventLog -before参数

时间:2014-06-25 18:41:06

标签: powershell get-eventlog

我在这里有这段代码:

    $currentDate = get-date
    $pastDate = $currentDate.addhours(-5)


    $errorCommand = get-eventlog -Before $currentDate -After $pastDate -logname   Application -source "ESENT" 
    $errorInfo = $errorCommand | out-string

我有一台运行整个脚本的本地机器,它可以100%正常工作。 当我通过远程桌面在Windows Server标准上运行此代码时,我得到以下内容:

“Get-EventLog:在''之前找不到与参数名'匹配的参数 它指的是“$ errorCommand =”,我不能为我的生活弄清楚为什么它找不到这个参数,是不是我的powershell设置不正确?

1 个答案:

答案 0 :(得分:0)

内置的Get-EventLog似乎被同名的不同函数覆盖了。它不仅缺少许多标准参数,而且命令Get-Command Get-EventLog没有提及它应该具有的Microsoft.Powershell.Management

PS > Get-Command Get-EventLog

CommandType     Name             ModuleName                              
-----------     ----             ----------                              
Cmdlet          Get-EventLog     Microsoft.PowerShell.Management         


PS > 

您可以使用New-Alias将名称设置回原始cmdlet:

$currentDate = get-date
$pastDate = $currentDate.addhours(-5)

#####################################################################
New-Alias Get-EventLog Microsoft.PowerShell.Management\Get-EventLog
#####################################################################

$errorCommand = get-eventlog -Before $currentDate -After $pastDate -logname   Application -source "ESENT" 
$errorInfo = $errorCommand | out-stringApplication -source "ESENT" 

参见下面的演示:

PS > function Get-EventLog { 'Different' }  
PS > Get-EventLog  # This is a new function, not the original cmdlet
Different

PS > New-Alias Get-EventLog Microsoft.PowerShell.Management\Get-EventLog  
PS > Get-EventLog  # This is the original cmdlet
cmdlet Get-EventLog at command pipeline position 1
Supply values for the following parameters:
LogName: 

虽然最好先调查一下为什么重写cmdlet,然后再修改它。