Add specific error message that creating Junctions requires absolute path (#19409)

This commit is contained in:
Steve Lee
2023-05-09 08:37:28 -07:00
committed by GitHub
parent d314b0b1c0
commit 7888196b87
3 changed files with 21 additions and 0 deletions
@@ -2434,6 +2434,13 @@ namespace Microsoft.PowerShell.Commands
bool exists = false;
// junctions require an absolute path
if (!Path.IsPathRooted(strTargetPath))
{
WriteError(new ErrorRecord(new ArgumentException(FileSystemProviderStrings.JunctionAbsolutePath), "NotAbsolutePath", ErrorCategory.InvalidArgument, strTargetPath));
return;
}
try
{
exists = GetFileSystemInfo(strTargetPath, out isDirectory) != null;
@@ -345,4 +345,7 @@
<data name="CopyingLocalBytesStatus" xml:space="preserve">
<value>{0} of {1} ({2:0.0} MB/s)</value>
</data>
<data name="JunctionAbsolutePath" xml:space="preserve">
<value>Creating a junction requires an absolute path for the target.</value>
</data>
</root>
@@ -613,6 +613,17 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows"
Test-Path $junctionToDir | Should -BeTrue
}
It 'New-Item fails creating junction with relative path' -Skip:(!$IsWindows) {
try {
Push-Location $TestDrive
1 > 1.txt
{ New-Item -ItemType Junction -Path 2.txt -Target 1.txt -ErrorAction Stop } | Should -Throw -ErrorId "NotAbsolutePath,Microsoft.PowerShell.Commands.NewItemCommand"
}
finally {
Pop-Location
}
}
It 'New-Item can create hardlink with relative path' {
try {
Push-Location $TestDrive