Powershell and a C# Connection Broker
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, G开发者_Python百科et-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
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.
精彩评论