PSRemoting - PSSessionConfiguration - Using PSSnapin in ModulesToImport

Tagged: 

This topic contains 8 replies, has 3 voices, and was last updated by Profile photo of tommymaynard tommymaynard 2 years, 3 months ago.

  • Author
    Posts
  • #17152
    Profile photo of Don Hunt
    Don Hunt
    Participant

    I have been playing with creating my own PSSessionConfiguration in PS V3 on 2008 R2 Enterprise SP1. For the most part I've been able to successfully get most aspects working, but I am puzzled when it comes to using a PSSnapin in the ModulesToImport. Standard modules work fine.

    According to get-help the -ModulesToImport parameter is String[] with a default value of Microsoft.PowerShell.Core, and "Specifies the modules and [b]snap-ins [/b]that are automatically imported into sessions that use the session configuration."

    I can include a PSSnapin name in the ModulesToImport parameter of New-PSSessionConfigurationFile & I can then do the Register-PSSessionConfiguration using that file. The PSSessionConfiguration looks like this:

    PS C:\> Get-PSSessionConfiguration TestSnapinEndPoint | FL *
    
    Copyright                     : (c) 2014 Don Hunt. All rights reserved.
    Description                   : Trying to find syntax of PSSnapin in ModulesToImport
    CompanyName                   : Unknown
    GUID                          : 4054684b-38ee-4353-ae69-dee49c1dbf7f
    Author                        : Don Hunt
    ModulesToImport               : {WDeploySnapin3.0, BitsTransfer}
    SessionType                   : Default
    ExecutionPolicy               : RemoteSigned
    SchemaVersion                 : 1.0.0.0
    LanguageMode                  : FullLanguage
    Architecture                  : 64
    Filename                      : %windir%\system32\pwrshplugin.dll
    ResourceUri                   : http://schemas.microsoft.com/powershell/TestSnapinEndPoint
    MaxProcessesPerShell          : 15
    MaxConcurrentCommandsPerShell : 1000
    Capability                    : {Shell}
    xmlns                         : http://schemas.microsoft.com/wbem/wsman/1/config/PluginConfiguration
    MaxConcurrentUsers            : 5
    pssessionthreadapartmentstate : STA
    Name                          : TestSnapinEndPoint
    SupportsOptions               : true
    pssessionthreadoptions        : ReuseThread
    ExactMatch                    : true
    ConfigFilePath                : C:\Windows\System32\WindowsPowerShell\v1.0\SessionConfig\TestSnapinEndPoint_4054684b-38
                                    ee-4353-ae69-dee49c1dbf7f.pssc
    RunAsUser                     :
    IdleTimeoutms                 : 7200000
    OutputBufferingMode           : Block
    PSVersion                     : 3.0
    SecurityDescriptorSddl        : O:NSG:BAD:P(D;;GA;;;NU)(A;;GA;;;BA)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)
    MaxShellsPerUser              : 25
    AutoRestart                   : false
    MaxShells                     : 25
    MaxIdleTimeoutms              : 43200000
    Uri                           : http://schemas.microsoft.com/powershell/TestSnapinEndPoint
    SDKVersion                    : 2
    XmlRenderingType              : text
    RunAsPassword                 :
    ProcessIdleTimeoutSec         : 0
    ParentResourceUri             : http://schemas.microsoft.com/powershell/TestSnapinEndPoint
    Enabled                       : True
    UseSharedProcess              : false
    MaxMemoryPerShellMB           : 1024
    lang                          : en-US
    Permission                    : NT AUTHORITY\NETWORK AccessDenied, BUILTIN\Administrators AccessAllowed

    However, I get a runtime error when I try to use this configuration.

    PS C:\> $pssnap = New-PSSession -EnableNetworkAccess -ConfigurationName TestSnapinEndPoint
    New-PSSession : One or more errors occurred processing the module 'WDeploySnapin3.0' specified in the
    InitialSessionState object used to create this runspace. See the ErrorRecords property for a complete list of errors.
    The first error was: The specified module 'WDeploySnapin3.0' was not loaded because no valid module file was found in
    any module directory.
    At line:1 char:11
    + $pssnap = New-PSSession -EnableNetworkAccess -ConfigurationName TestSnapinEndPoi ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], RemoteExc
    eption
    + FullyQualifiedErrorId : PSSessionOpenFailed

    I have been able to work around this by specifying a ScriptsToProcess entry, which just did an Add-PSSnapin of the needed snap in.

    Any insight would be appreciated.

    Don

  • #17219
    Profile photo of Martin Nielsen
    Martin Nielsen
    Participant

    Are you sure the snapin is appropriately installed on the machine to which you're remoting?

    The first error was: The specified module 'WDeploySnapin3.0' was not loaded because no valid module file was found in any module directory.
  • #17237
    Profile photo of Don Hunt
    Don Hunt
    Participant

    As mentioned in my original post:[br]

    [blockquote]I have been able to work around this by specifying a ScriptsToProcess entry, which just did an Add-PSSnapin of the needed snap in.[/blockquote]

    So, if an

    Add-PSSnapin WDeploySnapin3.0

    is run on this machine it works fine.[br]

    The error messages output are consistent with doing an Import-Module rather than an Add-PSSnapin to make this resource available for use.

    Don

  • #17254
    Profile photo of Martin Nielsen
    Martin Nielsen
    Participant

    Right, I didn't notice the last line you put there.

    I get that the documentation says

    By default, only the Microsoft.PowerShell.Core snap-in is imported into remote sessions, but unless the cmdlets
    are excluded, users can use the Import-Module and Add-PSSnapin cmdlets to add modules and snap-ins to the session.
    
    Each module or snap-in in the value of this parameter can be represented by a string or as a hash table. A module
    string consists only of the name of the module or snap-in. A module hash table can include ModuleName,
    ModuleVersion, and GUID keys. Only the ModuleName key is required.
    

    but with that said, nothing seems to imply while running these commands, that it actually treats ModulesToImport like anything other than modules, and trying to register WDeploySnapin3.0 as a module will fail.

    Perhaps you ought to create an entry on connect and tell Microsoft about it, so they either fix the cmdlet to work as documented, or fix the documentation to prevent misunderstandings.

  • #17257
    Profile photo of Martin Nielsen
    Martin Nielsen
    Participant

    On the other hand, if you create a module of your own

    $snapin = Get-PSSnapin -Registered WDeploySnapin3.0
    Import-Module (Join-Path $snapin.ApplicationBase $snapin.ModuleName) -Verbose
    Export-ModuleMember -Cmdlet *
    

    Put it in C:\Program Files\WindowsPowerShell\Modules\TestModule (or name it something else) and then

    Register-PSSessionConfiguration -Name T -ModulesToImport 'C:\Program Files\WindowsPowerShell\Modules\TestModule'

    That at least does seem to work.

  • #17321
    Profile photo of Don Hunt
    Don Hunt
    Participant

    Martin,

    Thanks for taking the time to investigate this scenario. [br]
    I think I'll follow up as you suggested with a Connect entry to tell Microsoft about this.[br]

    As I was looking at the current set of PSSnapins on my server, it appeared that only one, WDeploySnapin3.0 could be handled by the technique of

    $snapin = Get-PSSnapin -Registered WDeploySnapin3.0
    Import-Module (Join-Path $snapin.ApplicationBase $snapin.ModuleName) -Verbose

    So, as a somewhat generic work around, given a list of PSnapin resources needed I can dynamically create a script which will do an Add-PSSnapin for each one. [br]
    This is not too much additional work on my part, but I was expecting a cleaner solution out of the box.[br]

    Thanks again,

    Don

  • #18005
    Profile photo of tommymaynard
    tommymaynard
    Member

    Please link your entry at connect.microsoft.com as I would like to vote for it. I have dealt with this for months and haven't been able to get a PowerShell EndPoint/Session Configuration to load a PSSnapin (SharePoint, in my case). I did the same thing, used ScriptsToProcess and a .ps1 file that runs the Add-PSSnapin cmdlet. At one point I added the PSSnapin's ApplicationBase to $env:PSModulePath – still no luck.

  • #18006
    Profile photo of Don Hunt
    Don Hunt
    Participant

    Tommy,

    Here is a link to the newly created entry at [b]connect.microsoft.com[/b]

    [url]https://connect.microsoft.com/PowerShell/feedbackdetail/view/945497/pssnapins-listed-in-modulestoimport-parameter-of-new-pssessionconfigurationfile-and-subsequent-register-pssessionconfiguration-cause-runtime-errors-upon-use-of-endpoint[/url]

  • #18007
    Profile photo of tommymaynard
    tommymaynard
    Member

    Thanks, Don. I've voted and commented.

You must be logged in to reply to this topic.