错误:“ Get-EventLog”未被识别为cmdlet的名称

时间:2019-03-11 22:02:43

标签: c#

通过Powershell提取事件日志时,出现错误“无法将'Get-EventLog'识别为cmdlet的名称”。

`公共列表GetEventLogs()         {             列表retVal = new List();

        string psScript = "Get-EventLog -LogName Application -Newest 10 -After (Get-Date).AddDays(-30) -EntryType Error";


        using (PowerShell ps = PowerShell.Create())
        {
            ps.Commands.Clear();
            Collection<PSObject> pSObjects = ps.AddScript(psScript).Invoke();

            foreach (PSObject p in pSObjects)
            {

                retVal.Add(p.ToString());
                Console.WriteLine(((System.ServiceProcess.ServiceController)p.BaseObject).DisplayName + " : " + ((System.ServiceProcess.ServiceController)p.BaseObject).Status);
            }

            if (ps.Streams.Error.Count > 0)
                foreach (var s in ps.Streams.Error)
                {
                    Console.WriteLine(s.Exception);
                }
        }
        return retVal;
    }

`

我遇到的错误是:

System.Management.Automation.CommandNotFoundException:术语“ Get-EventLog”未被识别为cmdlet,函数,脚本文件或可运行程序的名称。 检查名称的拼写,或者是否包含路径,请验证路径是否正确,然后重试。    在System.Management.Automation.CommandDiscovery.LookupCommandInfo处(字符串commandName,CommandTypes commandTypes,SearchResolutionOptions searchResolutionOptions,CommandOrigin commandOrigin,ExecutionContext上下文)    在System.Management.Automation.CommandDiscovery.LookupCommandProcessor处(字符串commandName,CommandOrigin commandOrigin,可空1 useLocalScope) at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource) at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, ExecutionContext context) at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext) at System.Management.Automation.Interpreter.ActionCallInstruction 6.Run(InterpretedFrame框架)    在System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame框架)

0 个答案:

没有答案