Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how call a function from anonymous one?

Tags:

powershell

I have this script below and somehow cannot call function from this anonymous one. I need to call mail function to send notification on this event.

function test
{
    Write-Host "send email" 
}

$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" 
    test
    }

  $folder = 'C:\temp'
    $filter = '*.*'                             
    $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
    IncludeSubdirectories = $true        
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
    }
    $onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -  Action         $action 

Event is caught, write-host writes fine but test function is not called.

like image 863
Zulu Z Avatar asked Oct 26 '11 19:10

Zulu Z


People also ask

How do you call an anonymous function?

Normally we use the function keyword before the function name to define a function in JavaScript, however, in anonymous functions in JavaScript, we use only the function keyword without the function name.

How do you call an anonymous recursive function?

Anonymous recursive function is a type of recursion in which function does not explicitly call another function by name. This can be done either comprehensively, by using a higher order function passing in a function as an argument and calling that function.

Can a function be anonymous?

An anonymous function is a function that is not stored in a program file, but is associated with a variable whose data type is function_handle . Anonymous functions can accept multiple inputs and return one output. They can contain only a single executable statement.

How do you use anonymous function in Python?

In Python, an anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.


1 Answers

It is not exactly an issue of calling a function from an "anonymous one" ( called a scriptblock).

What is happening here is that when you specify an action to Register-ObjectEvent, it sets up a job and sends the action as the command for the job. When the event occurs and the job runs, it has no clue what the test function is.

If you actually do Get-Job and see the job, you will see that it has failed. Easiest solution is to inline the code. Or have the function in your session, by either defining it with global scope:

function global:test
{
    Write-Host "send email" 
}

or manually just defining it in you console or adding it to your profile.

Alternatively, you can add the function to a script, say test.ps1, dot source it in your $action - . 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
}
like image 138
manojlds Avatar answered Sep 23 '22 05:09

manojlds