PowerShell Spawn a background process for keep alive PowerShell Spawn a background process for keep alive powershell powershell

PowerShell Spawn a background process for keep alive


Easiest way - use a background job:

Start-Job {    $wsh = New-Object -ComObject WScript.Shell    while($true) {        $wsh.SendKeys('{SCROLLLOCK}')        Start-Sleep 60    }}

Background jobs will (transparently to you) result in the code running in a separate child process.

If you want to run your code in the background within the same process, you'll want to assign your code to run in a separate runspace - a runspace is somewhat analogous to a "thread" in low-level programming languages:

# Create (and open) a new runspace$rs = [runspacefactory]::CreateRunspace([initialsessionstate]::CreateDefault2())$rs.Open()# Create a [powershell] object to bind our script to the above runspaces$ps = [powershell]::Create().AddScript({    $wsh = New-Object -ComObject WScript.Shell    while($true) {        $wsh.SendKeys('{SCROLLLOCK}')        Start-Sleep 60    }})# Tell powershell to run the code in the runspace we created$ps.Runspace = $rs# Invoke the code asynchronously$handle = $ps.BeginInvoke()

The BeginInvoke() function will return immediately, and the code will start executing in our background runspace - meaning it doesn't block the default runspace, so you can continue using the shell in the mean time.

To end/halt execution again:

$ps.Stop()


I've confirmed that Start-Process seems to start a completely new process. I think this might be the correct approach. I guess that somehow I need to start a new powershell window, then get that process to run my code? A bit confused on the best approach here: should I take the Keypress code code and redirect it into a temp script file, and then call that script from the new Start-Process or is there a more efficient approach to do this do you think?