0

I'm trying to create a central repository for our company's modules. What I've done so far:

  1. Shared the module folder on the script server as read only

    C:\Program Files\WindowsPowerShell\Modules → shared as \\SERVER1\PS Modules

  2. Started a new PS session to the second server with Enter-PSsession SERVER2

  3. On SERVER2 I ran the following code found on the MS website:

    Function Add-ModulePath {
        [CmdletBinding(SupportsShouldProcess=$True)]
        Param(
            [parameter(Mandatory=$true,Position=0)]
            [ValidateNotNullOrEmpty()]
            [String]$Path
        )
        Process {
            if ($env:PSModulePath -like "*$Path*") {
                Write-Verbose "Add-ModulePath: Path '$Path' already present"       
            } 
            else {
                Write-Verbose "Add-ModulePath: Adding path '$Path' to the modules"
                #Save the current value in the $p variable.
                $p = [Environment]::GetEnvironmentVariable("PSModulePath")
                #Add the new path to the $p variable. Begin with a semi-colon separator.
                $p += ";$Path"
                #Add the paths in $p to the PSModulePath value.
                [Environment]::SetEnvironmentVariable("PSModulePath",$p)
            }
        }
    }
    Add-ModulePath -Path '\\SERVER1\PS Modules\' -Verbose
    

When I now try to do Get-Module -ListAvailable within the remote session to SERVER2 it doesn't show me the modules from the network share, although the network share shows up in $env:PSModulePath. SERVER2 is running PowerShell v2.0, but this doesn't matter I think. According to Microsoft I should "broadcast an environment message", but I don't know how to do this.

What we want to achieve is to have this module repo available to all servers in the park, so they all use the same modules when executing scripts.

When we try to load the module from the network share manually within the remote session, it says it can't be found. Although we can browse it perfectly fine from that machine:

[SERVER2]: PS C:\Documents and Settings\user\My Documents> Import-Module '\\SERVER1\PS Modules\Toolbox.General\Toolbox.General.psm1' -Verbose
Import-Module : The specified module '\\SERVER1\PS Modules\Toolbox.General\Toolbox.General.psm1' was not loaded because no valid module file was found in any module directory.
    + CategoryInfo          : ResourceUnavailable: (\\SERVER1...ox.General.psm1:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

When loading the module from an RDP session on SERVER2 it does work. So this might be related to the double hop issue..

How is it possible to load modules from a network share within a remote session?

My question is very much related to this, but I refuse to copy the modules to all the servers. I would like to have them available from one central location.

Community
  • 1
  • 1
DarkLite1
  • 9,743
  • 30
  • 88
  • 160
  • 1
    Changes to Environment variables are not picked up automatically. You can broadcast a message to all processes on the system using the Win32 SendMessageTimeout() function to alert all running processes that the global environment variables have been updated, but not every process will respond. The only guaranteed way to do this is to restart the target process so it picks up the changed environment. – Stephen Connolly Nov 18 '14 at 15:25
  • Thank you Stephen, but how can I restart the target process? After adding the new path `\\SERVER1\PS Modules` to `$env:PSModulePath`on `SERVER2` it's visible in that PowerShell session, but when I close it it's gone again.. – DarkLite1 Nov 19 '14 at 07:41
  • 1
    http://technet.microsoft.com/en-us/library/ff730964.aspx - use SetEnvironmentVariable with the Machine option to make the change at the machine level. – Stephen Connolly Nov 19 '14 at 13:47

0 Answers0