I have this simple script which does not seem to work.
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
The output always says Matched...
when it should not. Surprisingly the match line by itself behaves correctly when run in the 开发者_StackOverflow中文版interactive env.
PS C:\Users\sjoshi> .\sc1 -where "." -what "*s*"
Finding in '.' - '*s*'
Matched...
Finding in '.' - '*s*'
Any thoughts what is happening here ?
Right here: if (!$what -match ".sql$")
!$what is either going to be $true or $false, depending on whether $what is null or contains some value, and that's what your comparing ".\sql$" to.
I think what you wanted was:
if ($what -notmatch "\.sql$")
To demonstrate:
$a = "something"
!$a
False
$a = $null
!$a
True
精彩评论