Search notes:

PowerShell cmdLet Start-Job

start-job returns a System.Management.Automation.PSRemotingJob object.

Passing variables to a job

In order to pass a variable to a script block that is started with start-job, the variable name needs to be prefixed with using:.
$var=42

$scriptBlock = { add-content ~/variable-value "var=$var | using:var=$using:var" }

$var='changed'

start-job $scriptBlock
Github repository about-PowerShell, path: /cmdlets/job/start/pass-variable.ps1
This script, when run, writes the following line into ~/variable-value:
var= | using:var=changed
See also scopes.

Working directory for jobs ps-start-job-wd

A job's working directory is set to $home\Documents (Windows Powershell) or $home (PowerShell Core).
This is demonstrated by the following simple pipeline: it starts a job with a script block which in turn writes the current location (get-location) to ~/current-directory.
After the job has finished, ~/current-directory can be inspected to determine the current directory of powershell jobs.
start-job { add-content ~/current-directory (get-location) }
Github repository about-PowerShell, path: /cmdlets/job/start/current-directory.ps1
PowerShell 7 finally comes with the new option -workingDirectory which allows to change the initial working directory for a background job.

Passing parameters to a scheduled cmdLet

The following command tries to use start-job to schedule a download of a resource and storing it to an aribitrary file, using the -outFile parameter:
start-job invoke-webRequest http://server.xyz/path/to/resource.pdf -outFile downloadedFile.pdf
However, this results in the error message Start-Job : Parameter set cannot be resolved using the specified named parameters. This is becouse start-job (rather than invoke-webRequest) tries to interprete the -outfile parameter.
In order to pass a parameter to a scheduled cmdLet, a script block must be used:
start-job -scriptBlock { invoke-webRequest http://server.xyz/path/to/resource.pdf -outFile downloadedFile.pdf }
The -scriptBlock parameter is not necessary:
start-job { invoke-webRequest http://server.xyz/path/to/resource.pdf -outFile downloadedFile.pdf }
Of course, if using a script block to download a resource, absolute paths or the working directory should be specified in order to download the file to a non-default location.
The following command combines the using scope with the $pwd automatic variable to download a resource to the current directory:
start-job  { invoke-webRequest https//server.xyz/path/to/resource.pdf -outFile $using:pwd\downloadedFile.pdf }

See also

start-process.
register-objectEvent
The command parameter -credential.
Powershell command noun: job

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Windows/...', 1745379668, '160.79.110.169', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Windows/PowerShell/command-inventory/noun/job/start(111): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78