Esempio n. 1
0
        /// <summary>
        /// Asynchronously invokes a PowerShell script by using the supplied input parameters.
        /// </summary>
        /// <param name="apiController">The ApiController. This is an extension method to ApiController, when you use instance method syntax to call this method, omit this parameter.</param>
        /// <param name="scriptPath">The fully qualified location of the PowerShell script to be run.</param>
        /// <param name="parameters">A set of parameters to the PowerShell script. The parameter names and values are taken from the keys and values of a collection.</param>
        /// <param name="cancellationToken">The cancellation token can be used to request that the operation be abandoned before completing the execution. Exceptions will be reported via the returned Task object.</param>
        /// <returns>A task representing the asynchronous operation.</returns>
        public async static Task <HttpResponseMessage> InvokePowerShellAsync(this ApiController apiController, string scriptPath, IEnumerable <KeyValuePair <string, object> > parameters, CancellationToken cancellationToken)
        {
            PSContentNegotiator contentNegotiator = new PSContentNegotiator(apiController.Request);
            PSConverterRegistry converter         = contentNegotiator.NegotiatedPsConverter;
            Encoding            encoding          = contentNegotiator.NegotiatedEncoding;

            if (converter == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotAcceptable);
            }

            using (PowerShell ps = PowerShell.Create())
            {
                ps.RunspacePool = _runspacePool;
                ps.AddCommand("Set-Location").AddParameter("LiteralPath", Path.GetDirectoryName(scriptPath));
                ps /*.AddStatement()*/.AddCommand(scriptPath, true).Commands.AddParameters(parameters);

                if (!string.IsNullOrWhiteSpace(converter.ConversionCmdlet))
                {
                    ps.AddCommand(converter.ConversionCmdlet, true).Commands.AddParameters(converter.CmdletParameters);
                }

                try
                {
                    string stringResult = GetPsResult(await ps.InvokeAsync(cancellationToken).ConfigureAwait(false), encoding);

                    ps.CheckErrors(cancellationToken);

                    StringContent responseContent = new StringContent(stringResult, encoding, contentNegotiator.NegotiatedMediaType.MediaType);

                    responseContent.Headers.SetContentHeader(ps.Streams);

                    return(new HttpResponseMessage(string.IsNullOrEmpty(stringResult) ? HttpStatusCode.NoContent : HttpStatusCode.OK)
                    {
                        Content = responseContent
                    });
                }
                catch (CommandNotFoundException)
                {
                    return(new HttpResponseMessage(HttpStatusCode.NotFound));
                }
            }
        }