/// <summary> /// Initializes a new instance of the PSConfiguration class. /// </summary> /// <param name="supportJson">true to support JSON response; otherwise, false.</param> /// <param name="supportXml">true to support XML response; otherwise, false.</param> /// <param name="supportCsv">true to support CSV response; otherwise, false.</param> /// <param name="supportHtml">true to support HTML response; otherwise, false.</param> /// <param name="supportText">true to support text response; otherwise, false.</param> /// <param name="supportString">true to support string response; otherwise, false.</param> public PSConfiguration(bool supportJson = true, bool supportXml = true, bool supportCsv = true, bool supportHtml = true, bool supportText = true, bool supportString = true) { _supportedConverters = new List <PSConverterRegistry>(); SupportedConverters = new Collection <PSConverterRegistry>(_supportedConverters); if (supportJson) { _jsonConverter = new PSConverterRegistry(new string[] { "application/json", "text/json" }, "json", "ConvertTo-Json"); _supportedConverters.Add(_jsonConverter); } if (supportXml) { _xmlConverter = new PSConverterRegistry(new string[] { "application/xml", "text/xml" }, "xml", "ConvertTo-Xml"); _supportedConverters.Add(_xmlConverter); } if (supportCsv) { _csvConverter = new PSConverterRegistry(new string[] { "text/csv", "application/csv" }, "csv", "ConvertTo-Csv"); _supportedConverters.Add(_csvConverter); } if (supportHtml) { _htmlConverter = new PSConverterRegistry(new string[] { "text/html", "application/xhtml" }, "html", "ConvertTo-Html"); _supportedConverters.Add(_htmlConverter); } if (supportText) { _textConverter = new PSConverterRegistry(new string[] { "text/plain" }, "string", "Out-String"); _supportedConverters.Add(_textConverter); } if (supportString) { _stringConverter = new PSConverterRegistry(new string[] { "application/string" }, "str", string.Empty); // .ToString() _supportedConverters.Add(_stringConverter); } _nullConverter = new PSConverterRegistry(new string[] { "application/null" }, "null", "Out-Null"); _supportedConverters.Add(_nullConverter); }
/// <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)); } } }
public PSContentNegotiator(HttpRequestMessage request) { HttpConfiguration configuration = request.GetConfiguration(); IContentNegotiator contentNegotiator = configuration.Services.GetContentNegotiator(); IEnumerable <MediaTypeFormatter> formatters = configuration.Formatters; ContentNegotiationResult contentNegotiationResult = contentNegotiator.Negotiate(typeof(string), request, formatters); _negotiatedMediaType = contentNegotiationResult.MediaType; MediaTypeFormatter resultformatter = contentNegotiationResult.Formatter; if (resultformatter != null) { _negotiatedEncoding = _psDefaultContentNegotiator.NegotiateEncoding(request, resultformatter); } PSMediaTypeFormatter negotiatedMediaTypeFormatter = resultformatter as PSMediaTypeFormatter; if (negotiatedMediaTypeFormatter != null) { _negotiatedPsConverter = negotiatedMediaTypeFormatter.Configuration.Lookup(_negotiatedMediaType); } }