Created
April 26, 2012 17:38
-
-
Save Iristyle/2501208 to your computer and use it in GitHub Desktop.
WinRM - Import module source files to remote machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Export-SourceModulesToSession | |
{ | |
Param( | |
[Management.Automation.Runspaces.PSSession] | |
[ValidateNotNull()] | |
$Session, | |
[IO.FileInfo[]] | |
[ValidateNotNull()] | |
[ValidateScript( | |
{ | |
(Test-Path $_) -and (!$_.PSIsContainer) -and ($_.Extension -eq '.psm1') | |
})] | |
$ModulePaths | |
) | |
$remoteModuleImportScript = { | |
Param($Modules) | |
Write-Host "Writing $($Modules.Count) modules to temporary disk location" | |
$Modules | | |
% { | |
$path = ([IO.Path]::GetTempFileName() + '.psm1') | |
$_.Contents | Out-File -FilePath $path -Force | |
"Importing module [$($_.Name)] from [$path]" | |
Import-Module $path | |
} | |
} | |
$modules = $ModulePaths | % { @{Name = $_.Name; Contents = Get-Content $_ } } | |
$params = @{ | |
Session = $Session; | |
ScriptBlock = $remoteModuleImportScript; | |
Argumentlist = @(,$modules); | |
} | |
Invoke-Command @params | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
$session = New-PSSession -ComputerName Foo
Export-SourceModulesToSession $session -ModulePaths '.\module.psm1','.\module2.psm1'