Powershell 和 C# 连接代理
我们在使用 C# 脚本通过 Powershell 配置连接代理时遇到问题。
当我们导入模块remotedesktopservices
时,因为之前已经安装并使用了会话主机角色,然后安装了连接代理角色,所以我们必须重新启动脚本才能获得正确的输出。
如果我们直接使用 Powershell,我们可以重现该行为。
这些是我们调用的命令:
PS C:\Windows\system32> import-module servermanager
PS C:\Windows\system32> import-module remotedesktopservices
PS C:\Windows\system32> add-windowsfeature rds-connection-broker
Success Restart Needed Exit Code Feature Result
------- -------------- --------- --------------
True No Success {Remotedesktop-ConnectionBroker}
PS C:\Windows\system32> ls rds:
Directory: RDS:
Name Type CurrentValue GP PermissibleValues PermissibleOperations
---- ---- ------------ -- ----------------- ---------------------
RDSConfiguration Container - Get-Item, Get-ChildItem
RemoteApp Container - Get-Item, Get-ChildItem
PS C:\Windows\system32> import-module -force remotedesktopservices
PS C:\Windows\system32> ls rds:
Directory: RDS:
Name Type CurrentValue GP PermissibleValues PermissibleOperations
---- ---- ------------ -- ----------------- ---------------------
RDSConfiguration Container - Get-Item, Get-ChildItem
RemoteApp Container - Get-Item, Get-ChildItem
重新启动 Powershell 或我们的程序后,结果(正确地)如下所示:
PS C:\Windows\system32> ls rds:
Directory: RDS:
Name Type CurrentValue GP PermissibleValues PermissibleOperations
---- ---- ------------ -- ----------------- ---------------------
RDSConfiguration Container - Get-Item, Get-ChildItem
RDSFarms Container - Get-Item, Get-ChildItem
RemoteApp Container - Get-Item, Get-ChildItem
ConnectionBroker Container - Get-Item, Get-ChildItem
我们需要此输出,而无需重新启动程序。 是否有可能在 C# 中获得全新的 Powershell? “删除模块”和“导入模块”没有帮助。
这是我们获取 Powershell 运行空间的 C# 代码:
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ThrowOnRunspaceOpenError = true;
iss.ImportPSModule(initialRoles);
RemoteHost.powerShellRunspace = RunspaceFactory.CreateRunspace(iss);
RemoteHost.powerShellRunspace.Open();
即使我们以相同的方式创建一个新的 powershell 运行空间,似乎旧的 Powershell 已加载
We have a problem using a C# script to configure the connection broker with the Powershell.
When we import the module remotedesktopservices
, because the session host role was installed and used before, then install the connection broker role, we have to restart our script to get the right output.
We can reproduce the behavior if we use the Powershell directly.
These are the commands we call:
PS C:\Windows\system32> import-module servermanager
PS C:\Windows\system32> import-module remotedesktopservices
PS C:\Windows\system32> add-windowsfeature rds-connection-broker
Success Restart Needed Exit Code Feature Result
------- -------------- --------- --------------
True No Success {Remotedesktop-ConnectionBroker}
PS C:\Windows\system32> ls rds:
Directory: RDS:
Name Type CurrentValue GP PermissibleValues PermissibleOperations
---- ---- ------------ -- ----------------- ---------------------
RDSConfiguration Container - Get-Item, Get-ChildItem
RemoteApp Container - Get-Item, Get-ChildItem
PS C:\Windows\system32> import-module -force remotedesktopservices
PS C:\Windows\system32> ls rds:
Directory: RDS:
Name Type CurrentValue GP PermissibleValues PermissibleOperations
---- ---- ------------ -- ----------------- ---------------------
RDSConfiguration Container - Get-Item, Get-ChildItem
RemoteApp Container - Get-Item, Get-ChildItem
After a restart of the Powershell, or our programm, the result looks (correctly) like this:
PS C:\Windows\system32> ls rds:
Directory: RDS:
Name Type CurrentValue GP PermissibleValues PermissibleOperations
---- ---- ------------ -- ----------------- ---------------------
RDSConfiguration Container - Get-Item, Get-ChildItem
RDSFarms Container - Get-Item, Get-ChildItem
RemoteApp Container - Get-Item, Get-ChildItem
ConnectionBroker Container - Get-Item, Get-ChildItem
We need this output without a restart of our program.
Is it possible to get a fresh new Powershell in C#? "remove-module" and "import-module" did not help.
This is our C# code to get the Powershell runspace:
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ThrowOnRunspaceOpenError = true;
iss.ImportPSModule(initialRoles);
RemoteHost.powerShellRunspace = RunspaceFactory.CreateRunspace(iss);
RemoteHost.powerShellRunspace.Open();
Even if we create a new powershell runspace in the same way, it seems the old Powershell is loaded
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C Sharp 中有 Power Shell Class 看看。它提供了 Power Shell 的几乎所有功能。
http://msdn .microsoft.com/en-us/library/system.management.automation.powershell%28v=vs.85%29.aspx
您也可以刷新 power shell。
让我知道这是否能解决您的问题或我这边的问题。
There is Power Shell Class in C sharp have a look at that. It provides all most all the functionality of Power Shell.
http://msdn.microsoft.com/en-us/library/system.management.automation.powershell%28v=vs.85%29.aspx
You can refresh power shell also.
Let me know if this will solve your problem, or something from my side.