2011-02-04 72 views
0

我有這個簡單的腳本,似乎不工作。Powershell匹配子句

param ($where, $what) 
Write-Host "Finding in '$where' - '$what'" 
if (!$what -match "\.sql$") 
{ 
    $what += ".sql" 
    Write-Host "Unmatched..." 
} 
else 
{ 
    Write-Host "Matched..." 
} 
Write-Host "Finding in '$where' - '$what'" 
#Get-ChildItem $where $what -Recurse 

輸出總是說不應該是Matched...。令人驚訝的是,匹配行本身在交互式環境中運行時表現正確。

PS C:\Users\sjoshi> .\sc1 -where "." -what "*s*" 
Finding in '.' - '*s*' 
Matched... 
Finding in '.' - '*s*' 

有什麼想法這裏發生了什麼?

回答

1

就在這裏:如果(!$什麼-match 「.SQL $」)

$什麼是要麼會爲$ true或$ false,取決於是否$什麼爲空或包含一定的價值,這就是你比較「。\ sql $」到。

我想你想要的是:

if ($what -notmatch "\.sql$") 

爲了證明:

$a = "something" 
!$a 
False 

$a = $null 
!$a 
True 
+0

我的壞...您的回覆後,我改變(($什麼-match「\ $名爲.sql 「)),並工作。感謝Sunit – Sunit 2011-02-04 21:36:26