From 22aac12c3e39d9fd6077b935af55cc9a9473aefe Mon Sep 17 00:00:00 2001 From: Dave Wyatt Date: Mon, 22 Aug 2016 10:52:09 -0400 Subject: [PATCH] Add support for multiple paths to Join-Path Multiple child paths can be specified via the new positional parameter -AdditionalChildPath. --- .../commands/management/CombinePathCommand.cs | 25 +++++++++++++++++-- .../Join-Path.Tests.ps1 | 5 ++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs index ef8a815fdf..ae70f3fce2 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs @@ -3,6 +3,7 @@ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ using System; +using System.Text; using System.Collections.ObjectModel; using System.Management.Automation; using Dbg = System.Management.Automation; @@ -32,7 +33,16 @@ namespace Microsoft.PowerShell.Commands [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true)] [AllowNull] [AllowEmptyString] - public string ChildPath { get; set; } = String.Empty; + public string ChildPath { get; set; } + + /// + /// Gets or sets additional childPaths to the command. + /// + [Parameter(Position = 2, Mandatory = false, ValueFromPipelineByPropertyName = true, ValueFromRemainingArguments = true)] + [AllowNull] + [AllowEmptyString] + [AllowEmptyCollection] + public string[] AdditionalChildPath { get; set; } = Utils.EmptyArray(); /// /// Determines if the path should be resolved after being joined @@ -55,6 +65,17 @@ namespace Microsoft.PowerShell.Commands Path != null, "Since Path is a mandatory parameter, paths should never be null"); + string combinedChildPath = ChildPath; + + // join the ChildPath elements + if (AdditionalChildPath != null) + { + foreach (string childPath in AdditionalChildPath) + { + combinedChildPath = SessionState.Path.Combine(combinedChildPath, childPath, CmdletProviderContext); + } + } + foreach (string path in Path) { // First join the path elements @@ -64,7 +85,7 @@ namespace Microsoft.PowerShell.Commands try { joinedPath = - SessionState.Path.Combine(path, ChildPath, CmdletProviderContext); + SessionState.Path.Combine(path, combinedChildPath, CmdletProviderContext); } catch (PSNotSupportedException notSupported) { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Join-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Join-Path.Tests.ps1 index 9bd63888df..d21e03369b 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Join-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Join-Path.Tests.ps1 @@ -43,4 +43,9 @@ Describe "Join-Path cmdlet tests" -Tags "CI" { $result.Count | Should be 1 $result | Should BeExactly ("Env:"+$SepChar+"foo") } + It "should be able to join multiple child paths passed by position with remaining arguments" { + $result = Join-Path one two three four five + $result.Count | Should Be 1 + $result | Should BeExactly "one${sepChar}two${sepChar}three${sepChar}four${sepChar}five" + } }