有没有命令可以列出我在脚本中创建的所有函数?
比如我创建了 doXY 函数和 getABC 函数之类的。
然后我输入命令,它会显示:
- 函数 doXY
- 函数 getABC
这将是一个很酷的功能^^
感谢您的所有帮助。
有没有命令可以列出我在脚本中创建的所有函数?
比如我创建了 doXY 函数和 getABC 函数之类的。
然后我输入命令,它会显示:
这将是一个很酷的功能^^
感谢您的所有帮助。
Get-Command
可能是访问AST最简单的方式:# Use Get-Command to parse the script
$myScript = Get-Command .\path\to\script.ps1
$scriptAST = $myScript.ScriptBlock.AST
# Search the AST for function definitions
$functionDefinitions = $scriptAST.FindAll({
$args[0] -is [Management.Automation.Language.FunctionDefinitionAst]
}, $false)
# Report function name and line number in the script
$functionDefinitions |ForEach-Object {
Write-Host "Function '$($_.Name)' found on line $($_.StartLineNumber)!"
}
typset -f
是什么,但你无法在不读取文件的情况下枚举和执行文件中的函数定义。 - Mathias R. Jessendefine -f
。 - Timo如果你的脚本名叫 things.ps1
,那么类似于...
cat ./things.ps1 | grep function
For MacOS/Linux or...
cat ./things.ps1 | select-string function
适用于Windows。
Invoke-Expression
,以将返回的函数加载到当前范围中。function Get-Function {
<#
.SYNOPSIS
Returns one or more named functions from a .ps1 file without executing the file
.DESCRIPTION
This is useful where you have a blended file containing functions and executed instructions.
If neither -Names nor -Regex are provided then all functions in the file are returned.
Returned objects can be piped directly into Invoke-Expression which will place them into the current scope.
Returns an array of objects with the following
- .ToString()
- .Name
- .Parameters
- .Body
- .Extent
- .IsFilter
- .IsWorkFlow
- .Parent
.PARAMETER -File
String; Mandatory
Path of file to parse
.PARAMETER -Names
Array of Strings; Optional
If provided then function objects of these names will be returned
The name must exactly match the provided value
Case Insensitive.
.PARAMETER -Regex
Regular Expression; Optional
If provided then function objects with names that match will be returned
Case Insensitive
.EXAMPLE
Get all the functions names included in the file
Get-Function -name TestA | select name
.EXAMPLE
Import a function into the current scope
Get-Function -name TestA | Invoke-Expression
#>
param (
[Parameter(Mandatory = $true)]
[alias("Path", "FilePath")]
$File
,
[alias("Name", "FunctionNames", "Functions")]
$Names
,
[alias("NameRegex")]
$Regex
) # end param
# get the script and parse it
$Script = Get-Command $File
$AllFunctions = $Script.ScriptBlock.AST.FindAll({$args[0] -is [Management.Automation.Language.FunctionDefinitionAst]}, $false)
# return all requested functions
$AllFunctions | Where-Object {
( $Names -icontains $_.Name ) `
-or ( $Regex -imatch $_.Name ) `
-or (-not($Names) -and -not($Regex))
} # end where-object
} # end function Get-Function
这是 PowerShell 帮助文件中显示的内置功能。
类似的问题以前已经被问过了。因此,这可能是一个重复的问题:
答案是使用 PSDrive 功能。
# To get a list of available functions
Get-ChildItem function:\
# To remove a powershell function
# removes `someFunction`
Remove-Item function:\someFunction
或者
Function Get-MyCommands {
Get-Content -Path $profile | Select-String -Pattern "^function.+" | ForEach-Object {
[Regex]::Matches($_, "^function ([a-z.-]+)","IgnoreCase").Groups[1].Value
} | Where-Object { $_ -ine "prompt" } | Sort-Object
}
或者这个
$currentFunctions = Get-ChildItem function:
# dot source your script to load it to the current runspace
. "C:\someScript.ps1"
$scriptFunctions = Get-ChildItem function: | Where-Object { $currentFunctions -notcontains $_ }
$scriptFunctions | ForEach-Object {
& $_.ScriptBlock
}
关于这个问题...
谢谢,这很接近我想要的,但它也显示了像A:,B:,Get-Verb,Clear-Host等函数。
这是预期的结果。如果您想以其他方式获得它,那么您必须编写代码。 要获取任何脚本中函数的名称,首先必须将其加载到内存中,然后可以点源定义并获取其内部信息。如果您只想要函数名称,您可以使用正则表达式来获取它们。
或者简单地说...
Function Show-ScriptFunctions
{
[cmdletbinding()]
[Alias('ssf')]
Param
(
[string]$FullPathToScriptFile
)
(Get-Content -Path $FullPathToScriptFile) |
Select-String -Pattern 'function'
}
ssf -FullPathToScriptFile 'D:\Scripts\Format-NumericRange.ps1'
# Results
<#
function Format-NumericRange
function Flush-NumberBuffer
#>
TAB
)并输入以下内容:get-content Function:
。 - not2qubit
cat ./things.ps1 | grep function
或者在 Windows 中使用cat ./things.ps1 | select-string function
这样的命令无法满足您的需求吗?其中,您的脚本名为things.ps1
。 - Adam