Skip to content

Commit cee3c90

Browse files
committed
2 parents 38f3827 + 33a2ad9 commit cee3c90

File tree

7 files changed

+42
-13
lines changed

7 files changed

+42
-13
lines changed

Controllers/GenericController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public async Task<HttpResponseMessage> ProcessRequestAsync()
169169
}
170170
catch (Exception ex)
171171
{
172-
DynamicPowershellApiEvents.Raise.UnhandledException(ex.Message, ex.StackTrace);
172+
DynamicPowershellApiEvents.Raise.UnhandledException(ex.Message, ex.StackTrace ?? String.Empty);
173173

174174
return new HttpResponseMessage
175175
{

DynamicPowerShellAPI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<NuGetPackageImportStamp>f44a79db</NuGetPackageImportStamp>
1919
</PropertyGroup>
2020
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
21-
<PlatformTarget>AnyCPU</PlatformTarget>
21+
<PlatformTarget>x64</PlatformTarget>
2222
<DebugSymbols>true</DebugSymbols>
2323
<DebugType>full</DebugType>
2424
<Optimize>false</Optimize>

DynamicPowerShellApi.Host/DynamicPowerShellApi.Host.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<RestorePackages>true</RestorePackages>
1717
</PropertyGroup>
1818
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
19-
<PlatformTarget>AnyCPU</PlatformTarget>
19+
<PlatformTarget>x64</PlatformTarget>
2020
<DebugSymbols>true</DebugSymbols>
2121
<DebugType>full</DebugType>
2222
<Optimize>false</Optimize>
@@ -96,9 +96,9 @@
9696
</ProjectReference>
9797
</ItemGroup>
9898
<ItemGroup>
99-
<Manifests Include="$(SolutionDir)ETW\*">
100-
<Display>true</Display>
101-
</Manifests>
99+
<Manifests Include="$(SolutionDir)ETW\*">
100+
<Display>true</Display>
101+
</Manifests>
102102
</ItemGroup>
103103
<Target Name="CopyETWManifests" BeforeTargets="BeforeBuild" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
104104
<Copy SourceFiles="@(Manifests)" DestinationFolder="$(OutputPath)ETW\" />

Owin/DynamicPowerShellApi.Owin.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<RestorePackages>true</RestorePackages>
1818
</PropertyGroup>
1919
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
20-
<PlatformTarget>AnyCPU</PlatformTarget>
20+
<PlatformTarget>x64</PlatformTarget>
2121
<DebugSymbols>true</DebugSymbols>
2222
<DebugType>full</DebugType>
2323
<Optimize>false</Optimize>

PowershellRunner.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Text;
2+
using System.Text.RegularExpressions;
23
using DynamicPowerShellApi.Model;
34

45
namespace DynamicPowerShellApi
@@ -105,6 +106,7 @@ public Task<PowershellReturn> ExecuteAsync(string filename, string snapin, IList
105106

106107
// invoke execution on the pipeline (collecting output)
107108
Collection<PSObject> psOutput = powerShellInstance.Invoke();
109+
string sMessage = psOutput.LastOrDefault() != null ? Regex.Replace(psOutput.LastOrDefault().ToString(), @"[^\u0000-\u007F]", string.Empty) : String.Empty;
108110

109111
DynamicPowershellApiEvents.Raise.PowerShellScriptFinalised("The powershell has completed - anlaysing results now");
110112

@@ -118,13 +120,35 @@ public Task<PowershellReturn> ExecuteAsync(string filename, string snapin, IList
118120
// do something with the items found.
119121
Console.WriteLine("PowerShell script crashed with errors:");
120122
sb.Append("PowerShell script crashed with errors:" + Environment.NewLine);
123+
sb.Append(String.Format("{0}", sMessage));
121124

122125
var errors = powerShellInstance.Streams.Error.ReadAll();
123126
if (errors != null)
124127
{
125128
foreach (var error in errors)
126129
{
127-
DynamicPowershellApiEvents.Raise.PowerShellError(error.ErrorDetails.Message, error.ScriptStackTrace, error.InvocationInfo.PSCommandPath, error.InvocationInfo.ScriptLineNumber);
130+
if (error.ErrorDetails == null )
131+
DynamicPowershellApiEvents.Raise.UnhandledException("error.ErrorDetails is null");
132+
133+
string errorDetails = error.ErrorDetails != null ? error.ErrorDetails.Message : String.Empty;
134+
string scriptStack = error.ScriptStackTrace ?? String.Empty;
135+
string commandPath = error.InvocationInfo.PSCommandPath ?? String.Empty;
136+
137+
if (error.ScriptStackTrace == null)
138+
DynamicPowershellApiEvents.Raise.UnhandledException("error.ScriptStackTrace is null");
139+
140+
if (error.InvocationInfo == null)
141+
DynamicPowershellApiEvents.Raise.UnhandledException("error.InvocationInfo is null");
142+
else
143+
{
144+
if (error.InvocationInfo.PSCommandPath == null)
145+
DynamicPowershellApiEvents.Raise.UnhandledException("error.InvocationInfo.PSCommandPath is null");
146+
}
147+
148+
if (error.Exception == null)
149+
DynamicPowershellApiEvents.Raise.UnhandledException("error.Exception is null");
150+
151+
DynamicPowershellApiEvents.Raise.PowerShellError(errorDetails, scriptStack, commandPath, error.InvocationInfo.ScriptLineNumber);
128152

129153
if (error.Exception != null)
130154
{
@@ -136,6 +160,10 @@ public Task<PowershellReturn> ExecuteAsync(string filename, string snapin, IList
136160
sb.Append(String.Format("Error {0}", error.ScriptStackTrace));
137161
}
138162
}
163+
else
164+
{
165+
sb.Append(sMessage);
166+
}
139167

140168
Console.WriteLine("Creating a new PowershellReturn object");
141169
DynamicPowershellApiEvents.Raise.PowerShellScriptFinalised(String.Format("An error was rasied {0}", sb.ToString()));
@@ -156,16 +184,17 @@ public Task<PowershellReturn> ExecuteAsync(string filename, string snapin, IList
156184
var psGood = new PowershellReturn
157185
{
158186
PowerShellReturnedValidData = true,
159-
ActualPowerShellData = lastMessage == null ? string.Empty : lastMessage.ToString()
187+
ActualPowerShellData = sMessage
160188
};
161189

162190
DynamicPowershellApiEvents.Raise.PowerShellScriptFinalised(String.Format("The powershell returned the following {0}", psGood.ActualPowerShellData));
163191

164192
return Task.FromResult(psGood);
165193
}
166194
}
167-
catch
195+
catch (Exception runnerException)
168196
{
197+
DynamicPowershellApiEvents.Raise.UnhandledException(runnerException.Message, runnerException.StackTrace);
169198
throw;
170199
}
171200
}

Settings.StyleCop

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@
498498
</Rules>
499499
<AnalyzerSettings>
500500
<StringProperty Name="SP2002_Mode">NotEmpty</StringProperty>
501-
<StringProperty Name="SP2101_Limit">150</StringProperty>
501+
<StringProperty Name="SP2101_Limit">200</StringProperty>
502502
</AnalyzerSettings>
503503
</Analyzer>
504504
</Analyzers>

Tests/DynamicPowerShellAPI.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
<SpecificVersion>False</SpecificVersion>
4545
<HintPath>..\packages\Autofac.3.5.2\lib\net40\Autofac.dll</HintPath>
4646
</Reference>
47-
<Reference Include="Microsoft.Diagnostics.Tracing.EventSource, Version=1.0.16.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
47+
<Reference Include="Microsoft.Diagnostics.Tracing.EventSource, Version=1.0.26.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
4848
<SpecificVersion>False</SpecificVersion>
49-
<HintPath>..\packages\Microsoft.Diagnostics.Tracing.EventSource.1.0.16\lib\net40\Microsoft.Diagnostics.Tracing.EventSource.dll</HintPath>
49+
<HintPath>..\packages\Microsoft.Diagnostics.Tracing.EventSource.Redist.1.0.26\lib\net40\Microsoft.Diagnostics.Tracing.EventSource.dll</HintPath>
5050
</Reference>
5151
<Reference Include="Moq">
5252
<HintPath>..\packages\Moq.4.2.1409.1722\lib\net40\Moq.dll</HintPath>

0 commit comments

Comments
 (0)