0

我正在修改下面的代码。它应该创建一个 PoshRSJob 并foo在那里的运行空间中再次运行该函数。

我希望能够将$list参数转换为[array]or [string[]],但是当我这样做时会引发错误。我考虑将我的数组展平为一个字符串,但如果我更改$list3为在字符串中包含空格或逗号,它也会引发错误。我相信是这条线导致了这个问题,但我不知道为什么或怎么做才能绕过这个问题:

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

代码:

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

这是错误:

输入对象不能绑定到命令的任何参数,因为命令不接受管道输入,或者输入及其属性与接受管道输入的任何参数都不匹配。

有谁知道如何使它工作,以便我可以将列表传递到运行空间?

4

0 回答 0