如何重新定义由 powershell 模块导出的函数

Pet*_*voy 5 powershell

有没有办法可以重新定义由模块导出并由另一个模块使用的函数?

鉴于以下文件夹结构:

C:.
?   MyScript.ps1
?
????MyModules
    ????Deployment
    ?       Deployment.psm1
    ?
    ????Logging
            Logging.psm1
Run Code Online (Sandbox Code Playgroud)

以及每个文件的以下内容:

MyScript.ps1:

#Add MyModules to the module path
$cleanedModulePath = @()
$env:PSModulePath -split ";" | %{
    if( !( $_ -Match "MyModules") ){
        $cleanedModulePath += $_
    }
}
$thisDir = (Split-Path -parent $script:MyInvocation.MyCommand.Definition)
$moduleDir = Join-Path $thisDir "MyModules"
$cleanedModulePath += $moduleDir
$env:PSModulePath = ($cleanedModulePath -Join ";")

Import-Module Deployment -DisableNameChecking

#Neither of these manage to redefine Log-Debug - even if put before the Import
function Log-Debug{}
Set-Item -Path function:Log-Debug -Value {} -Option "AllScope,ReadOnly" -Force

Do-Something
Run Code Online (Sandbox Code Playgroud)

部署.psm1:

Import-Module Logging -DisableNameChecking

function Do-Something {
    Log-Debug "Do-Something outputting a message via Log-Debug"
    Write-Host "Do-Something outputting a message directly"
}


Export-ModuleMember -Function Do-Something
Run Code Online (Sandbox Code Playgroud)

和 Logging.psm1:

function Log-Debug {
    param([string] $message)
    Write-Host $message
}

Export-ModuleMember -Function Log-Debug
Run Code Online (Sandbox Code Playgroud)

输出是:

C:\Temp\Deploy2>powershell .\MyScript.ps1
Do-Something outputting a message via Log-Debug
Do-Something outputting a message directly
Run Code Online (Sandbox Code Playgroud)

但我希望它是:

C:\Temp\Deploy2>powershell .\MyScript.ps1
Do-Something outputting a message directly
Run Code Online (Sandbox Code Playgroud)

我意识到 Logging 模块的更好实现将允许我控制日志级别,但这不是这个问题的重点:是否可以重新定义/覆盖 Log-Debug 使其成为空操作?

正如 MyScript.ps1 的源代码中所指出的,我尝试使用 Set-Item 重新定义函数,只是重新声明函数无济于事....

Ans*_*ers 4

我猜想简单地定义一个具有相同名称的空函数就足够了。当两个模块都直接在 PowerShell 中导入时,实际上确实如此:

PS C:\> $modules = "$env:USERPROFILE\Documents\WindowsPowerShell\Modules"
PS C:\> Get-Content "$modules\Logging\Logging.psm1"
function Log-Debug($msg) {
  Write-Host "DEBUG: $msg"
}
Export-ModuleMember -Function Log-Debug
PS C:\> Get-Content "$modules\Deployment\Deployment.psm1"
function Foo($msg) {
  Write-Output 'FOO: something'
  Log-Debug $msg
}
Export-ModuleMember Foo
PS C:\> Import-Module Logging -DisableNameChecking
PS C:\> Import-Module Deployment
PS C:\> Foo 'bar'
FOO: something
DEBUG: bar
PS C:\> function Log-Debug {}
PS C:\> Foo 'bar'
FOO: something
PS C:\> _
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,您无法重新定义或删除另一个模块从一个模块导入的函数(或者至少我没有找到这样做的方法)。

但是,您应该能够通过使用命令优先级来覆盖该函数。由于首先评估别名,因此您可以Log-Debug为自定义日志记录函数定义别名:

PS C:\> $modules = "$env:USERPROFILE\Documents\WindowsPowerShell\Modules"
PS C:\> Get-Content "$modules\Logging\Logging.psm1"
function Log-Debug($msg) {
  Write-Host "DEBUG: $msg"
}
Export-ModuleMember -Function Log-Debug
PS C:\> Get-Content "$modules\Deployment\Deployment.psm1"
Import-Module Logging -DisableNameChecking
function Foo($msg) {
  Write-Output 'FOO: something'
  Log-Debug $msg
}
Export-ModuleMember Foo
PS C:\> Import-Module Deployment
PS C:\> Foo 'bar'
FOO: something
DEBUG: bar
PS C:\> function Log-DebugSuperseded {}
PS C:\> Set-Alias -Name Log-Debug -Value Log-DebugSuperseded
PS C:\> Foo 'bar'
FOO: something
PS C:\> _
Run Code Online (Sandbox Code Playgroud)

这样,您甚至可以动态更改日志记录实现,或者在原始日志记录功能和自定义日志记录功能之间来回切换:

PS C:\> Foo 'bar'
FOO: something
DEBUG: bar
PS C:\> function Log-DebugSuperseded {}
PS C:\> Set-Alias -Name Log-Debug -Value Log-DebugSuperseded
PS C:\> Foo 'bar'
FOO: something
PS C:\> function Log-DebugSuperseded {'baz'}
PS C:\> Foo 'bar'
FOO: something
baz
PS C:\> Remove-Item Alias:\Log-Debug
PS C:\> Foo 'bar'
FOO: something
DEBUG: bar
PS C:\> _
Run Code Online (Sandbox Code Playgroud)

编辑:

要在脚本中实现此功能,您需要将函数和别名定义为全局对象:

PS C:\> Get-Content .\test.ps1
Import-Module Deployment
Write-Host '--- without alias ---'
Foo 'bar'
function global:Log-DebugSuperseded {}
Set-Alias -Name Log-Debug -Value Log-DebugSuperseded -Scope global
Write-Host '--- with alias ---'
Foo 'bar'
PS C:\> ./test.ps1
--- without alias ---
FOO: something
DEBUG: bar
--- with alias ---
FOO: something
PS C:\> _
Run Code Online (Sandbox Code Playgroud)