在PSersmowing 4.0中将PSRemotingJob对象作为参数传递

时间:2016-06-23 15:26:03

标签: powershell

我正在编写一个Powershell Cmdlet,我需要传递给PSRemotingJob 对象作为参数。 MCVE如下:

function My-Cmdlet {
    [CmdletBinding()]
    Param(
        [Parameter(Position=0,
                   Mandatory=$true,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [PSRemotingJob[]]$Job
    )

    BEGIN {}

    PROCESS {
        ForEach ($j in $Job) {
            $j
        }
    }

    END {}
}

问题是当我将作业传递给cmdlet时,我收到错误,如下所示:

PS C:\Temp> Invoke-Command -AsJob -CN svr001 -Command {Start-Sleep 10} | My-Cmdlet
My-Cmdlet : Unable to find type [PSRemotingJob]. Make sure that the assembly that contains this type is loaded.
At line:1 char:63
+ Invoke-Command -AsJob -CN svr001 -Command {Start-Sleep 10} | My-Cmdlet
+                                                              ~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (PSRemotingJob:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

PS C:\Temp>

我意识到这应该是替换正确对象的简单问题 类型或完全限定的对象,但我也尝试过使用 [System.Management.Automation.PSRemotingJob]具有相同的结果。

我正在使用Powershell 4.0。

1 个答案:

答案 0 :(得分:5)

System.Management.Automation.PSRemotingJob不是公共类型,因此无法用PowerShell类型语法表示。但您可以使用其基本公共类型:[System.Management.Automation.Job]