C
C#2y ago
saberune

❔ Catching more detailed errors for PowerShell in C# web application (.Net6)

I have a snippet of code here that does catch generic errors fine but I am having a hard time troubleshooting without more details about the error. The following helps catch the general error. I am wondering if anyone knows how to improve it so it can catch the full error: ... note: -ps is type Powershell - errorMsg is type string ps.Streams.Error.DataAdded += (object sender, DataAddedEventArgs e) => { errorMsg = ((PSDataCollection<ErrorRecord>)sender)[e.Index].ToString(); }; Thanks in advance!
5 Replies
jcotton42
jcotton422y ago
I mean Use the full object instead of just the stringification?
saberune
saberuneOP2y ago
@jcotton42 like if I just used this? errorMsg = ((PSDataCollection<ErrorRecord>)sender)[e.Index] I won't be able to output the error as string without stringification right? Unless you're thinking of a different way to capture the full error. I'll get an error like: "An error has occurred which Windows PowerShell cannot handle." ---which is too vague for me to troubleshoot. Since the web app is invoking a powershell instance, there's no way for me to see the error. Thanks again for your help.
jcotton42
jcotton422y ago
I Should've elaborated further The error object actually contains a good bit of info, like the exception that caused it So rather than just stringifying it, you should look through the properties and produce an error object or log string that suits your needs
saberune
saberuneOP2y ago
Ahhh amazing thank you so much that really helped. I added the .exception property and now I'm able to see the inner stack trace. errorMsg = ((PSDataCollection<ErrorRecord>)sender)[e.Index].Exception.ToString();
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server