Why is there no way to pass InitialSessionState to the remote RunspacePool?

Why is there no overload in the following method that takes a RunspaceConnectionInfo (to indicate the remote server information) as well as an InitialSessionState ?


Full context:

I am creating a RunspacePoolCache that will cache remote RunspacePools created with RunspaceFactory. The cache holds a key to information about the remote server. Prior to the RunspacePoolStateInfo.State Open pool, the same RunspacePool will be used to execute Powershell scripts on the same remote server. (Shameless plug: will this work?)

Now I want to add a set of Powershell snap-ins that are common to the generated RunspacePool. Adding snap-ins to an executable script from time to time results in the following exception:

An item with the same key has already been added


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

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


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


source to share

1 answer

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


A pool of spaces is created as follows:

$runspacePool =    [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool($minRunspaces, $maxRunspaces, $iss, $Host)




All Articles