internal/functions/utility/Join-DbaPath.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function Join-DbaPath
{
<#
    .SYNOPSIS
        Performs multisegment path joins.
     
    .DESCRIPTION
        Performs multisegment path joins.
     
    .PARAMETER Path
        The basepath to join on.
     
    .PARAMETER Child
        Any number of child paths to add.
     
    .EXAMPLE
        PS C:\> Join-DbaPath -Path 'C:\temp' 'Foo' 'Bar'
     
        Returns 'C:\temp\Foo\Bar' on windows.
        Returns 'C:/temp/Foo/Bar' on non-windows.
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, Position = 0)]
        [string]
        $Path,
        
        [Parameter(ValueFromRemainingArguments = $true)]
        [string[]]
        $Child
    )
    
    $resultingPath = $Path
    if (($PSVersionTable.PSVersion.Major -ge 6) -and (-not $script:isWindows))
    {
        $resultingPath = $resultingPath.Replace("\", "/")
    }
    else { $resultingPath = $resultingPath.Replace("/", "\") }
    
    foreach ($childItem in $Child)
    {
        $resultingPath = Join-Path -Path $resultingPath -ChildPath $childItem
    }
    
    $resultingPath
}