0

I'm tinkering with the code below. It should create a PoshRSJob and run the function foo again in the runspace there.

I want to be able to turn the $list parameter into an [array] or [string[]], but when I do it throws errors. I considered flattening my array into a string, but if I change $list3 to include a space or comma in the string it also throws an error. I believe it is this line that is causing the issue, but I don't know why or what to do to circumvent this issue:

ScriptBlock = [scriptblock]::Create("`$_ | $($PSCmdlet.MyInvocation.MyCommand.Name) -Parallel:`$false -fn:$fn -sqlQuery:$SQLQuery -option:$option -List:$List")

Code:

function foo {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
        [Alias("ComputerName")]
        [PSObject] $InputObject,
        [switch] $Parallel = $true,
        [string] $fn,
        [string] $sqlQuery = "none",
        [string] $option = "none",
        [int] $number,
        [int] $Throttle = 100,
#Want this to be [array] or [string[]] :
        [string] $List = "none" 
    )

    begin {
        $batch = [System.Guid]::NewGuid().Guid  #run all jobs under same batch number
    }

    process {
        if ($Global:debugging -eq $true){$host.ui.WriteDebugLine("fn:$fn | SQLQuery:$sqlQuery")}
        if (!$Parallel) {
            $server = $InputObject.name
            switch($fn){
                Manage{ return $list }
            } #end switch
        } else {#region Parallel run            
            $jobArguments = @{
                Throttle = $Throttle
                Batch = $batch
                FunctionsToLoad = $PSCmdlet.MyInvocation.MyCommand.Name
#This is the problematic line:
                ScriptBlock = [scriptblock]::Create("`$_ | $($PSCmdlet.MyInvocation.MyCommand.Name) -Parallel:`$false -fn:$fn -sqlQuery:$SQLQuery -option:$option -List:$List")
            }
            if ($_ -and $_ -isnot [string]) { $serverName=$_ } else { $serverName=$InputObject.name }
            @(if ($_ -and $_ -isnot [string]) { $_ } else { $InputObject }) | Start-RSJob @jobArguments | Out-Null
        } #endregion
    }

    end {#region Wait for results and return them
        if ($Parallel) {
            Get-RSJob -batch $batch | Wait-RSJob -ShowProgress | Out-Null
        }#endregion
    }
}

$obj = New-Object -TypeName PSObject -Property @{
    Name = 'server1'
    Other = 'other'
}
$list2 = $obj
$list3 = "item1-item2"
$list2 | foo -fn 'Manage' -number 2 -option Q -List $list3

This is the error:

The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

Does anyone know how to get this working so I can pass a list into the runspace?

CuriousOne
  • 922
  • 1
  • 10
  • 26

0 Answers0