Powershell - 需要检查名称是否以符号结尾

Powershell - Need to check if name is ended with a sign

我的脚本从 TFS 读取路径并向其中添加一个字符串,但在我需要验证路径是否包含符号之前

示例 1: 这是路径,在这种情况下我需要添加 '/database/'

$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22

示例2:我只需要添加'database/'

$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/

示例 3:我需要添加 '/'

$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database

目标是使用 path/database/ 继续脚本 所以我需要先检查路径,然后添加或删除 'database' 字符串 有人可以帮我吗?

如果我正确理解了这个问题,你想检查来自 TFS 的路径是否以正斜杠结尾,这样你就会知道要附加什么,为此你可以使用像这样的小辅助函数:

function Join-TFSPath {
    [CmdletBinding()]
    param (
        [parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        [string] $Path,
        [parameter(Mandatory = $false, Position = 1)]
        [string[]] $ChildPath,
        [char]$Separator = '/'
    )
    if ($ChildPath.Count) {
        "{0}$separator{1}$Separator" -f $Path.TrimEnd("\/"),
                                        (($ChildPath | ForEach-Object { $_.Trim("\/") } | 
                                          Where-Object { $_ -match '\S' }) -join $Separator)
    }
    else {
        "{0}$separator" -f $Path.TrimEnd("\/")
    }
}


# test if you need to add `database` or not

$tfsPath = '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database'
$folder  = 'database'

if (($tfsPath.TrimEnd("\/") -split '[\/]')[-1] -ne $folder) {
    # use the function adding the $folder as ChildPath
    Join-TFSPath -Path $tfsPath -ChildPath $folder
}
else {
    # use the function without specifying the ChildPath so it will only ensure it 
    # ends with the chosen (or in this case default) separator character
    Join-TFSPath -Path $tfsPath
}

根据您的评论,您也许可以使用更专用的辅助函数,例如:

function Append-TFSPath {
    [CmdletBinding()]
    param (
        [parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        [string] $Path,
        [parameter(Mandatory = $false, Position = 1)]
        [string] $ChildPath = 'database',
        [char]$Separator = '/'
    )
    $Path = $Path -replace '[\/]+$'  # trim off final slash(es)
    $ChildPath = $ChildPath -replace '^[\/]|[\/]$' -replace '\', $Separator
    if ([string]::IsNullOrWhiteSpace($ChildPath) -or ($Path -replace '\', $Separator) -like "*$ChildPath") {
        "{0}$separator" -f $Path
    }
    else {
        "{0}$separator{1}$Separator" -f $Path, $ChildPath
    }
}

然后,只需将您收到的路径发送给该函数,它就会return您想要的路径

$tfsPath = '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database'
$folder  = 'V8.6.22/database'

Append-TFSPath -Path $tfsPath -ChildPath $folder
# because 'database' is the default value for the ChildPath parameter, you can leave that out:
# Append-TFSPath -Path $tfsPath

测试用例:

Append-TFSPath -Path '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22'
Append-TFSPath -Path '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/'
Append-TFSPath -Path '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database'
Append-TFSPath -Path '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database/'

都会return$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database/