diff --git a/src/System.Management.Automation/engine/SessionStateContainer.cs b/src/System.Management.Automation/engine/SessionStateContainer.cs index 376b1e2d46..97c412f3f0 100644 --- a/src/System.Management.Automation/engine/SessionStateContainer.cs +++ b/src/System.Management.Automation/engine/SessionStateContainer.cs @@ -9,6 +9,7 @@ using System.Management.Automation.Runspaces; using System.Management.Automation.Internal; using System.Reflection; using Dbg = System.Management.Automation; +using System.IO; #pragma warning disable 1634, 1691 // Stops compiler from warning about unknown warnings #pragma warning disable 56500 @@ -3689,10 +3690,17 @@ namespace System.Management.Automation foreach (string path in paths) { + string resolvePath = null; if (path == null) { PSTraceSource.NewArgumentNullException("paths"); } + else + { + // To be compatible with Linux OS. Which will be either '/' or '\' depends on the OS type. + char[] charsToTrim = {' ', Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar}; + resolvePath = path.TrimEnd(charsToTrim); + } ProviderInfo provider = null; PSDriveInfo driveInfo; @@ -3705,7 +3713,7 @@ namespace System.Management.Automation if (String.IsNullOrEmpty(name)) { string providerPath = - Globber.GetProviderPath(path, context, out provider, out driveInfo); + Globber.GetProviderPath(resolvePath, context, out provider, out driveInfo); providerInstance = GetProviderInstance(provider); providerPaths.Add(providerPath); @@ -3714,7 +3722,7 @@ namespace System.Management.Automation { providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( - path, + resolvePath, true, context, out provider, diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 index 9f855ec8de..1c64851191 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 @@ -186,4 +186,10 @@ Describe "New-Item with links" -Tags @('CI', 'RequireAdminOnWindows') { $_.FullyQualifiedErrorId | Should Be "NewItemSymbolicLinkElevationRequired,Microsoft.PowerShell.Commands.NewItemCommand" } } + + It "New-Item -ItemType SymbolicLink should understand directory path ending with slash" { + $folderName = [System.IO.Path]::GetRandomFileName() + $symbolicLinkPath = New-Item -ItemType SymbolicLink -Path "$tmpDirectory/$folderName/" -Value "/bar/" + $symbolicLinkPath | Should Not Be $null + } }