Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DependencyManagement/DependencyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ internal void InstallFunctionAppDependencies(PowerShell pwsh, ILogger logger)
finally
{
// Clean up
pwsh.AddCommand("Microsoft.PowerShell.Core\\Remove-Module")
pwsh.AddCommand(Utils.RemoveModuleCmdletInfo)
.AddParameter("Name", "PackageManagement, PowerShellGet")
.AddParameter("Force", true)
.AddParameter("ErrorAction", "SilentlyContinue")
Expand Down
15 changes: 11 additions & 4 deletions src/PowerShell/PowerShellManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ internal void InvokeProfile(string profilePath)
try
{
// Import-Module on a .ps1 file will evaluate the script in the global scope.
_pwsh.AddCommand("Microsoft.PowerShell.Core\\Import-Module")
_pwsh.AddCommand(Utils.ImportModuleCmdletInfo)
.AddParameter("Name", profilePath)
.AddParameter("PassThru", true)
.AddCommand("Microsoft.PowerShell.Core\\Remove-Module")
.AddCommand(Utils.RemoveModuleCmdletInfo)
.AddParameter("Force", true)
.AddParameter("ErrorAction", "SilentlyContinue")
.InvokeAndClearCommands();
Expand Down Expand Up @@ -144,7 +144,7 @@ internal Hashtable InvokeFunction(
{
// If an entry point is defined, we import the script module.
moduleName = Path.GetFileNameWithoutExtension(scriptPath);
_pwsh.AddCommand("Microsoft.PowerShell.Core\\Import-Module")
_pwsh.AddCommand(Utils.ImportModuleCmdletInfo)
.AddParameter("Name", scriptPath)
.InvokeAndClearCommands();

Expand Down Expand Up @@ -206,12 +206,19 @@ private void ResetRunspace(string moduleName)
if (!string.IsNullOrEmpty(moduleName))
{
// If the function had an entry point, this will remove the module that was loaded
_pwsh.AddCommand("Microsoft.PowerShell.Core\\Remove-Module")
_pwsh.AddCommand(Utils.RemoveModuleCmdletInfo)
.AddParameter("Name", moduleName)
.AddParameter("Force", true)
.AddParameter("ErrorAction", "SilentlyContinue")
.InvokeAndClearCommands();
}

// Clean up jobs started during the function execution.
_pwsh.AddCommand(Utils.GetJobCmdletInfo)
.AddCommand(Utils.RemoveJobCmdletInfo)
.AddParameter("Force", true)
.AddParameter("ErrorAction", "SilentlyContinue")
.InvokeAndClearCommands();
}
}
}
6 changes: 6 additions & 0 deletions src/Utility/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
using System.IO;
using System.Management.Automation;
using System.Text;
using Microsoft.PowerShell.Commands;

namespace Microsoft.Azure.Functions.PowerShellWorker.Utility
{
internal class Utils
{
internal static CmdletInfo ImportModuleCmdletInfo = new CmdletInfo("Import-Module", typeof(ImportModuleCommand));
internal static CmdletInfo RemoveModuleCmdletInfo = new CmdletInfo("Remove-Module", typeof(RemoveModuleCommand));
internal static CmdletInfo GetJobCmdletInfo = new CmdletInfo("Get-Job", typeof(GetJobCommand));
internal static CmdletInfo RemoveJobCmdletInfo = new CmdletInfo("Remove-Job", typeof(RemoveJobCommand));

/// <summary>
/// Helper method to do additional transformation on the input value based on the type constraints specified in the script.
/// </summary>
Expand Down