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

时间:2014-07-03 12:23:40

标签: powershell

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

给出以下文件夹结构:

C:.
│   MyScript.ps1
│
└───MyModules
    ├───Deployment
    │       Deployment.psm1
    │
    └───Logging
            Logging.psm1

每个文件的以下内容:

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

Deployment.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

和Logging.psm1:

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

Export-ModuleMember -Function Log-Debug

输出结果为:

C:\Temp\Deploy2>powershell .\MyScript.ps1
Do-Something outputting a message via Log-Debug
Do-Something outputting a message directly

但我希望如此:

C:\Temp\Deploy2>powershell .\MyScript.ps1
Do-Something outputting a message directly

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

正如在MyScript.ps1的源代码中所提到的,我试图使用Set-Item重新定义函数,只是重新声明函数无效....

2 个答案:

答案 0 :(得分:2)

我猜想只需定义一个具有相同名称的空函数就足够了。实际上,当两个模块都直接在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:\> _

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

但是,您应该可以使用command precedence覆盖该功能。由于首先评估别名,因此可以为自定义日志记录功能定义别名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:\> _

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

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:\> _

修改

要在脚本中完成此工作,您需要将函数和别名定义为全局对象:

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:\> _

答案 1 :(得分:0)

使用Remove-Item function:尝试PSDrive

Remove-Item function:Log-Debug