1
1
using System . Text ;
2
+ using System . Text . RegularExpressions ;
2
3
using DynamicPowerShellApi . Model ;
3
4
4
5
namespace DynamicPowerShellApi
@@ -105,6 +106,7 @@ public Task<PowershellReturn> ExecuteAsync(string filename, string snapin, IList
105
106
106
107
// invoke execution on the pipeline (collecting output)
107
108
Collection < PSObject > psOutput = powerShellInstance . Invoke ( ) ;
109
+ string sMessage = psOutput . LastOrDefault ( ) != null ? Regex . Replace ( psOutput . LastOrDefault ( ) . ToString ( ) , @"[^\u0000-\u007F]" , string . Empty ) : String . Empty ;
108
110
109
111
DynamicPowershellApiEvents . Raise . PowerShellScriptFinalised ( "The powershell has completed - anlaysing results now" ) ;
110
112
@@ -118,13 +120,35 @@ public Task<PowershellReturn> ExecuteAsync(string filename, string snapin, IList
118
120
// do something with the items found.
119
121
Console . WriteLine ( "PowerShell script crashed with errors:" ) ;
120
122
sb . Append ( "PowerShell script crashed with errors:" + Environment . NewLine ) ;
123
+ sb . Append ( String . Format ( "{0}" , sMessage ) ) ;
121
124
122
125
var errors = powerShellInstance . Streams . Error . ReadAll ( ) ;
123
126
if ( errors != null )
124
127
{
125
128
foreach ( var error in errors )
126
129
{
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 ) ;
128
152
129
153
if ( error . Exception != null )
130
154
{
@@ -136,6 +160,10 @@ public Task<PowershellReturn> ExecuteAsync(string filename, string snapin, IList
136
160
sb . Append ( String . Format ( "Error {0}" , error . ScriptStackTrace ) ) ;
137
161
}
138
162
}
163
+ else
164
+ {
165
+ sb . Append ( sMessage ) ;
166
+ }
139
167
140
168
Console . WriteLine ( "Creating a new PowershellReturn object" ) ;
141
169
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
156
184
var psGood = new PowershellReturn
157
185
{
158
186
PowerShellReturnedValidData = true ,
159
- ActualPowerShellData = lastMessage == null ? string . Empty : lastMessage . ToString ( )
187
+ ActualPowerShellData = sMessage
160
188
} ;
161
189
162
190
DynamicPowershellApiEvents . Raise . PowerShellScriptFinalised ( String . Format ( "The powershell returned the following {0}" , psGood . ActualPowerShellData ) ) ;
163
191
164
192
return Task . FromResult ( psGood ) ;
165
193
}
166
194
}
167
- catch
195
+ catch ( Exception runnerException )
168
196
{
197
+ DynamicPowershellApiEvents . Raise . UnhandledException ( runnerException . Message , runnerException . StackTrace ) ;
169
198
throw ;
170
199
}
171
200
}
0 commit comments