コード例 #1
0
        public Collection <PSObject> RunCommand(string cmdlet, SessionParameters parameters, bool ignoreNotFoundErrors)
        {
            string data = null;
            string text = this.BuildCmdletInvocationForLogging(cmdlet, parameters);

            if (this.shouldInvokePowershellCommand(text))
            {
                this.CheckDisposed();
                using (PowerShell powerShell = PowerShell.Create())
                {
                    powerShell.Runspace = this.runspace;
                    powerShell.AddCommand(cmdlet);
                    if (parameters != null && parameters.Count > 0)
                    {
                        powerShell.AddParameters(parameters.ToDictionary());
                    }
                    ExDateTime now = ExDateTime.Now;
                    try
                    {
                        this.logger.LogInformation(HybridStrings.HybridInfoCmdletStart(this.connectionType.ToString(), text, string.Empty));
                        Collection <PSObject> collection = powerShell.Invoke();
                        if (powerShell.Streams.Error.Count > 0)
                        {
                            foreach (ErrorRecord errorRecord in powerShell.Streams.Error)
                            {
                                if (!errorRecord.CategoryInfo.Reason.Equals("ManagementObjectNotFoundException") || !ignoreNotFoundErrors)
                                {
                                    this.logger.LogError(errorRecord.Exception.ToString());
                                    throw errorRecord.Exception;
                                }
                            }
                        }
                        try
                        {
                            data = RemotePowershellSession.ToText(collection);
                        }
                        catch
                        {
                            data = "?";
                        }
                        return(collection);
                    }
                    catch (Exception innerException)
                    {
                        Exception ex = new Exception(HybridStrings.ErrorCmdletException(cmdlet).ToString(), innerException);
                        throw ex;
                    }
                    finally
                    {
                        TimeSpan t = ExDateTime.Now.Subtract(now);
                        this.totalProcessedTime += t;
                        this.LogInformationAndData(HybridStrings.HybridInfoCmdletEnd(this.connectionType.ToString(), cmdlet, t.TotalMilliseconds.ToString()), data);
                    }
                }
            }
            return(null);
        }