如何查找变量并使用结果在PowerShell中匹配?

时间:2014-12-05 14:27:40

标签: powershell

$installLocation = "C:\hello\world"
$packageName = "hello"

Write-Host "if ("$installLocation" -match '${packageName}|bla') {"

if ("$installLocation" -match '${packageName}|bla') {
    Write-Host "hello";
}

if ("$installLocation" -match 'hello|bla') {
    Write-Host "world";
}

当前结果

if ( C:\hello\world -match 'hello|bla') {
world

预期结果

if ( C:\hello\world -match 'hello|bla') {
hello
world

1 个答案:

答案 0 :(得分:1)

您需要在变量周围使用双引号。

试试这个:

$packageName = "hello"
echo '${packageName}|bla'
echo "${packageName}|bla"

结果是:

${packageName}|bla
hello|bla

因此,要修复脚本,请使用:

if ("$installLocation" -match "${packageName}|bla") {
    Write-Host "hello";
}

给出了结果:

hello
world