欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-25 10:03:10
要快速获取你ps脚本库中所有文件的函数名,你可以这样做: 复制代码 代码如下: filter find-function {    $path...

要快速获取你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及以后