我想模仿一个带有winform的powershell/powershell-ISE控制台。使用Powershell.Create()/Invoke(),我几乎可以做任何事情来复制这种行为,但是在进入远程会话时,我遗漏了一些关于运行空间变化的内容。考虑下面的守则:
PowerShell ps = PowerShell.Create();
ps.AddScript("$i = 1").Invoke();
ps.AddScript("Enter-PSSession -computer localhost").Invoke();
var obj = ps.AddScript("$i").Invoke();
// obj contains 1, whereas I would expect not to find $i我想知道如何使用户透明,输入enter-pssession实际上会改变运行空间,就像powershell.exe一样。
发布于 2022-05-15 20:19:41
进入-PSSession在PowerShell SDK的上下文中不受支持。
实际上,它甚至不受常规脚本或CLI的支持;它只有在从交互式 PowerShell session调用时才能工作-参见GitHub问题#16350。
你会发现问题所在:
ps.Streams.Error检查- When you use `.AddScript()`, any errors that occur during execution of the PowerShell code do _not_ surface _as exceptions_ in your C# program; instead, you must examine the `.Streams.Error` collection (`.HadErrors` is a Boolean that indicates whether any errors occurred) - see [this answer](https://stackoverflow.com/a/69051991/45375) for details.您将看到以下错误,这可能与SDK使用的PowerShell主机有关,Default Host缺少Enter-PSSession所需的特性
输入-PSSession:方法或操作未实现。
https://stackoverflow.com/questions/72249565
复制相似问题