Powershell Passing Argument to background job

Tag: powershell , start-job Author: qtds00111 Date: 2013-04-30

I have a script with the following parameters:

[Parameter(ParameterSetName="FromPipe", Mandatory=$true, ValueFromPipelineByPropertyName=$true)] [string[]]$ComputerName,
[Parameter(ParameterSetName="FromPipe", Mandatory=$true, ValueFromPipelineByPropertyName=$true)] [string[]]$AppName,
[Parameter(ParameterSetName="FromPipe", Mandatory=$true, ValueFromPipelineByPropertyName=$true)] [string[]]$key,
[Parameter(ParameterSetName="FromPipe", Mandatory=$true, ValueFromPipelineByPropertyName=$true)] [string[]]$setting,

I want to run this script in the background but I've struggled with getting the arguments in. This script is a module. I can successfully run it from the command line with

start-job -InitializationScript {Import-Module ConfigureServer.psm1} -ScriptBlock {Get-ConfigureServerInput} -ArgumentList "google.com","Test1","Test2","Test3","-Verbose"

But here is the actual code in my script for starting the job:

$JobObj = New-Object PSObject
$JobObj | Add-Member -type NoteProperty -name ComputerName -value $($obj.ComputerName)
$JobObj | Add-Member -type NoteProperty -name AppName -value $($obj.AppName)
$JobObj | Add-Member -type NoteProperty -name key -value $($obj.key)
$JobObj | Add-Member -type NoteProperty -name setting -value $($obj.setting)
$JobObj | Add-Member -type NoteProperty -name Verbose -value $true
Write-Verbose "Starting job: -Name $JobName -FilePath Job-ConfigureServer.ps1 -ArgumentList $($JobObj)"
#Start-Job -Name $JobName -InitializationScript {import-module ConfigureServer.psm1} -ScriptBlock {Get-ConfigureServerInput} -ArgumentList $($JobObj) | Out-Null
#Start-Job -Name $JobName -InitializationScript {import-module ConfigureServer.psm1} -ScriptBlock {Get-ConfigureServerInput @ARGS} -ArgumentList $($JobObj) | Out-Null
Start-Job -Name $JobName -InitializationScript {import-module ConfigureServer.psm1} -ScriptBlock {Get-ConfigureServerInput -ComputerName $($obj.ComputerName) -AppName $($obj.AppName) -key $($obj.key) -setting $($obj.setting) -Verbose}
Start-Job -FilePath TestJob.ps1 -ArgumentList $($obj)

The uncommented Start-Job just runs a test script that dumps $args.

"Arguments: $($args.count)"
$args

Which results in:

PS E:\Powershell\ConfigureServer> get-job | receive-job
Arguments: 1


ComputerName : server.bogus.com
AppName      : iis
key          : default site
setting      : cert
RunspaceId   : 5c72401e-3cb1-4f44-b0e1-c370c8a3c200

Arguments: 1
ComputerName : fbravo
AppName      : Net
key          : IP
setting      : 10.11.12.13
RunspaceId   : 80b2095c-6129-4d7c-8e89-528cf93ce975

Arguments: 1
ComputerName : tfs.bogus.com
AppName      : edamame
key          : DLL
setting      : edamame.dll
RunspaceId   : f7727c69-121c-4a59-b646-a4f0eac53e7d

Arguments: 1
ComputerName : falpha
AppName      : IIS
key          : IP
setting      : 1.1.1.1
RunspaceId   : cd182911-d4a6-4bfd-ba4b-103ebaab20a3

Arguments: 1
ComputerName : web.bogus.com
AppName      : Net
key          : IP
setting      : 192.168.100.102
RunspaceId   : 03d6130c-46d1-483d-833c-781720fcc0cf

How do I format my Start-Job to successfully pass the arguments? I've tried various formats of Start-Job with -FilePath, listing the arguments instead of passing an object, etc.

They all end up blocked waiting for user input prompting for the parameters.