在脚本中执行所有函数 [英] Execute All Functions In A Script
本文介绍了在脚本中执行所有函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个具有某些功能的脚本,例如:
If I have a script with some functions such as:
function FunctionOne{}
function FunctionTwo{}
如何在同一脚本中的一行中调用它们,而不必指定每个函数的名称?
How can I call them all in one line, in the same script, without having to specify the name of each function?
我想做类似的事情:
Call-AllFunctionsInCurrentScriptConsecutively #calls FunctionOne then FunctionTwo
推荐答案
这篇博文 建议使用ParseFile()
用于解析脚本:
This blog post suggests using ParseFile()
for parsing the script:
$filename = 'C:\path\to\your.ps1'
[ref]$tokens = $null
[ref]$parseErrors = $null
$ast = [Management.Automation.Language.Parser]::ParseFile($filename, $tokens, $parseErrors)
然后您可以通过调用运算符调用函数(在您点源脚本之后):
Then you can invoke the functions (after you dot-sourced the script) via the call operator:
$ast.EndBlock.Statements | Where-Object { $_.Name } | ForEach-Object { & $_.Name }
要在脚本中使用它,请将文件名替换为 $MyInvocation.MyCommand.Path
.
For using this from within a script replace the filename with $MyInvocation.MyCommand.Path
.
这篇关于在脚本中执行所有函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文