We have a problem using a C # script to configure a connection broker using Powershell.
When we import the module remotedesktopservices, since the session host role was installed and used earlier, then install the connection broker role, we must restart our script to get the correct output.
We can reproduce the behavior if we use 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 restarting Powershell or our program, the result looks (correctly) as follows:
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 reloading our program. Is it possible to get a new version of Powershell in C #? "remove-module" and "import-module" did not help.
This is our C # code to get a Powershell space:
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ThrowOnRunspaceOpenError = true;
iss.ImportPSModule(initialRoles);
RemoteHost.powerShellRunspace = RunspaceFactory.CreateRunspace(iss);
RemoteHost.powerShellRunspace.Open();
powershell , , Powershell