Powershell: how to get functions defined in a script?
来源:互联网 发布:约瑟夫环算法 c 编辑:程序博客网 时间:2024/05/16 18:21
It is actually part of ScriptCop http://scriptcop.start-automating.com/, very useful.
function Get-FunctionFromScript { <# .Synopsis Gets the functions declared within a script block or a file .Description Gets the functions exactly as they are written within a script or file .Example Get-FunctionFromScript { function foo() { "foo" } function bar() { "bar" } } #> [CmdletBinding(DefaultParameterSetName='File')] param( # The script block containing functions [Parameter(Mandatory=$true, ParameterSetName="ScriptBlock", ValueFromPipelineByPropertyName=$true)] [ScriptBlock] $ScriptBlock, # A file containing functions [Parameter(Mandatory=$true, ParameterSetName="File", ValueFromPipelineByPropertyName=$true)] [Alias('FullName')] [String] $File, # If set, outputs the command metadatas [switch] $OutputMetaData ) process { if ($psCmdlet.ParameterSetName -eq "File") { $realFile = Get-Item $File if (-not $realFile) { $realFile = Get-Item -LiteralPath $File -ErrorAction SilentlyContinue if (-not $realFile) { return } } $text = [IO.File]::ReadAllText($realFile.Fullname) $scriptBlock = [ScriptBlock]::Create($text) if ($scriptBlock) { $functionsInScript = Get-FunctionFromScript -ScriptBlock $scriptBlock -OutputMetaData:$OutputMetaData if ($OutputMetaData) { $functionsInScript | Add-Member NoteProperty File $realFile.FullName -PassThru } } } elseif ($psCmdlet.ParameterSetName -eq "ScriptBlock") { $text = $scriptBlock.ToString() $tokens = [Management.Automation.PSParser]::Tokenize($scriptBlock, [ref]$null) for ($i = 0; $i -lt $tokens.Count; $i++) { if ($tokens[$i].Content -eq "function" -and $tokens[$i].Type -eq "Keyword") { $groupDepth = 0 $functionName = $tokens[$i + 1].Content $startI = $i -1 while ($startI -ge 0 -and ('Newline', 'Comment' -contains $tokens[$startI].Type)) { $startI-- } $startI++ $ii = $i $done = $false while (-not $done) { while ($tokens[$ii] -and $tokens[$ii].Type -ne 'GroupStart') { $ii++ } $groupDepth++ while ($groupDepth -and $tokens[$ii]) { $ii++ if ($tokens[$ii].Type -eq 'GroupStart') { $groupDepth++ } if ($tokens[$ii].Type -eq 'GroupEnd') { $groupDepth-- } } if (-not $tokens[$ii]) { break } if ($tokens[$ii].Content -eq "}") { $done = $true } } if (-not $tokens[$ii] -or ($tokens[$ii].Start + $tokens[$ii].Length) -ge $Text.Length) { $chunk = $text.Substring($tokens[$startI].Start) } else { $chunk = $text.Substring($tokens[$startI].Start, $tokens[$ii].Start + $tokens[$ii].Length - $tokens[$startI].Start) } if ($OutputMetaData) { New-Object PSObject -Property @{ Name = $functionName Definition = [ScriptBlock]::Create($chunk) } } else { [ScriptBlock]::Create($chunk) } } } } }}
0 0
- Powershell: how to get functions defined in a script?
- How to write comment in PowerShell Script
- How to launche powershell script in C#
- How to pass an argument to a PowerShell script?
- How to write a script in Linux
- How to run a script in ubuntu?
- How To Load .NET Assemblies In A PowerShell Session
- How to use ActiveRecord in a ruby script outside Rails?
- how to write a configure script in linux
- how to get remote fqdn by powershell?
- How to get runtime LOV query script
- How To Get a Jump Start In Your Programming Career
- How to Get a List of Class Attributes in Python
- How to format DateTime in PowerShell window
- How to add Snap-in (扩展Powershell)
- How to access a value defined in the application.properties file in Spring Boot ( Externalized Conf)
- How to use an ActiveX script task to create a loop in DTS
- How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings
- Codeforces 474B Worms 二分(水
- Java输入输出(3)--OutputStream和Writer
- 详解协方差与协方差矩阵
- CentOS下安装JDK7
- Android UI开发详解之Fragment
- Powershell: how to get functions defined in a script?
- Codeforces 474C Captain Marmot 给定4个点和各自旋转中心 问旋转成正方形的次数
- 基于MFC的一个简单计算器
- 在Vultr的VPS上安装支持ZFS的FreeBSD及SMTP问题的解决
- 轉載:更有效率的使用Visual Studio
- 如何选购汽车贴膜
- 大学,不是学习的终点,而是起点。
- 举例说明使用JMeter做压力测试
- 九种排序算法总结