Is there anything special that I need to do to import a PowerShell module in C#?
I am trying to use the excellent PSTerminalServices module (https://psterminalservices.codeplex.com/) to perform TS tasks.
The following works perfectly in a PowerShell session (with elevated privileges):
PS> Import-Module PSTerminalServices
PS> Get-TSServers
However, if I attempt to use C#, I do not receive any errors when I try to import the module, but I receive the following error when running the cmdlet Get-TSServers:
"The term 'Get-TSServers' is not recognized as the name of a cmdlet, function, script file, or operable program..."
which seems to indicate that the module was not imported successfully.
My code is as follows:
var ps = PowerShell.Create();
ps.Commands.AddCommand("Import-Module").AddArgument("PSTerminalServices");
ps.Invoke();
ps.Commands.AddCommand("Get-TSServers");
Collection<PSObject> results = ps.Invoke();
I also tried using InitialSessionState, as described here:
http://nivot.org/blog/post/2010/05/03/PowerShell20DeveloperEssentials1InitializingARunspaceWithAModule
... and received the same error.
Is there any way to check if the module imported successfully?
Thank you.