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

Gra*_*len 3 powershell powershell-4.0 type-accelerators

我无法弄清楚如何正确地转换PSSession以用作函数中的参数.

我是否需要装载组件或其他东西?我正在使用Powershell v4.

我喜欢投射我的函数参数以确保正确使用.我正在尝试的是:

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

但是在转换参数时出现此错误:

Unable to find type [PSSession]. Make sure that the assembly that contains this type is loaded.
Run Code Online (Sandbox Code Playgroud)

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

IsPublic IsSerial Name                                     BaseType                                                                                                                           
-------- -------- ----                                     --------                                                                                                                           
True     False    PSSession                                System.Object    
Run Code Online (Sandbox Code Playgroud)

所以看起来应该是正确的类型名称......

所有帮助表示赞赏.

Ben*_*enH 10

试试这个:

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

  • @Grallen 1)您可以随时接受并更改.2)这是正确的答案.没有任何上下文我知道你可以单独使用`[PSSession]`.检查`$ mySession.GetType().FullName`.这是`$ mySession.GetType().Namespace`和`$ mySession.GetType().Name`的串联. (3认同)