Skip to content

Commit cb0487c

Browse files
committed
Import PSDesiredStateConfiguration by name
Instead of a hard-coded path which doesn't always work. Also apply `ErrorAction -Ignore` fix to Plaster module.
1 parent ae4abd7 commit cb0487c

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,17 @@ public static async Task<DscBreakpointCapability> GetDscCapabilityAsync(
9292
if (!isDscInstalled.HasValue)
9393
{
9494
PSCommand psCommand = new PSCommand()
95-
.AddCommand("Import-Module")
96-
.AddArgument(@"C:\Program Files\DesiredStateConfiguration\1.0.0.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psd1")
95+
.AddCommand("Microsoft.PowerShell.Core\\Import-Module")
96+
.AddParameter("-Name", "PSDesiredStateConfiguration")
9797
.AddParameter("PassThru")
9898
.AddParameter("ErrorAction", ActionPreference.Ignore);
9999

100100
IReadOnlyList<PSModuleInfo> dscModule =
101101
await psesHost.ExecutePSCommandAsync<PSModuleInfo>(
102102
psCommand,
103103
CancellationToken.None,
104-
new PowerShellExecutionOptions { ThrowOnError = false }).ConfigureAwait(false);
104+
new PowerShellExecutionOptions { ThrowOnError = false })
105+
.ConfigureAwait(false);
105106

106107
isDscInstalled = dscModule.Count > 0;
107108
logger.LogTrace("Side-by-side DSC module found: " + isDscInstalled.Value);

src/PowerShellEditorServices/Services/Template/TemplateService.cs

+14-5
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ public async Task<bool> ImportPlasterIfInstalledAsync()
7474

7575
IReadOnlyList<PSObject> moduleObject =
7676
await _executionService.ExecutePSCommandAsync<PSObject>(
77-
psCommand, CancellationToken.None).ConfigureAwait(false);
77+
psCommand,
78+
CancellationToken.None,
79+
new PowerShellExecutionOptions { ThrowOnError = false })
80+
.ConfigureAwait(false);
7881

7982
isPlasterInstalled = moduleObject.Count > 0;
8083
_logger.LogTrace("Plaster installed: " + isPlasterInstalled.Value);
@@ -86,13 +89,19 @@ await _executionService.ExecutePSCommandAsync<PSObject>(
8689

8790
psCommand = new PSCommand();
8891
psCommand
89-
.AddCommand("Import-Module")
92+
.AddCommand("Microsoft.PowerShell.Core\\Import-Module")
9093
.AddParameter("ModuleInfo", (PSModuleInfo)moduleObject[0].ImmediateBaseObject)
91-
.AddParameter("PassThru");
94+
.AddParameter("PassThru")
95+
.AddParameter("ErrorAction", ActionPreference.Ignore);
9296

93-
IReadOnlyList<PSModuleInfo> importResult = await _executionService.ExecutePSCommandAsync<PSModuleInfo>(psCommand, CancellationToken.None).ConfigureAwait(false);
97+
IReadOnlyList<PSModuleInfo> plasterModule =
98+
await _executionService.ExecutePSCommandAsync<PSModuleInfo>(
99+
psCommand,
100+
CancellationToken.None,
101+
new PowerShellExecutionOptions { ThrowOnError = false })
102+
.ConfigureAwait(false);
94103

95-
isPlasterLoaded = importResult.Count > 0;
104+
isPlasterLoaded = plasterModule.Count > 0;
96105
_logger.LogTrace("Plaster loaded: " + isPlasterLoaded);
97106
}
98107
}

0 commit comments

Comments
 (0)