简体   繁体   中英

How to avoid crash of a .NET exe when a termination error occurred in powershell script execution that has invoked using Powershell.Invoke()

The following code invokes a powershell script and when the script breaks due to a termination error from powershell script such as "Attempting to perform the InitializeDefaultDrives operation on the 'ActiveDirectory' provider failed." catches the exception but the .NET app crashes.

try
{
 output = this.PowerShellEnvironment.Invoke();
}
 catch (CmdletInvocationException ex)
{ 
}
catch (RuntimeException ex)
{
}
catch (Exception ex)
{
}
  try
{
 output = this.PowerShellEnvironment.Invoke();
}
 catch (Exception ex)
{ 
//handle ex here
}

use following code get the excpetion type and do the operation you want to perform (handle exception etc..) in catch block

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM