public System.Collections.ObjectModel.Collection<System.Management.Automation.PSObject> RunCommand(CmdletParameterSetBase paramSet, out System.Collections.ObjectModel.Collection<object> errors)
        {
            errors = new System.Collections.ObjectModel.Collection<object>();
            System.Collections.ObjectModel.Collection<System.Management.Automation.PSObject> results = null;

            System.Text.StringBuilder cmdString = new System.Text.StringBuilder(paramSet.Cmdlet);
            System.Text.StringBuilder parameterValueClearScriptBuilder = new System.Text.StringBuilder();
            foreach (CmdletParameterSwitchValuePair parameter in paramSet.Parameters)
            {
                if (parameter.Value != null && parameter.Value.IsSet)
                {
                    if (parameter.Value is CmdletSwitchParameter)
                    {
                        cmdString.AppendFormat(" -{0}", parameter.ParameterSwitch);
                    }
                    else
                    {
                        string parameterSting = System.String.Empty, parameterGuidString;
                        switch (parameter.Value.ParameterValueType)
                        {
                            case ParameterValueTypeEnum.String:
                                parameterSting = (string)parameter.Value.ValueObject;
                                break;
                            case ParameterValueTypeEnum.Object:
                                parameterGuidString = System.Guid.NewGuid().ToString("N");
                                parameterSting = System.String.Format("${0}", parameterGuidString);
                                runspace.SessionStateProxy.SetVariable(parameterGuidString, parameter.Value.ValueObject);
                                parameterValueClearScriptBuilder.AppendLine(System.String.Format("{0} = $NULL", parameterSting));
                                break;
                            default:
                                System.Diagnostics.Debug.Assert(false, "Parameter object is not set properly. This error should never be seen.");
                                break;
                        }
                        cmdString.AppendFormat(" -{0} {1}", parameter.ParameterSwitch, parameterSting);
                    }
                }
                try
                {
                    using (System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline(cmdString.ToString()))
                    {
                        results = pipeline.Invoke();
                        errors = pipeline.Error.ReadToEnd();
                    }
                }
                finally
                {
                    using (System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline(parameterValueClearScriptBuilder.ToString()))
                    {
                        pipeline.Invoke();
                    }
                }
            }
            return results;
        }
示例#2
0
        public void RunCmdletTest()
        {
            //List<string> getlist = new List<string>();
            //getlist.Add("Set-ExecutionPolicy RemoteSigned");
            //var s= ExecuteShellScript(getlist, null);

            using (CmdletProcessor cp = new CmdletProcessor())
            {
                string pspath = System.AppDomain.CurrentDomain.BaseDirectory + "\\PSWrapper\\get-process.ps1";
                var    par    = new CmdletParameterSetBase(pspath).AddParameter(new CmdletParameterSwitchValuePair("processName", new CmdletStringParameter("qq")));
                var    result = cp.RunCmdlet <PSObject>(par);
            }
        }
        public void RunCmdletTest()
        {
            //List<string> getlist = new List<string>();
            //getlist.Add("Set-ExecutionPolicy RemoteSigned");
            //var s= ExecuteShellScript(getlist, null);

            using (CmdletProcessor cp = new CmdletProcessor())
            {
                string pspath = System.AppDomain.CurrentDomain.BaseDirectory + "\\PSWrapper\\get-process.ps1";
                var par = new CmdletParameterSetBase(pspath).AddParameter(new CmdletParameterSwitchValuePair("processName", new CmdletStringParameter("qq")));
                var result = cp.RunCmdlet<PSObject>(par);


            }
        }
示例#4
0
 public T[] RunCmdlet <T>(CmdletParameterSetBase paramSet)
 {
     System.Collections.ObjectModel.Collection <System.Management.Automation.PSObject> results = this.ProcessCmdletCall(paramSet);
     // powershell sometimes returns null PSObject objects,
     // so we can't rely on the .Count property of the results
     // collection for an accurate count of the results and we
     // need to count them ourselves
     System.Collections.Generic.List <T> retVal = new System.Collections.Generic.List <T>(results.Count); // can never be larger than the results colleciton
     foreach (System.Management.Automation.PSObject result in results)
     {
         if (result != null && result.BaseObject is T)
         {
             retVal.Add((T)result.BaseObject);
         }
     }
     return(retVal.ToArray());
 }
示例#5
0
        private ICmdletProcessor GetCmdletProcessor(CmdletParameterSetBase paramSet)
        {
            ICmdletProcessor retVal = this.cmdletParameterSetProcessor;

            if (paramSet.Parameters != null)
            {
                foreach (CmdletParameterSwitchValuePair parameter in paramSet.Parameters)
                {
                    if (parameter.Value.IsSet &&
                        parameter.Value.ParameterValueType == ParameterValueTypeEnum.String)
                    {
                        retVal = this.cmdletStringProcessor;
                        break;
                    }
                }
            }
            return(retVal);
        }
 public System.Collections.ObjectModel.Collection<System.Management.Automation.PSObject> RunCommand(CmdletParameterSetBase paramSet, out System.Collections.ObjectModel.Collection<object> errors)
 {
     errors = new System.Collections.ObjectModel.Collection<object>();
     System.Collections.ObjectModel.Collection<System.Management.Automation.PSObject> results;
     using (System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline())
     {
         System.Management.Automation.Runspaces.Command cmd = new System.Management.Automation.Runspaces.Command(paramSet.Cmdlet);
         if (paramSet.Parameters != null)
         {
             foreach (CmdletParameterSwitchValuePair parameter in paramSet.Parameters)
             {
                 if (parameter.Value != null && parameter.Value.IsSet)
                 {
                     cmd.Parameters.Add(parameter.ParameterSwitch, parameter.Value.ValueObject);
                 }
             }
             pipeline.Commands.Add(cmd);
         }
         results = pipeline.Invoke();
         errors = pipeline.Error.ReadToEnd();
         //try
         //{
         //    results = pipeline.Invoke();
         //    return results;
         //}
         //catch (Exception Ex)
         //{
         //    errors = pipeline.Error.ReadToEnd();
             
         //}
         //return null;
     
         
     }
     return results;
     
 }
示例#7
0
 private void HandleErrors(System.Collections.ObjectModel.Collection <object> errors, CmdletParameterSetBase paramSet)
 {
     if (errors.Count > 0)
     {
         System.Text.StringBuilder sb = new System.Text.StringBuilder();
         sb.AppendLine(System.String.Format("Errors were reported by powershell while running the {0} cmdlet. Check the exception Data property for more details. A list of the errors follows:\n", paramSet.Cmdlet));
         for (int i = 0; i < errors.Count; i++)
         {
             sb.AppendLine("\t" + errors[i].ToString());
         }
         Log.LogFactory.ExceptionLog.Error(string.Format("Cmdlet:{0}{1}error:{2}", paramSet.Cmdlet, System.Environment.NewLine, sb.ToString()));
         throw new PALCmdletInvocationException(sb.ToString(), errors);
     }
 }
示例#8
0
        private System.Collections.ObjectModel.Collection <System.Management.Automation.PSObject> ProcessCmdletCall(CmdletParameterSetBase paramSet)
        {
            System.Collections.ObjectModel.Collection <object> errors;
            ICmdletProcessor iCmdletProcessor = GetCmdletProcessor(paramSet);

            System.Collections.ObjectModel.Collection <System.Management.Automation.PSObject> results = iCmdletProcessor.RunCommand(paramSet, out errors);
            if (errors.Count > 0)
            {
                HandleErrors(errors, paramSet);
            }
            return(results);
        }
        public System.Collections.ObjectModel.Collection <System.Management.Automation.PSObject> RunCommand(CmdletParameterSetBase paramSet, out System.Collections.ObjectModel.Collection <object> errors)
        {
            errors = new System.Collections.ObjectModel.Collection <object>();
            System.Collections.ObjectModel.Collection <System.Management.Automation.PSObject> results = null;

            System.Text.StringBuilder cmdString = new System.Text.StringBuilder(paramSet.Cmdlet);
            System.Text.StringBuilder parameterValueClearScriptBuilder = new System.Text.StringBuilder();
            foreach (CmdletParameterSwitchValuePair parameter in paramSet.Parameters)
            {
                if (parameter.Value != null && parameter.Value.IsSet)
                {
                    if (parameter.Value is CmdletSwitchParameter)
                    {
                        cmdString.AppendFormat(" -{0}", parameter.ParameterSwitch);
                    }
                    else
                    {
                        string parameterSting = System.String.Empty, parameterGuidString;
                        switch (parameter.Value.ParameterValueType)
                        {
                        case ParameterValueTypeEnum.String:
                            parameterSting = (string)parameter.Value.ValueObject;
                            break;

                        case ParameterValueTypeEnum.Object:
                            parameterGuidString = System.Guid.NewGuid().ToString("N");
                            parameterSting      = System.String.Format("${0}", parameterGuidString);
                            runspace.SessionStateProxy.SetVariable(parameterGuidString, parameter.Value.ValueObject);
                            parameterValueClearScriptBuilder.AppendLine(System.String.Format("{0} = $NULL", parameterSting));
                            break;

                        default:
                            System.Diagnostics.Debug.Assert(false, "Parameter object is not set properly. This error should never be seen.");
                            break;
                        }
                        cmdString.AppendFormat(" -{0} {1}", parameter.ParameterSwitch, parameterSting);
                    }
                }
                try
                {
                    using (System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline(cmdString.ToString()))
                    {
                        results = pipeline.Invoke();
                        errors  = pipeline.Error.ReadToEnd();
                    }
                }
                finally
                {
                    using (System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline(parameterValueClearScriptBuilder.ToString()))
                    {
                        pipeline.Invoke();
                    }
                }
            }
            return(results);
        }