回答

7

可以使函数本身重新写,基本上成为首调用后一个空操作:

function TestFunction 
{ 
    'Do Stuff' 
    function Script:TestFunction { Return } 
} 
+0

太棒了!那就是我一直在寻找的东西。简单的东西,谢谢! –

+0

这似乎对整个PowerShell会话都有效?有没有办法可以检查每个脚本运行,即使在相同的PowerShell会话? –

+0

您是在ISE中测试还是在实际的Powershell控制台会话中测试?脚本:TestFunction应该将其隔离为当前脚本的范围,但是在ISE中的范围作用有点不同。 – mjolinor

1

像这样的东西可以帮助你:

function A 
{ 
    Write-Host "calling function A" 
} 

function B 
{ 
    Write-Host "calling function B" 
} 

function C 
{ 
    Write-Host "calling function C" 
} 

function All 
{ 
    A 

    if (!$script:BHasBeenCalled) 
     { B } 

    C 
} 

Get-PSBreakpoint | Remove-PSBreakpoint 
Set-PSBreakpoint -Command B -Action { $script:BHasBeenCalled = $true } | Out-Null 

$script:bHasBeenCalled = $false 

#First call: B should be invoked 
All 

#Second call: B shouldn't be invoked 
All 
-1

如果该功能已经被称为,它被加载并出现在函数Ps驱动器中。

function test1 { 
    # define a function 
    function test2 {return} 
    # call it 
    test2 
    # check it was called 
    dir function:\test2 
} 
test1