2014-10-10 88 views
0

我在当前的PowerShell会话中创建了function。现在我想保存它以备后用。怎么样?从当前会话导出功能

例如,我如何在随后的会话中使用它?

function xfirefox { 
    Start-Process firefox.exe $args 
} 

我知道我只是把它放到我的profile。我希望能够运行类似Export-Function xfirefox myFunctions.txt的东西。

+0

你需要建立一个配置文件并保存它http://blogs.technet.com/b/heyscriptingguy/archive/2012/05/21/understanding-the-six-powershell-profiles.aspx – Matt 2014-10-10 00:52:57

回答

2

也许把它放在你的档案中。删除 - 什么时候准备好。

function say ($msg) { 
    Write-Host $msg 
} 

function Export-Function ($Name) { 
    $fx = (gcm -Name $Name -CommandType Function) 
    if ($fx) { 
@" 
function $($fx.Name) { 
    $($fx.Definition)} 
"@ | Out-File -FilePath $profile -Append -Encoding ascii -WhatIf 
    } 
} 

Export-Function say 
+0

这是一个完美的想法,我还没有尝试过。谢谢。 – 2014-10-10 01:13:11