I'm trying to run some of my custom code asynchronously in Powershell. The following tries to check for updates in a background thread:
Function CheckUpdates($manager) { . "$PSScriptRoot\..\commands\upgradecmd.ps1"; $upgradeCmd = New-Object UpgradeCmd -ArgumentList $manager; [bool]$upgradesAvailable = $false; try { $Global:silentmode = $true; $upgradeCmd.preview = $true; Start-Job -ArgumentList $upgradeCmd -ScriptBlock { param($upgradeCmd) $upgradeCmd.Run(); }; Get-Job | Receive-Job; $upgradesAvailable = $upgradeCmd.upgradesAvailable; } finally { $Global:silentmode = $false; } if ($upgradesAvailable) { WriteWarning "Upgrades detected."; WriteWarning "Please, run the upgrade command to update your Everbot installation."; } } The problem is that inside the job (in the ScriptBlock), PS doesn't recognize anything about my custom "Run()" method, so it doesn't know how to call it. I've tried to "include" the class in the job using the -InitializationScript parameter with little success.
After searching the web, it seems that the way to do this is using PS Jobs, there's no thread handling in PS or something like "async". The point is that I just want to run a method of some class of my PS code asynchronously.