1

I'm new on WPF/PowerShell and I'm trying to execute a locally defined Powershell function inside a DispatcherTimer Tick scriptblock but I get this error

My code is simply :

function Get-CurrentStorageJobs
{
     My_Code 
}

$MonitorTimer = New-Object System.Windows.Threading.DispatcherTimer
$MonitorTimer.Interval = New-TimeSpan -Seconds 3
$MonitorTimer.Add_Tick({ Get-CurrentStorageJobs })
$MonitorTimer.Start()

I assumed the function didn't exist in the Tick scriptblock context so I tried to pass the function in the same way as I do with Invoke-Command scriptblock:

$MonitorTimer.Add_Tick(${ function:Get-CurrentStorageJobs })

But it failed also...

Could somebody help me ?

Herunòla
  • 13
  • 2
  • The error indicates the code is looking for a cmdlet instead of a function. So you can make the function a text file with ps1 extension or put the code in a string a use Invoke-Expression. – jdweng Apr 14 '23 at 12:54
  • I succeed to do that by putting my function code inside a scriptblock variable (with script scope) and execute it with invoke-command. Thanks :) – Herunòla Apr 19 '23 at 11:43

0 Answers0