This is not a problem calling a function from "anonymous" (called a script block).
What happens here is that when you point the action to Register-ObjectEvent
, it sets the task and sends the action as a command for the task. When an event occurs and the task runs, he does not know what a test function is.
If you really do the Get-Job
and see the job, you will see that it worked. The easiest solution is to embed the code. Either you have a function in your session, or by defining it using the global
scope:
function global:test { Write-Host "send email" }
or manually just defining it in the console or adding to your profile.
Alternatively, you can add a function to the script, say test.ps1
, period, enter it in your $action
- . path\to\test.ps1
. path\to\test.ps1
, and then call test
:
$action = { $path = $Event.SourceEventArgs.FullPath $name = $Event.SourceEventArgs.Name $changeType = $Event.SourceEventArgs.ChangeType $timeStamp = $Event.TimeGenerated Write-Host "The file '$name' was $changeType at $timeStamp" . c:\test.ps1 test }
source share