Skip to content

Commit f04fc5f

Browse files
authored
Merge pull request #386 from daviwil/fix-192
Handle exceptions when calling Set-ExecutionPolicy
2 parents 34f949c + 967449a commit f04fc5f

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace Microsoft.PowerShell.EditorServices
2323
using System.Management.Automation.Runspaces;
2424
using Microsoft.PowerShell.EditorServices.Session.Capabilities;
2525
using System.IO;
26+
using System.Security;
2627

2728
/// <summary>
2829
/// Manages the lifetime and usage of a PowerShell session.
@@ -1302,10 +1303,18 @@ private void SetExecutionPolicy(ExecutionPolicy desiredExecutionPolicy)
13021303
.AddParameter("Scope", ExecutionPolicyScope.Process)
13031304
.AddParameter("Force");
13041305

1305-
this.powerShell.Invoke();
1306-
this.powerShell.Commands.Clear();
1306+
try
1307+
{
1308+
this.powerShell.Invoke();
1309+
}
1310+
catch (CmdletInvocationException e)
1311+
{
1312+
Logger.WriteException(
1313+
$"An error occurred while calling Set-ExecutionPolicy, the desired policy of {desiredExecutionPolicy} may not be set.",
1314+
e);
1315+
}
13071316

1308-
// TODO: Ensure there were no errors?
1317+
this.powerShell.Commands.Clear();
13091318
}
13101319
else
13111320
{

0 commit comments

Comments
 (0)