2

In C#, InitialSessionState class provides a way to import specific modules and make it available to all the runspaces in the runspacepool using "ImportPSModule" method. (But this imports all cmdlets in the module being imported)

To import specific cmdlets from a module, "Import-Module" command accepts a parameter "Cmdlet", using which we can import only specific cmdltes from that module. How to achieve this behaviour (importing only specific cmdlets from module) in Runspacepool

Praveen Kumar
  • 946
  • 3
  • 10
  • 29
  • Why is this needed? If it is needed for performance/resource reasons then I would not bother, I do not think there is a significant difference, if any. – Roman Kuzmin Aug 10 '15 at 17:03

1 Answers1

2

You can add commands to the Commands Property of your InitialSessionState instance (here using a Cmdlet type command as an example):

InitialSessionState iss = InitialSessionState.CreateDefault();
SessionStateCmdletEntry resolveCmdlet = new SessionStateCmdletEntry("Resolve-DnsName", 
    typeof(Microsoft.DnsClient.Commands.ResolveDnsName), 
    "dnslookup.dll-Help.xml");
iss.Commands.Add(resolveCmdlet);
Runspace rs = RunspaceFactory.CreateRunspace(iss);
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206