开发者

Powershell match clause

开发者 https://www.devze.com 2023-02-08 09:56 出处:网络
I have this simple script which does not seem to work. param ($where, $what) Write-Host \"Finding in \'$where\' - \'$what\'\"

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号