Works fine in powershell ISE, but in console I get:
The term 'MainAction' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
How to fix?
function MainAction () {
$test = "123"
Write-Host $test
}
MainAction
$action = {
try {
Write-Host in action
MainAction
} catch {
Write-Host $Error
$timer.Stop()
Unregister-Event thetimer
}
}
$timer = New-Object Timers.Timer
Register-ObjectEvent -InputObject $timer -EventName elapsed `
-SourceIdentifier thetimer -Action $action -OutVariable out
$timer.Interval = 5000
$timer.AutoReset = $true
$timer.Start()
EDIT:
I found that I can use profile to store function definitions. To create profile use:
New-Item -path $profile -itemType file -force
But I'am still intrested why powershell ISE don't need profile to store and use functions in action of Register-ObjectEvent.
function MainAction->function global:MainAction