2014-12-05 61 views
1
$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"; 
} 

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

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

预期成果

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

回答

1

您需要使用的变量左右双引号。

试试这个:

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

结果是:

${packageName}|bla 
hello|bla 

所以要修复脚本,使用:

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

其给出结果:

hello 
world 
相关问题