Esempio n. 1
0
 public VerifyRule(string commandName,string argumentName,string argumentValue,PowershellType pstype)
 {
     this.commandName = commandName;
     this.argumentName = argumentName;
     this.argumentValue = argumentValue;
     this.pstype = pstype;
     matches = new List<MatchProperty>();
 }
 /// <summary>
 /// Executes arbitary commands with Parameters
 /// </summary>
 public NameValueCollection ExecuteCommand(string CommandName, NameValueCollection Parameters,PowershellType pstype, ref string ErrorOutput)
 {
     PowerShell powershell = this.MSOLPowershell;
     if (pstype == PowershellType.Live)
         powershell = this.LivePowershell;
     if (powershell == null)
         return null;
     PSCommand command = new PSCommand();
     ErrorOutput = "";
     NameValueCollection output = new NameValueCollection();
     command.AddCommand(CommandName);
     foreach(string key in Parameters)
         command.AddParameter(key,Parameters[key]);
     powershell.Commands = command;
     try
     {
         foreach (PSObject psobject in powershell.Invoke())
         {
             if (psobject != null)
             {
                 foreach (PSPropertyInfo propinfo in psobject.Properties)
                 {
                     if (propinfo.Value != null)
                     {
                         // turn strings into formatted {lists}
                         if (propinfo.Value.GetType().IsGenericType)
                         {
                             if (propinfo.Value.GetType().GetGenericTypeDefinition() == typeof(List<>))
                             {
                                 String list_output = "{";
                                 System.Collections.IList proplist = (System.Collections.IList)propinfo.Value;
                                 foreach (object obj in proplist)
                                 {
                                     list_output += PowerShellObjToString(obj) + ",";
                                 }
                                 if (list_output.EndsWith(","))
                                     list_output = list_output.Remove(list_output.Length - 1);
                                 list_output += "}";
                                 output.Add(propinfo.Name, list_output);
                             }
                             else
                                 output.Add(propinfo.Name, PowerShellObjToString(propinfo.Value));
                         }
                         else
                             output.Add(propinfo.Name, PowerShellObjToString(propinfo.Value));
                     }
                     else
                         output.Add(propinfo.Name, null);
                 }
             }
         }
         foreach (ErrorRecord err in powershell.Streams.Error.ReadAll())
         {
             ErrorOutput += "Error executing command:" + CommandName + " error:" + err.Exception.Message;
         }
         return output;
     }
     catch (Exception ex)
     {
         ErrorOutput += "Error executing command:" + CommandName + " error:" + ex.Message;
         return null;
     }
 }