2014-12-05 26 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 
相關問題