为什么我不能转换为 [PSSession] 声明函数参数?

Why can I not cast to [PSSession] declaring function parameters?

我不知道如何正确转换 PSSession 以用作函数中的参数。

我是否必须加载程序集或其他内容?我正在使用 Powershell v4.

我喜欢转换我的函数参数以确保正确使用。我正在尝试的是:

function Some-Remote-Task([PSSession] $Session, [String]$Target) {
  # Do stuff...
}

但是我在投射我的参数时遇到这个错误:

Unable to find type [PSSession]. Make sure that the assembly that contains this type is loaded.

此外,在有效会话上使用 $mySession.GetType() 会产生以下结果:

IsPublic IsSerial Name                                     BaseType                                                                                                                           
-------- -------- ----                                     --------                                                                                                                           
True     False    PSSession                                System.Object    

看来这应该是正确的类型名称...

感谢所有帮助。

试试这个:

function Some-Remote-Task([System.Management.Automation.Runspaces.PSSession]$Session, [String]$Target) {
  # Do stuff...
}

编辑:

我现在可以正常使用[PSSession]了。

结合 biantist 评论中 link 的信息:Type Accelerator

这里有另一个答案:Simplify Your Script...

我正确添加了类型加速器

PS c:\> [PowerShell].Assembly.GetType("System.Management.Automation.TypeAccelerators")::add(“PSSession”,”System.Management.Automation.Runspaces.PSSession”)

PS c:\> [PSSession]

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    PSSession                                System.Object

是:

最后我用了别名。发布给同样喜欢干净外观的其他人。

New-Alias PSSession System.Management.Automation.Runspaces.PSSession

-ErrorAction SilentlyContinue 如果您不断重新运行相同的代码段,在测试期间添加它很有用。