作为 Jonathan 提到的替代方案,如果您有源模块想要通过网络推送,那么您可以轻松地做到这一点。如果你有二进制文件,你也许可以做类似的事情。但我想说所有的赌注都在那里。本质上,您将文件作为哈希中的参数推送,写入临时文件,然后导入。
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
}
打电话喜欢
$session = New-PSSession -ComputerName Foo
Export-SourceModulesToSession $session -ModulePaths '.\module.psm1','.\module2.psm1'
理论上也是可能的,将当前的本地主机会话导出到模块并将其推送到网络上——未经测试的伪代码。这可能行不通...
$localSession = New-PSSession #defaults to localhost
# if you don't have modules automatically loading in the profile, etc, then manually load them
Invoke-Command -Computer $localSession -ScriptBlock { Import-Module 'foo'; Import-Module '.\module.ps1' }
Export-PSSession $localSession -OutputModule TempLocalModule
#now that you have TempLocalModule written out, it's possible you can send that thing across the wire in the same way