Beispiel #1
0
 /// <summary>
 /// Constructor for the content stream.
 /// </summary>
 /// <param name="context">A provider context.</param>
 /// <param name="drive">SHiPS based provider drive.</param>
 /// <param name="node">An object that is corresponding to the current node path.</param>
 public ContentWriter(IProviderContext context, SHiPSDrive drive, SHiPSBase node)
 {
     _drive   = drive;
     _node    = node;
     _context = context;
     _stream  = new MemoryStream();
     _writer  = new StreamWriter(_stream);
 }
Beispiel #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;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Invokes a script block and updates the parent's children node list in the cached case.
        /// </summary>
        /// <param name="context">A ProviderContext object contains information that a PowerShell provider needs.</param>
        /// <param name="node">Node object that is corresponding to the current path.</param>
        /// <param name="drive">Current drive that a user is in use.</param>
        /// <param name="script">PowerShell script to be run.</param>
        /// <param name="errorHandler">Action for handling error cases.</param>
        /// <param name="args">Arguments passed into the script block.</param>
        /// <returns></returns>
        internal static ICollection <object> InvokeScriptBlock(
            IProviderContext context,
            SHiPSBase node,
            SHiPSDrive drive,
            string script,
            Action <string, IProviderContext, IEnumerable <ErrorRecord> > errorHandler,
            params string[] args)
        {
            try
            {
                var errors     = new ConcurrentBag <ErrorRecord>();
                var parameters = context?.GetSHiPSParameters();

                var results = CallPowerShellScript(
                    node,
                    drive.PowerShellInstance,
                    parameters,
                    script,
                    output_DataAdded,
                    (sender, e) => error_DataAdded(sender, e, errors),
                    args);


                if (errors.WhereNotNull().Any())
                {
                    if (context != null)
                    {
                        // report the error if there are any
                        errorHandler?.Invoke(node.Name, context, errors);
                        return(null);
                    }
                    else
                    {
                        // report the error if there are any
                        var error   = errors.FirstOrDefault();
                        var message = Environment.NewLine;
                        message += error.ErrorDetails == null ? error.Exception.Message : error.ErrorDetails.Message;
                        throw new InvalidDataException(message);
                    }
                }

                if (results == null || !results.Any())
                {
                    return(null);
                }

                if (context != null && node.UseCache)
                {
                    if (node.IsLeaf)
                    {
                        ProcessResultsWithCache(results, context, node.Parent, drive, addNodeOnly: true);
                    }
                    else
                    {
                        ProcessResultsWithCache(results, context, node as SHiPSDirectory, drive, addNodeOnly: true);
                    }
                }
                return(results);
            }
            finally
            {
                //stop the running script
                drive.PowerShellInstance.Stop();
            }
        }
Beispiel #4
0
 internal ContentHelper(SHiPSBase node, SHiPSDrive drive)
 {
     _node  = node;
     _drive = drive;
 }