使用 Uri 创建时,SessionStateProxy 属性 在新的运行空间对象中为空

SessionStateProxy property is empty at new Runspace object when created using Uri

当我使用 PS 3.0+ 时,我的远程运行空间工作正常,但一旦我 运行 我的代码使用 PS 2.0,SessionStateProxy 属性 为空(但仅当我尝试创建远程运行空间时。

powershell -version 2

$Uri = New-Object System.Uri("http://WIN-10NL6N4THGJ:5985/wsman")
$connectionInfo = New-Object System.Management.Automation.Runspaces.WSManConnectionInfo($Uri)
$runspace = [runspacefactory]::CreateRunspace($connectionInfo)

$runspace.Open()

$runspace |select *

$runspace.SessionStateProxy

SessionStateProxy 属性 应该是 System.Management.Automation.RemoteSessionStateProxy 但它是 $null。有什么线索吗?

SessionStateProxy 不适用于 PowerShell 2.0 中的远程运行空间。我没有任何文档 atm。备份它,但你可以自己验证它。

本地运行空间:

$r.SessionStateProxy.GetType().FullName
System.Management.Automation.Runspaces.SessionStateProxy

远程运行空间(PS 4.0):

$runspace.SessionStateProxy.GetType().Fullname
System.Management.Automation.RemoteSessionStateProxy

如果您使用 dotPeek 之类的工具查看 PowerShell 2.0 System.Management.Automation.dll v.1.0.0 (file Version 6.1.7600.16385) 中的代码,您会发现 SessionStateProxy class 用于本地运行空间,但是 RemoteSessionStateProxy 不见了。如果您查看 System.Management.Automation.dll v.3.0.0 内部,您还会发现 RemoteSessionStateProxy internal-class.

解决方案:升级 PowerShell (WMF 3.0 - 5.0)