Powershell小技巧之查找脚本中的函数

所属分类: 脚本专栏 / PowerShell 阅读数: 1722
收藏 0 赞 0 分享

要快速获取你PS脚本库中所有文件的函数名,你可以这样做:

复制代码 代码如下:

filter Find-Function
{
   $path = $_.FullName
   $lastwrite = $_.LastWriteTime
   $text = Get-Content -Path $path
   
   if ($text.Length -gt 0)
   {
      
      $token = $null
      $errors = $null
      $ast = [System.Management.Automation.Language.Parser]::ParseInput($text, [ref] $token, [ref] $errors)
      $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true) |
      Select-Object -Property Name, Path, LastWriteTime |
      ForEach-Object {
         $_.Path = $path
         $_.LastWriteTime = $lastwrite
         $_
      }
   }
}

这将扫描出你用户配置文件夹下的所有PS脚本中的函数:

复制代码 代码如下:

PS> dir $home -Filter *.ps1 -Recurse -Exclude *.ps1xml | Find-Function
Name                       Path                       LastWriteTime          
----                       ----                       -------------          
Inject-LogonCredentials    C:\Users\Tobias\Desktop... 06.01.2014 02:43:00    
Test-Command               C:\Users\Tobias\Desktop... 06.03.2014 10:17:02    
Test                       C:\Users\Tobias\Desktop... 30.01.2014 09:32:20    
Get-WebPictureOriginal     C:\Users\Tobias\Desktop... 11.12.2013 11:37:53    
Get-ConnectionString       C:\Users\Tobias\Documen... 23.05.2014 10:49:09    
Convert-SID2User           C:\Users\Tobias\Documen... 23.05.2014 15:33:06    
Lock-Screen                C:\Users\Tobias\Documen... 19.03.2014 12:51:54    
Show-OpenFileDialog        C:\Users\Tobias\Documen... 16.05.2014 13:42:16    
Show-UniversalData         C:\Users\Tobias\Documen... 16.05.2014 13:23:20    
Start-TimebombMemory       C:\Users\Tobias\Documen... 23.05.2014 09:12:28    
Stop-TimebombMemory        C:\Users\Tobias\Documen... 23.05.2014 09:12:28    
(...)

将结果用管道传给Out-GridView 将能得到更完美的信息。

支持PS3.0及以后

更多精彩内容其他人还在看

Windows Powershell排序和分组管道结果

本文通过实例,详细介绍了Powershell如何对管道结果进行排序和分组,非常的实用,有需要的朋友可以参考下
收藏 0 赞 0 分享

Windows Powershell过滤管道结果

通过管道可以过滤某些对象和对象的属性,这个功能很实用,因为很多时候我们并不是对所有的结果感兴趣,可能只会对某些结果感兴趣。
收藏 0 赞 0 分享

Windows Powershell分析和比较管道结果

这篇文章主要介绍了Windows Powershell分析和比较管道结果,需要的朋友可以参考下
收藏 0 赞 0 分享

Powershell小技巧--将文件夹中的大文件分成若干份

这篇文章主要介绍了使用Powershell将文件夹中的大文件分成若干份的一段代码分享,非常实用,大家也可以根据需求自己来稍微调整下
收藏 0 赞 0 分享

Powershell小技巧--远程对比服务配置

这篇文章主要介绍了使用Powershell远程对比服务配置的方法,大家可以推广下获取服务器其他参数进行对比,希望对大家能有所帮助
收藏 0 赞 0 分享

Windows Powershell导出管道结果

本文主要讲诉了PowerShell的输出命令详细解释,以及导出管道结果的部分示例,非常有用,有需要的朋友可以参考下
收藏 0 赞 0 分享

Windows Powershell扩展类型系统

本文主要详细介绍了集中将对象转换成文本的方法,并附上示例说明,非常的实用,有需要的朋友可以参考下
收藏 0 赞 0 分享

Windows Powershell对象=属性+方法

从今天开始,我们这个系列的教程进入到讲诉使用对象的阶段,那么本阶段的第一篇还是先来熟悉下概念,简单的说对象=属性+方法
收藏 0 赞 0 分享

Windows Powershell属性:描述对象是什么

既然上文说明了对象=属性+方法,那么本文我们就先来探讨下属性。先从属性的概念入手,接着通过属性中包含对象、只读属性和读写属性、属性的类型、查看所有属性这4个方面详细向我们展示了对象。有需要的朋友展示下
收藏 0 赞 0 分享

Windows Powershell方法(对象能做什么)

方法定义了一个对象可以做什么事情。当你把一个对象输出在控制台时,它的属性可能会被转换成可视的文本。但是它的方法却不可见。
收藏 0 赞 0 分享
查看更多