|
11 | 11 | using System.Threading.Tasks; |
12 | 12 | using System.Collections.Generic; |
13 | 13 | using System.Management.Automation; |
| 14 | +using System.ComponentModel; |
14 | 15 |
|
15 | 16 | namespace DataBooster.PSWebApi |
16 | 17 | { |
@@ -42,15 +43,22 @@ public async static Task<HttpResponseMessage> InvokePowerShellAsync(this ApiCont |
42 | 43 | if (!string.IsNullOrWhiteSpace(converter.ConversionCmdlet)) |
43 | 44 | ps.AddCommand(converter.ConversionCmdlet, true).Commands.AddParameters(converter.CmdletParameters); |
44 | 45 |
|
45 | | - string stringResult = GetPsResult(await ps.InvokeAsync(cancellationToken).ConfigureAwait(false), encoding); |
| 46 | + try |
| 47 | + { |
| 48 | + string stringResult = GetPsResult(await ps.InvokeAsync(cancellationToken).ConfigureAwait(false), encoding); |
46 | 49 |
|
47 | | - ps.CheckErrors(cancellationToken); |
| 50 | + ps.CheckErrors(cancellationToken); |
48 | 51 |
|
49 | | - StringContent responseContent = new StringContent(stringResult, encoding, contentNegotiator.NegotiatedMediaType.MediaType); |
| 52 | + StringContent responseContent = new StringContent(stringResult, encoding, contentNegotiator.NegotiatedMediaType.MediaType); |
50 | 53 |
|
51 | | - responseContent.Headers.SetContentHeader(ps.Streams); |
| 54 | + responseContent.Headers.SetContentHeader(ps.Streams); |
52 | 55 |
|
53 | | - return new HttpResponseMessage(string.IsNullOrEmpty(stringResult) ? HttpStatusCode.NoContent : HttpStatusCode.OK) { Content = responseContent }; |
| 56 | + return new HttpResponseMessage(string.IsNullOrEmpty(stringResult) ? HttpStatusCode.NoContent : HttpStatusCode.OK) { Content = responseContent }; |
| 57 | + } |
| 58 | + catch (CommandNotFoundException) |
| 59 | + { |
| 60 | + return new HttpResponseMessage(HttpStatusCode.NotFound); |
| 61 | + } |
54 | 62 | } |
55 | 63 | } |
56 | 64 |
|
@@ -91,22 +99,36 @@ public static async Task<HttpResponseMessage> InvokeCmdAsync(this ApiController |
91 | 99 |
|
92 | 100 | using (CmdProcess cmd = new CmdProcess(scriptPath, arguments) { OutputEncoding = encoding }) |
93 | 101 | { |
94 | | - int exitCode = await cmd.ExecuteAsync(cancellationToken).ConfigureAwait(false); |
95 | | - string responseString = cmd.GetStandardError(); |
96 | | - HttpStatusCode httpStatusCode; |
97 | | - |
98 | | - if (exitCode == 0 && string.IsNullOrEmpty(responseString)) |
| 102 | + try |
99 | 103 | { |
100 | | - responseString = cmd.GetStandardOutput(); |
101 | | - httpStatusCode = string.IsNullOrEmpty(responseString) ? HttpStatusCode.NoContent : HttpStatusCode.OK; |
| 104 | + int exitCode = await cmd.ExecuteAsync(cancellationToken).ConfigureAwait(false); |
| 105 | + string responseString = cmd.GetStandardError(); |
| 106 | + HttpStatusCode httpStatusCode; |
| 107 | + |
| 108 | + if (exitCode == 0 && string.IsNullOrEmpty(responseString)) |
| 109 | + { |
| 110 | + responseString = cmd.GetStandardOutput(); |
| 111 | + httpStatusCode = string.IsNullOrEmpty(responseString) ? HttpStatusCode.NoContent : HttpStatusCode.OK; |
| 112 | + } |
| 113 | + else |
| 114 | + httpStatusCode = HttpStatusCode.InternalServerError; |
| 115 | + |
| 116 | + StringContent responseContent = new StringContent(responseString, encoding, contentNegotiator.NegotiatedMediaType.MediaType); |
| 117 | + responseContent.Headers.Add("Exit-Code", exitCode.ToString()); |
| 118 | + |
| 119 | + return new HttpResponseMessage(httpStatusCode) { Content = responseContent }; |
| 120 | + } |
| 121 | + catch (Win32Exception e) |
| 122 | + { |
| 123 | + switch (e.NativeErrorCode) |
| 124 | + { |
| 125 | + case 2: // ERROR_FILE_NOT_FOUND |
| 126 | + case 267: // ERROR_DIRECTORY |
| 127 | + return new HttpResponseMessage(HttpStatusCode.NotFound); |
| 128 | + default: |
| 129 | + throw; |
| 130 | + } |
102 | 131 | } |
103 | | - else |
104 | | - httpStatusCode = HttpStatusCode.InternalServerError; |
105 | | - |
106 | | - StringContent responseContent = new StringContent(responseString, encoding, contentNegotiator.NegotiatedMediaType.MediaType); |
107 | | - responseContent.Headers.Add("Exit-Code", exitCode.ToString()); |
108 | | - |
109 | | - return new HttpResponseMessage(httpStatusCode) { Content = responseContent }; |
110 | 132 | } |
111 | 133 | } |
112 | 134 |
|
|
0 commit comments