Пример #1
0
        internal static void PropagateExchangePropertyContainer(ISessionState sessionState, RunspaceProxy runspace, bool propagateRBAC, bool propagateBudget, ADServerSettings adServerSettingsOverride, ExchangeRunspaceConfigurationSettings.ExchangeApplication application)
        {
            ExchangePropertyContainer propertyContainer         = ExchangePropertyContainer.GetPropertyContainer(sessionState);
            ExchangePropertyContainer exchangePropertyContainer = new ExchangePropertyContainer();

            if (propertyContainer.exchangeRunspaceConfiguration != null)
            {
                propertyContainer.exchangeRunspaceConfiguration.TryGetExecutingUserId(out exchangePropertyContainer.executingUserId);
                exchangePropertyContainer.executingUserOrganizationId = propertyContainer.exchangeRunspaceConfiguration.OrganizationId;
                if (propagateRBAC)
                {
                    exchangePropertyContainer.exchangeRunspaceConfiguration = propertyContainer.exchangeRunspaceConfiguration;
                }
                exchangePropertyContainer.propagatedClientAppId = application;
                if (propertyContainer.budget != null && propagateBudget)
                {
                    exchangePropertyContainer.budget = ExchangePropertyContainer.AcquirePowerShellBudget(propertyContainer.exchangeRunspaceConfiguration);
                }
            }
            else
            {
                exchangePropertyContainer.executingUserId             = propertyContainer.executingUserId;
                exchangePropertyContainer.executingUserOrganizationId = propertyContainer.executingUserOrganizationId;
            }
            exchangePropertyContainer.logEntries = propertyContainer.logEntries;
            exchangePropertyContainer.logEnabled = propertyContainer.logEnabled;
            if (adServerSettingsOverride == null)
            {
                exchangePropertyContainer.serverSettings = propertyContainer.serverSettings;
            }
            else
            {
                exchangePropertyContainer.serverSettings = adServerSettingsOverride;
            }
            runspace.SetVariable(ExchangePropertyContainer.ExchangePropertyContainerName, exchangePropertyContainer);
        }
Пример #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);
        }