예제 #1
0
 public void Dispose()
 {
     if (this.RemotePowershellSession != null)
     {
         this.RemotePowershellSession.Dispose();
         this.RemotePowershellSession = null;
     }
 }
        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);
        }
 private static string ToText(string type, object value)
 {
     if (value == null)
     {
         return("(null)");
     }
     if (value is PSObject)
     {
         return(RemotePowershellSession.ToText((PSObject)value));
     }
     return("\"" + value.ToString() + "\"");
 }
 private static string ToText(PSObject o)
 {
     if (o.ImmediateBaseObject is ArrayList)
     {
         return("[" + string.Join(", ", ((ArrayList)o.ImmediateBaseObject).ToArray().Select(delegate(object i)
         {
             if (i != null)
             {
                 return i.ToString();
             }
             return "(null)";
         })) + "]");
     }
     return("{ " + string.Join(", ", from p in o.Properties
                               orderby p.Name
                               select RemotePowershellSession.ToText(p)) + "}");
 }
예제 #5
0
 public PowerShellCommonSession(ILogger logger, string targetServer, PowershellConnectionType connectionType, PSCredential credentials)
 {
     this.RemotePowershellSession = new RemotePowershellSession(logger, targetServer, connectionType, true, new Func <string, bool>(this.ShouldInvokePowershellCommandHandler));
     this.RemotePowershellSession.Connect(credentials, Thread.CurrentThread.CurrentUICulture);
 }
 private static string ToText(PSPropertyInfo p)
 {
     return(string.Format("\"{0}\": {1}", p.Name, RemotePowershellSession.ToText(p.TypeNameOfValue, p.Value)));
 }
 private static string ToText(IEnumerable <PSObject> objects)
 {
     return("[" + string.Join(", ", from o in objects
                              select RemotePowershellSession.ToText(o)) + "]");
 }