Powershell匹配条款

时间:2011-02-04 20:49:33

标签: 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 个答案:

答案 0 :(得分:1)

就在这里:if(!$ what -match“.sql $”)

!$要么是$ true还是$ false,这取决于$ what是null还是包含一些值,这就是你的比较“。\ sql $”到。

我认为你想要的是:

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

演示:

 $a = "something"
 !$a
 False

 $a = $null
 !$a
 True