Fix Export-PSSession to not throw error when a rooted path is specified for -OutputModule (#17671)

This commit is contained in:
Dongbo Wang
2022-07-13 13:31:57 -07:00
committed by GitHub
parent 667ea982ce
commit f9d424dd86
2 changed files with 21 additions and 12 deletions
@@ -139,7 +139,7 @@
<value>The directory '{0}' already exists. Use the -Force parameter if you want to overwrite the directory and files within the directory.</value>
</data>
<data name="ExportPSSession_ErrorModuleNameOrPath" xml:space="preserve">
<value>The -OutputModule parameter does not resolve to a path, and a user module path cannot be found for the provided name.</value>
<value>The user module path does not exist, and hence a module folder cannot be created for the provided module name '{0}'.</value>
</data>
<data name="ExportPSSession_CannotCreateOutputDirectory" xml:space="preserve">
<value>Cannot create the module {0} due to the following: {1}. Use a different argument for the -OutputModule parameter and retry.</value>
@@ -7,7 +7,7 @@ using System.IO;
using System.Management.Automation.Internal;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.PowerShell.Commands;
using Dbg = System.Management.Automation.Diagnostics;
namespace System.Management.Automation
@@ -357,7 +357,9 @@ namespace System.Management.Automation
DirectoryInfo directoryInfo = null;
try
{
string rootedPath = Microsoft.PowerShell.Commands.ModuleCmdletBase.ResolveRootedFilePath(moduleNameOrPath, cmdlet.Context);
// Even if 'moduleNameOrPath' is a rooted path, 'ResolveRootedFilePath' may return null when the path doesn't exist yet,
// or when it contains wildcards but cannot be resolved to a single path.
string rootedPath = ModuleCmdletBase.ResolveRootedFilePath(moduleNameOrPath, cmdlet.Context);
if (string.IsNullOrEmpty(rootedPath) && moduleNameOrPath.StartsWith('.'))
{
PathInfo currentPath = cmdlet.CurrentProviderLocation(cmdlet.Context.ProviderNames.FileSystem);
@@ -366,18 +368,25 @@ namespace System.Management.Automation
if (string.IsNullOrEmpty(rootedPath))
{
string personalModuleRoot = ModuleIntrinsics.GetPersonalModulePath();
if (string.IsNullOrEmpty(personalModuleRoot))
if (Path.IsPathRooted(moduleNameOrPath))
{
cmdlet.ThrowTerminatingError(
new ErrorRecord(
new ArgumentException(PathUtilsStrings.ExportPSSession_ErrorModuleNameOrPath),
"ExportPSSession_ErrorModuleNameOrPath",
ErrorCategory.InvalidArgument,
cmdlet));
rootedPath = moduleNameOrPath;
}
else
{
string personalModuleRoot = ModuleIntrinsics.GetPersonalModulePath();
if (string.IsNullOrEmpty(personalModuleRoot))
{
cmdlet.ThrowTerminatingError(
new ErrorRecord(
new ArgumentException(StringUtil.Format(PathUtilsStrings.ExportPSSession_ErrorModuleNameOrPath, moduleNameOrPath)),
"ExportPSSession_ErrorModuleNameOrPath",
ErrorCategory.InvalidArgument,
cmdlet));
}
rootedPath = Path.Combine(personalModuleRoot, moduleNameOrPath);
rootedPath = Path.Combine(personalModuleRoot, moduleNameOrPath);
}
}
directoryInfo = new DirectoryInfo(rootedPath);