0

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.

Arluin
  • 594
  • 1
  • 8
  • 21
  • Thanks, I didn't realize it had auto-generated that title from my search parameters. – Arluin May 16 '13 at 18:57
  • I got partial success. I added positional notation to my parameters then used this command to start the job: Start-Job -Name $JobName -InitializationScript {import-module ConfigureServer.psm1} -ScriptBlock {Get-ConfigureServerInput @ARGS} -ArgumentList $($obj.ComputerName), $($obj.AppName), $($obj.key), $($obj.setting) – Arluin May 16 '13 at 19:46
  • But I can't use any switches, like -Verbose with that command or it gives me "there is no positional argument that accepts -verbose." – Arluin May 16 '13 at 19:48
  • possible duplicate of [Start-Job script block-- how to call cmdlets with arguments?](http://stackoverflow.com/questions/12571788/start-job-script-block-how-to-call-cmdlets-with-arguments) – Lars Truijens May 16 '13 at 20:35
  • The ArgumentList is passed to the script block. You have to then pass it to your function. Also see http://stackoverflow.com/questions/4225748/how-do-i-pass-named-parameters-with-invoke-command – Lars Truijens May 16 '13 at 20:36
  • ok, that resolves the problem of passing a switch. But it seems a broader answer is, "when using invoke-command or start-job with a script block you must also declare your parameters in the script block. – Arluin May 16 '13 at 21:35

0 Answers0