Example #1
0
        internal static SHiPSParameters GetSHiPSParameters(this IProviderContext context)
        {
            var myInvocationInfo = GetMyInvocationInfo(context.CmdletProvider);
            var verbose          = false;
            var debug            = false;
            var force            = false;

            if (myInvocationInfo == null)
            {
                return(new SHiPSParameters());
            }

            var boundParameters = myInvocationInfo.BoundParameters;

            if (boundParameters.Any())
            {
                object argument;

                if (boundParameters.TryGetValue("Verbose", out argument))
                {
                    verbose = ((SwitchParameter)argument).IsPresent;
                }
                if (boundParameters.TryGetValue("Debug", out argument))
                {
                    debug = ((SwitchParameter)argument).IsPresent;
                }
                if (boundParameters.TryGetValue("Force", out argument))
                {
                    force = ((SwitchParameter)argument).IsPresent;
                }
            }

            var parameterBag = new SHiPSParameters()
            {
                Verbose         = verbose,
                Debug           = debug,
                Force           = force,
                BoundParameters = myInvocationInfo.BoundParameters
            };

            return(parameterBag);
        }
Example #2
0
        internal static ICollection <object> CallPowerShellScript(
            SHiPSBase node,
            System.Management.Automation.PowerShell powerShell,
            SHiPSParameters parameters,
            string script,
            EventHandler <DataAddedEventArgs> outputAction,
            EventHandler <DataAddedEventArgs> errorAction,
            params string[] args)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            try
            {
                powerShell.Clear();

                var input = new PSDataCollection <object>();
                input.Complete();

                var output = new PSDataCollection <object>();

                if (outputAction != null)
                {
                    output.DataAdded += outputAction;
                }

                //register events
                if (errorAction != null)
                {
                    powerShell.Streams.Error.DataAdded += errorAction;
                }

                // Calling the following throws 'Unable to cast object of type 'System.Management.Automation.Language.FunctionMemberAst' to
                // type 'System.Management.Automation.Language.FunctionDefinitionAst'.
                //output = node.GetChildItem();

                //make script block
                powerShell.AddScript(script);
                powerShell.AddParameter("object", node);

                if (args != null && args.Any())
                {
                    for (int i = 0; i < args.Length; i++)
                    {
                        powerShell.AddParameter(("p" + i), args[i]);
                    }
                }

                if (parameters != null)
                {
                    if (parameters.Debug)
                    {
                        powerShell.AddParameter("debug");
                    }
                    if (parameters.Verbose)
                    {
                        powerShell.AddParameter("verbose");
                    }

                    node.SHiPSProviderContext.BoundParameters = parameters.BoundParameters;
                }

                powerShell.Invoke(null, output, new PSInvocationSettings());

                return(output.Count == 0 ? null : output);
            }
            finally
            {
                powerShell.Streams.Error.DataAdded -= errorAction;
            }
        }
Example #3
0
        internal static ICollection <object> CallPowerShellScript(
            SHiPSDirectory node,
            IProviderContext context,
            System.Management.Automation.PowerShell powerShell,
            SHiPSParameters parameters,
            string methodName,
            EventHandler <DataAddedEventArgs> outputAction,
            EventHandler <DataAddedEventArgs> errorAction)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            try
            {
                powerShell.Clear();

                var input = new PSDataCollection <object>();
                input.Complete();

                var output = new PSDataCollection <object>();

                if (outputAction != null)
                {
                    output.DataAdded += outputAction;
                }

                //register events
                if (errorAction != null)
                {
                    powerShell.Streams.Error.DataAdded += errorAction;
                }

                // Calling the following throws 'Unable to cast object of type 'System.Management.Automation.Language.FunctionMemberAst' to
                // type 'System.Management.Automation.Language.FunctionDefinitionAst'.
                //output = node.GetChildItem();

                //make script block
                powerShell.AddScript("[CmdletBinding()] param([object]$object)  $object.{0}()".StringFormat(methodName));
                powerShell.AddParameter("object", node);


                if (parameters != null)
                {
                    if (parameters.Debug)
                    {
                        powerShell.AddParameter("debug");
                    }
                    if (parameters.Verbose)
                    {
                        powerShell.AddParameter("verbose");
                    }
                }

                powerShell.Invoke(null, output, new PSInvocationSettings());

                return(output.Count == 0 ? null : output);
            }
            finally
            {
                powerShell.Streams.Error.DataAdded -= errorAction;
            }
        }