ForEach-Object循环中字符串的破坏性处理?

时间:2014-10-01 21:03:00

标签: powershell

我是PowerShell的新手,并了解cmdlet如何工作的细节。我有三条路,我想确保它们都有斜杠。下面的代码应该检查每个代码并附加一个\如果不存在,但它不会自己更改变量本身。

# append trailing slash if not present
$engineXCopyPath, $engineXBackupPath, $enlistmentBuildTargetPath | ForEach-Object {
    if(!$_.EndsWith("\")) {
        $_ += "\"
    }
} 

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

以这种方式尝试:

# append trailing slash if not present
$engineXCopyPath, $engineXBackupPath, $enlistmentBuildTargetPath =
$engineXCopyPath, $engineXBackupPath, $enlistmentBuildTargetPath | ForEach-Object {
    if(!$_.EndsWith("\")) {
        $_ + "\"
    }
    else {
        $_ # keep the original value if appending isn't needed
    }       
}