简体   繁体   中英

If statement within FileSystemWatcher Not working in Powershell

I am trying to implement a FileSystemWatcher that contains an if statement that evaluates and argument passed to it, but it doesn't seem to be evaluating. Am I making a mistake? Here is the code:

Function Auto-Watcher
{
    param ($folder, $filter, $Program)

    $watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
        IncludeSubdirectories = $true
        EnableRaisingEvents = $true
    }

    Write-Host "Watching $folder for creation or moving of $filter files..."

    $changeAction = {
        $path = $Event.SourceEventArgs.FullPath
        $name = $Event.SourceEventArgs.Name
        $changeType = $Event.SourceEventArgs.ChangeType
        $timeStamp = $Event.TimeGenerated

        if ($Program -match "Report1") {
            Write-Host $Path "Ready for Report1 Generation"
            else {
                if ($Program -match "Report2") {
                    Write-Host Split-Path $Path "Ready for Report2 Generation"
                    else {
                        write-output "Error, not capable of matching identical strings"
                    }
                }
            }
        }

        Write-Host "The file $name was $changeType at $timeStamp"
    }
    Register-ObjectEvent $Watcher -EventName "Created" -Action $changeAction
}

I added in a Write-Output statement before the if statements that confirmed that $Program -match "Report1" was returning $true , but nothing in the if statements seems to be evaluated. What can I do?

Your if / else structure is incorrect inside of $changeAction . Both else blocks are inside what should be their associated if block. That is, you have this...

if ($condition) {
    else {
    }
}

...when it should be this...

if ($condition) {
    # $true branch
} else {
    # $false branch
}

Try defining the if structure within $changeAction like this...

if ($Program -match "Report1") {
    Write-Host $Path "Ready for Report1 Generation"
} elseif ($Program -match "Report2") {
    Write-Host (Split-Path $Path) "Ready for Report2 Generation"
} else {
    Write-Output "Error, not capable of matching identical strings"
}

...and see if that works. Note that I added () around the call to Split-Path $Path so that is evaluated and the result passed to Write-Host .

You could also rewrite the above using a switch statement...

switch -Regex ($Program) {
    "Report1" {
        Write-Host $Path "Ready for Report1 Generation"
        break
    }
    "Report2" {
        Write-Host (Split-Path $Path) "Ready for Report2 Generation"
        break
    }
    Default {
        Write-Output "Error, not capable of matching identical strings"
        break
    }
}

I added the -Regex parameter to make it equivalent to your use of the -match operator in the if conditions. Note that if your intent was to perform an exact string comparison in the if statements you could use, for example, if ($Program -eq "Report1") { to perform a case-insensitive comparison. If your intent was to perform a substring comparison you could use if ($Program -like "*Report1*") { instead of the -match operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM