Skip to content

Instantly share code, notes, and snippets.

@Iristyle
Created April 26, 2012 17:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Iristyle/2501208 to your computer and use it in GitHub Desktop.
Save Iristyle/2501208 to your computer and use it in GitHub Desktop.
WinRM - Import module source files to remote machine
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
}
@Iristyle
Copy link
Author

Usage

$session = New-PSSession -ComputerName Foo
Export-SourceModulesToSession $session -ModulePaths '.\module.psm1','.\module2.psm1'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment