예제 #1
0
        public static void RegisterPowerShellToActivity(PowerShell powerShell, CmdletActivity activity, IEnumerable pipelineInput, out List <PSObject> psDataCollection, bool isGetListAsync)
        {
            psDataCollection = null;
            int managedThreadId = Thread.CurrentThread.ManagedThreadId;

            if (AsyncServiceManager.workerThreads.Contains(managedThreadId))
            {
                AsyncServiceManager.WorkItem workItem = (AsyncServiceManager.WorkItem)AsyncServiceManager.workerThreads[managedThreadId];
                lock (workItem)
                {
                    workItem.ProgressCalculator.SetPipelineInput(pipelineInput);
                    workItem.PowerShell = powerShell;
                    if (isGetListAsync && workItem.AsyncGetListContext.PsObjectCollection == null)
                    {
                        psDataCollection = new List <PSObject>(DDIHelper.GetListDefaultResultSize * 2);
                        workItem.AsyncGetListContext.PsObjectCollection = psDataCollection;
                    }
                }
                if (powerShell != null)
                {
                    powerShell.Streams.Progress.DataAdded += delegate(object sender, DataAddedEventArgs e)
                    {
                        ProgressRecord progressRecord = ((PSDataCollection <ProgressRecord>)sender)[e.Index];
                        if (isGetListAsync)
                        {
                            AsyncServiceManager.OnProgress(workItem, progressRecord);
                            return;
                        }
                        List <ErrorRecord> list = new List <ErrorRecord>();
                        if (AsyncServiceManager.IsCurrentWorkBulkEdit())
                        {
                            Collection <ErrorRecord> collection = powerShell.Streams.Error.ReadAll();
                            foreach (ErrorRecord errorRecord in collection)
                            {
                                list.Add(new ErrorRecord(errorRecord));
                            }
                        }
                        string status = (progressRecord.RecordType != ProgressRecordType.Completed) ? progressRecord.StatusDescription : string.Empty;
                        ProgressReportEventArgs progressReportEventArgs = new ProgressReportEventArgs(list, progressRecord.PercentComplete, status);
                        DDIHelper.Trace("Async Progress Id: {0},  ProgressReportEventArgs: {1}", new object[]
                        {
                            workItem.Id,
                            progressReportEventArgs
                        });
                        activity.OnPSProgressReport(progressReportEventArgs);
                    };
                }
            }
        }
예제 #2
0
        private static PowerShellResults <O> InvokeCore <O>(PSCommand psCommand, RunspaceMediator runspaceMediator, IEnumerable pipelineInput, WebServiceParameters parameters, CmdletActivity activity, bool isGetListAsync)
        {
            Func <WarningRecord, string> func  = null;
            Func <Command, string>       func2 = null;

            PSCommandExtension.EnsureNoWriteTaskInGetRequest(psCommand, parameters);
            PowerShellResults <O> powerShellResults = new PowerShellResults <O>();

            ExTraceGlobals.EventLogTracer.TraceInformation <string, EcpTraceFormatter <PSCommand> >(0, 0L, "{0} tries to invoke {1}. For more details, refer to task trace", RbacPrincipal.Current.NameForEventLog, psCommand.GetTraceFormatter());
            using (RunspaceProxy runspaceProxy = new RunspaceProxy(runspaceMediator))
            {
                runspaceProxy.SetVariable("ConfirmPreference", "None");
                if (parameters != null)
                {
                    psCommand.AddParameters(parameters);
                }
                using (PowerShell powerShell = runspaceProxy.CreatePowerShell(psCommand))
                {
                    List <PSObject> list = null;
                    if (activity != null)
                    {
                        AsyncServiceManager.RegisterPowerShellToActivity(powerShell, activity, pipelineInput, out list, isGetListAsync);
                    }
                    else
                    {
                        AsyncServiceManager.RegisterPowerShell(powerShell);
                    }
                    int      requestLatency = 0;
                    DateTime utcNow         = DateTime.UtcNow;
                    try
                    {
                        TaskPerformanceRecord taskPerformanceRecord = new TaskPerformanceRecord(psCommand.GetCmdletName(), PSCommandExtension.powerShellLatencyDetectionContextFactory, EcpEventLogConstants.Tuple_EcpPowerShellInvoked, EcpEventLogConstants.Tuple_EcpPowerShellCompleted, EcpEventLogExtensions.EventLog, new IPerformanceDataProvider[]
                        {
                            PerformanceContext.Current,
                            RpcDataProvider.Instance,
                            TaskPerformanceData.CmdletInvoked,
                            TaskPerformanceData.BeginProcessingInvoked,
                            TaskPerformanceData.ProcessRecordInvoked,
                            TaskPerformanceData.EndProcessingInvoked,
                            EcpPerformanceData.PowerShellInvoke
                        });
                        try
                        {
                            using (EcpPerformanceData.PowerShellInvoke.StartRequestTimer())
                            {
                                if (list == null)
                                {
                                    powerShellResults.Output = powerShell.Invoke <O>(pipelineInput).ToArray <O>();
                                }
                                else
                                {
                                    powerShell.Invoke <PSObject>(pipelineInput, list);
                                    powerShellResults.Output = Array <O> .Empty;
                                }
                            }
                        }
                        finally
                        {
                            requestLatency = (int)taskPerformanceRecord.Stop().TotalMilliseconds;
                            IDisposable disposable2 = taskPerformanceRecord;
                            if (disposable2 != null)
                            {
                                disposable2.Dispose();
                            }
                        }
                        List <ErrorRecord> list2 = new List <ErrorRecord>();
                        bool flag = false;
                        foreach (ErrorRecord errorRecord in powerShell.Streams.Error)
                        {
                            if (!flag)
                            {
                                flag = PSCommandExtension.TryPatchShouldContinueException(errorRecord, psCommand, parameters);
                            }
                            list2.Add(new ErrorRecord(errorRecord));
                        }
                        powerShellResults.ErrorRecords = list2.ToArray();
                        PowerShellResults           powerShellResults2 = powerShellResults;
                        IEnumerable <WarningRecord> warning            = powerShell.Streams.Warning;
                        if (func == null)
                        {
                            func = ((WarningRecord warningRecord) => warningRecord.Message);
                        }
                        powerShellResults2.Warnings = warning.Select(func).ToArray <string>();
                    }
                    catch (RuntimeException ex)
                    {
                        PSCommandExtension.TryPatchShouldContinueException(ex.ErrorRecord, psCommand, parameters);
                        ErrorRecord errorRecord2;
                        if (ex.ErrorRecord != null && !(ex is ParameterBindingException))
                        {
                            errorRecord2 = new ErrorRecord(ex.ErrorRecord);
                        }
                        else
                        {
                            errorRecord2 = new ErrorRecord(ex);
                        }
                        powerShellResults.ErrorRecords = new ErrorRecord[]
                        {
                            errorRecord2
                        };
                    }
                    finally
                    {
                        string         text     = HttpContext.Current.Request.QueryString["reqId"];
                        ServerLogEvent logEvent = new ServerLogEvent(psCommand, pipelineInput, requestLatency, string.IsNullOrEmpty(text) ? string.Empty : text, (powerShellResults.ErrorRecords != null && powerShellResults.ErrorRecords.Length > 0) ? powerShellResults.ErrorRecords.ToLogString() : string.Empty, (powerShellResults.Output != null) ? powerShellResults.Output.Length : 0);
                        ServerLogger.Instance.LogEvent(logEvent);
                    }
                    PowerShellResults     powerShellResults3 = powerShellResults;
                    IEnumerable <Command> commands           = psCommand.Commands;
                    if (func2 == null)
                    {
                        func2 = ((Command cmd) => cmd.CommandText);
                    }
                    powerShellResults3.Cmdlets = commands.Select(func2).ToArray <string>();
                    if (powerShellResults.ErrorRecords.Length > 0)
                    {
                        ExTraceGlobals.EventLogTracer.TraceError <string, EcpTraceFormatter <PSCommand>, EcpTraceFormatter <ErrorRecord[]> >(0, 0L, "{0} invoked {1} and encountered errors: {2}", RbacPrincipal.Current.NameForEventLog, psCommand.GetTraceFormatter(), powerShellResults.ErrorRecords.GetTraceFormatter());
                    }
                    CmdExecuteInfo cmdExecuteInfo = CmdletLogger.CaculateLogAndSaveToContext(powerShell, utcNow, powerShellResults.ErrorRecords);
                    if (cmdExecuteInfo != null)
                    {
                        powerShellResults.CmdletLogInfo = new CmdExecuteInfo[]
                        {
                            cmdExecuteInfo
                        };
                    }
                }
            }
            return(powerShellResults);
        }
예제 #3
0
 public static PowerShellResults <O> Invoke <O>(this PSCommand psCommand, RunspaceMediator runspaceMediator, IEnumerable pipelineInput, WebServiceParameters parameters, CmdletActivity activity, bool isGetListAsync)
 {
     return(PSCommandExtension.InvokeCore <O>(psCommand, runspaceMediator, pipelineInput, parameters, activity, isGetListAsync));
 }