5

Why the following method does not have an overload which accepts a RunspaceConnectionInfo (to specify the remote server info) as well as a InitialSessionState?

http://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.runspacefactory.createrunspacepool(v=vs.85).aspx

Full Context:

I am building a RunspacePoolCache, that would cache remote RunspacePools created using RunspaceFactory. Cache is keyed on remote server info. Till the pool's RunspacePoolStateInfo.State is Open, the same RunspacePool would be used for executing Powershell scripts on same remote server. (Shameless plug: Will this work?)

Now, I want to add a set of Powershell snap-ins that are common to the created RunspacePool. Adding the snap-ins within the executed script is at times leading to the following exception:

An item with the same key has already been added

This is happening even when I do the following in the Powershell script (but less often):

if ((Get-PSSnapin | ? { $_.Name -eq 'VeeamPSSnapIn' }) -eq $null) {
    Add-PsSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
}

That is where I am trying to load the snap-ins via InitialSessionState. But from the set of methods provided, it seems InitialSessionState can be specified only while creating local RunspacePools.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Jagan
  • 447
  • 5
  • 12
  • Was looking for the same thing and this pointed me in the right direction, cheers! Maybe this will help others - https://technet.microsoft.com/en-us/library/ee706578(v=vs.85).aspx – Andrew Morpeth Nov 05 '15 at 06:45

1 Answers1

0

To add snap-ins to the ISS you use the ImportPSSnapIn method. Example:

$iss = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
s.ImportPSSnapIn($snapName,[ref]'') | Out-Null

The you create the runspace pool like this:

$runspacePool =    [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool($minRunspaces, $maxRunspaces, $iss, $Host)
ojk
  • 2,502
  • 15
  • 17
  • But my question was, how to pass this iss to RunspaceFactory.CreateRunspacePool(), along with the RunspaceConnectionInfo? There is no method overload that accepts both. – Jagan Oct 09 '14 at 11:09
  • Updated my answer, but this is also not what you want I guess? Because you require the use of the RunspaceConnectionInfo object right? – ojk Oct 09 '14 at 11:16
  • Yep! :) . I am in need of specifying RunspaceConnectionInfo (WSManConnectionInfo in my case), as I want the Powershell script to run on a remote server. – Jagan Oct 09 '14 at 12:20
  • Have you tried to create the runspacepool by using one of the constructors that use the RunspaceConnectionInfo, and later add the ISS to the runspacepool object using the InitialSessionState property? Something like this: $pool.InitialSessionState = $iss – ojk Oct 09 '14 at 12:29
  • Tried that. Compiler is saying InitialSessionState is read-only property. – Jagan Oct 09 '14 at 14:08
  • Ok. I'm out of ideas. Could it be that the reason this is not supported is so that you won't end up in a scenario where you try to start a remote runspacepool trying to add snap-ins not available on the remote host? I find it a bit strange that this should be so hard though... – ojk Oct 09 '14 at 14:33