[英]How to import custom PowerShell module into the remote session?
我正在开发一个自定义 PowerShell 模块,我想在与另一台计算机的远程会话的上下文中使用它。 以下代码(显然不起作用)解释了我想要实现的目标:
import-module .\MyCustomModule.psm1
$session = new-pssession -computerName server01
invoke-command -session $session -scriptblock {
<# use function defined in MyCustomModule here #>
}
第一个问题是是否有可能实现这种场景? 我的意思是我只希望我的自定义模块实际存在于我的机器上,而不是远程服务器上。
我找到了这个线程,但我没有设法让它工作 - 它不允许从远程机器创建一个会话回到本地机器。 可能,我遇到了该线程评论中某处提到的配置限制......此外,作者提到了对我的解决方案至关重要的性能影响......
如果可以,那怎么做?
PowerShell 的版本目前不是限制条件 - 如果该解决方案仅在 PS 3.0 中可用 - 我可以接受。
对这个问题有一些很好的评论,我花了一些时间研究解决这个问题的各种方法。
首先,我最初要求的是不可能的。 我的意思是,如果您采用模块方式,那么该模块应该物理存在于目标机器上,以便能够将Import-Module
导入远程会话。
为了进一步抽象我的问题,我正在尝试为产品部署创建一个可重用的基于 PowerShell 的框架。 这将是一种推送方式部署,这意味着我们鼓励人们在本地机器上运行一些脚本以部署到某个远程服务器。 就我调查该地区而言,有两种可能的方式符合常识。
要遵循的过程:
*.psm1
)PSModulePath
变量以包含新模块的位置Invoke-Command -Session $s -ScriptBlock {...}
Import-Module CustomModule
开始的脚本块中 - 它会在远程机器上搜索CustomModule
并且显然会找到它以下是喜欢这种方法的原因:
以下是需要考虑的重要事项:
xcopy
。 此外,交付机制应该支持升级/降级和(最好)多实例安装,但这与我的任务更相关,而不是一般问题要遵循的过程:
Invoke-Command -Session $s -FilePath .\\myscript.ps1
将脚本中定义的函数加载到远程会话Invoke-Command -Session $s -ScriptBlock {...}
并引用您的自定义函数 - 它们将在会话中以下是这种方法的优点:
当然,这并不理想:
最后,我应该说远程机器仍然需要为远程处理做好准备。 这就是我的意思:
Set-ExecutionPolicy Unrestricted
Enable-PSRemoting
这是另一种方法:在远程会话中重新创建模块,而不复制任何文件。
我没有尝试处理模块之间的依赖关系,但这对于简单的自包含模块似乎可以正常工作。 它依赖于在本地会话中可用的模块,因为这使得确定导出更容易,但通过一些额外的工作,它也可以与模块文件一起使用。
function Import-ModuleRemotely([string] $moduleName,[System.Management.Automation.Runspaces.PSSession] $session)
{
$localModule = get-module $moduleName;
if (! $localModule)
{
write-warning "No local module by that name exists";
return;
}
function Exports([string] $paramName, $dictionary)
{
if ($dictionary.Keys.Count -gt 0)
{
$keys = $dictionary.Keys -join ",";
return " -$paramName $keys"
}
}
$fns = Exports "Function" $localModule.ExportedFunctions;
$aliases = Exports "Alias" $localModule.ExportedAliases;
$cmdlets = Exports "Cmdlet" $localModule.ExportedCmdlets;
$vars = Exports "Variable" $localModule.ExportedVariables;
$exports = "Export-ModuleMember $fns $aliases $cmdlets $vars;";
$moduleString= @"
if (get-module $moduleName)
{
remove-module $moduleName;
}
New-Module -name $moduleName {
$($localModule.Definition)
$exports;
} | import-module
"@
$script = [ScriptBlock]::Create($moduleString);
invoke-command -session $session -scriptblock $script;
}
我不相信这是在没有任何“黑客”的情况下立即支持的。 明智的做法可能是将模块放在文件服务器等公共位置,并在需要时将其导入服务器。 前任:
$session = new-pssession -computerName server01
invoke-command -session $session -scriptblock {
#Set executionpolicy to bypass warnings IN THIS SESSION ONLY
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
#Import module from public location
Import-Module \\fileserver\folders\modulelocation...
<# use function defined in MyCustomModule here #>
}
如何从您的自定义函数中制作脚本块并使用Invoke-command
将其发送到 terget 服务器
Import-module YourModule
$s = [scriptblock]::Create($(get-item Function:\Your-ModuleFunction).Definition)
Invoke-Command -ScriptBlock $s -Computername s1,s2,sn
感谢这个线程它很有帮助......
但我实际上重写了这个函数。
请注意,本文中的原始函数或重写的函数均不包含模块清单数据。 所以你不能依赖模块的版本检查。
function Import-ModuleRemotely {
Param (
[string] $moduleName,
[System.Management.Automation.Runspaces.PSSession] $session
)
Import-Module $moduleName
$Script = @"
if (get-module $moduleName)
{
remove-module $moduleName;
}
New-Module -Name $moduleName { $($(Get-Module $moduleName).Definition) } | Import-Module
"@
Invoke-Command -Session $Session -ScriptBlock {
Param($Script)
. ([ScriptBlock]::Create($Script))
Get-Module
} -ArgumentList $Script
}
从 PS 5.0 开始,我认为现在有另一种更简洁的方法:
利用 Copy-Item 的 ToSession 参数将本地模块复制到远程机器。
这不涉及以前解决方案的缺点:
用法示例:
$s = New-PSSession MyTargetMachine
Get-Module MyLocalModule | Import-LocalModuleToRemoteSession -Session $s -Force
# Show module is loaded
Invoke-Command $s -ScriptBlock { Get-Module }
Import-LocalModuleToRemoteSession 函数
请注意,它不会加载模块依赖项
<#
.SYNOPSIS
Imports a loaded local module into a remote session
.DESCRIPTION
This script copies a module's files loaded on the local machine to a remote session's temporary folder and imports it, before removing the temporary files.
It does not require any shared folders to be exposed as it uses the default Copy-To -ToSession paramter (added in PS 5.0).
#>
function Import-LocalModuleToRemoteSession
{
[CmdletBinding()]
param(
# Module to import
[Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,Mandatory)]
[System.Management.Automation.PSModuleInfo]$ModuleInfo,
# PSSession to import module to
[Parameter(Mandatory)]
[System.Management.Automation.Runspaces.PSSession]
$Session,
# Override temporary folder location for module to be copied to on remote machine
[string]
$SessionModuleFolder=$null,
[switch]
$Force,
[switch]
$SkipDeleteModuleAfterImport
)
begin{
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
}
process{
if( [string]::IsNullOrWhiteSpace($SessionModuleFolder) ){
Write-Verbose "Creating temporary module folder"
$item = Invoke-Command -Session $Session -ScriptBlock ${function:New-TemporaryDirectory} -ErrorAction Stop
$SessionModuleFolder = $item.FullName
Write-Verbose "Created temporary folder $SessionModuleFolder"
}
$directory = (Join-Path -Path $SessionModuleFolder -ChildPath $ModuleInfo.Name)
Write-Verbose "Copying module $($ModuleInfo.Name) to remote folder: $directory"
Copy-Item `
-ToSession $Session `
-Recurse `
-Path $ModuleInfo.ModuleBase `
-Destination $directory
Write-Verbose "Importing module on remote session @ $directory "
try{
Invoke-Command -Session $Session -ErrorAction Stop -ScriptBlock `
{
Get-ChildItem (Join-Path -Path ${Using:directory} -ChildPath "*.psd1") `
| ForEach-Object{
Write-Debug "Importing module $_"
Import-Module -Name $_ #-Force:${Using:Force}
}
if( -not ${Using:SkipDeleteModuleAfterImport} ){
Write-Debug "Deleting temporary module files: $(${Using:directory})"
Remove-Item -Force -Recurse ${Using:directory}
}
}
}
catch
{
Write-Error "Failed to import module on $Session with error: $_"
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.