2009-07-10 82 views

回答

3

我用這一個所有的時間,因爲Windows資源管理器的搜索文件內容從來沒有爲我工作:

Get-ChildItem -Recurse -Filter *.extension | 
    Select-String -List somestring | 
    Format-Table filename,linenumber -AutoSize 

只需更換「擴展」與您感興趣的文件類型的文件擴展名(或完全刪除-Filter參數),並將「somestring」替換爲您要在文件中找到的文本。

+0

我怎麼得到它停止中止上的目錄我無法訪問? – Maslow 2011-08-28 04:06:08

2

任何時候當你看到正確大小寫的東西時,這表示我已經使用了TAB完成。你應該知道PS會爲你完成哪些事情 - 這在V2中是相當不錯的。

無論何時您看到小寫的別名,都是我從內存中鍵入的內容。你也應該記住它。

# grep example - find all using statements 
dir -r -fil *cs | ss using 
# advanced version 
dir -fil *cs -r | ss '^using[^\(]+' | gpv line | sort -unique 

# figure out how to query for drive free space (emphasis on "figure out" -- I can never remember things like this) 
gcm *drive* 
help Get-PSDrive -full 
Get-PSDrive | gm 
# now use it 
Get-PSDrive | ? { $_.free -gt 1gb } 

# pretend mscorlib.dll is an assembly you're developing and want to do some ad-hoc testing on 
$system = [system.reflection.assembly]::LoadFile("c:\blah\...\mscorlib.dll") 
$system | gm 
$types = $a.GetTypes()  
$types | gm 
$types | ? { $_.ispublic -and $_.basetype -eq [system.object] } | sort name 
$sbType = $types | ? { $_.name -eq "StringBuilder" } 
# now that we've loaded the assembly, we could have also done: 
# $sbType = [system.text.stringbuilder] 
# but we may not have known it was in the Text namespace 
$sb = new-object $sbType.FullName 
$sb | gm 
$sb.Append("asdf") 
$sb.Append("jkl;") 
$sb.ToString() 
6

我把一堆腳本放在一起在命令行使用Subversion。他們中的大多數只是使用--xml選項將各種信息放在對象形式中。這裏有幾個例子:

function Get-SvnStatus([string[]] $Path = ".", 
         [string] $Filter = "^(?!unversioned|normal|external)", 
         [switch] $NoFormat) 
{ 
    # powershell chokes on "wc-status" and doesn't like two definitions of "item" 
    [xml]$status = ((Invoke-Expression "svn status $($Path -join ',') --xml") -replace "wc-status", "svnstatus") ` 
     -replace "item=", "itemstatus=" 

    $statusObjects = $status.status.target | Foreach-Object { $_.entry } | Where-Object { 
     $_.svnstatus.itemstatus -match $Filter 
    } | Foreach-Object { 
     $_ | Select-Object @{ Name = "Status"; Expression = { $_.svnstatus.itemstatus } }, 
          @{ Name = "Path"; Expression = { Join-Path (Get-Location) $_.path } } 
    } | Sort-Object Status, Path 

    if ($NoFormat) 
    { 
     $statusObjects 
    } 
    else 
    { 
     $statusObjects | Format-Table -AutoSize 
    } 
} 

function Get-SvnLog([string] $Path = ".", 
        [int] $Revision, 
        [int] $Limit = -1, 
        [switch] $Verbose, 
        [switch] $NoFormat) 
{ 
    $revisionString = "" 
    $limitString = "" 
    $verboseString = "" 

    if ($Revision) 
    { 
     $revisionString = "--revision $Revision" 
    } 

    if ($Limit -ne -1) 
    { 
     $limitString = "--limit $Limit" 
    } 

    if ($Verbose) 
    { 
     $verboseString = "--verbose" 
    } 

    [xml]$log = Invoke-Expression "svn log $($path -join ',') --xml $revisionString $limitString $verboseString" 

    $logObjects = $log.log.logentry | Foreach-Object { 
     $logEntry = $_ 

     $logEntry | Select-Object ` 
      @{ Name = "Revision"; Expression = { [int]$logEntry.revision } }, 
      @{ Name = "Author"; Expression = { $logEntry.author } }, 
      @{ Name = "Date"; 
       Expression = { 
        if ($NoFormat) 
        { 
         [datetime]$logEntry.date 
        } 
        else 
        { 
         "{0:dd/MM/yyyy hh:mm:ss}" -f [datetime]$logEntry.date 
        } 
       } }, 
      @{ Name = "Message"; Expression = { $logEntry.msg } } | 
     Foreach-Object { 
      # add the changed path information if the $Verbose parameter has been specified 
      if ($Verbose) 
      { 
       $_ | Select-Object Revision, Author, Date, Message, 
        @{ Name = "ChangedPaths"; 
         Expression = { 
          $paths = $logEntry.paths.path | Foreach-Object { 
           $_ | Select-Object ` 
            @{ Name = "Change"; 
             Expression = { 
              switch ($_.action) 
              { 
               "A" { "added" } 
               "D" { "deleted" } 
               "M" { "modified" } 
               "R" { "replaced" } 
               default { $_.action } 
              } 
             } }, 
            @{ Name = "Path"; Expression = { $_."#text" } } 
          } 

          if ($NoFormat) 
          { 
           $paths 
          } 
          else 
          { 
           ($paths | Sort-Object Change | Format-Table -AutoSize | Out-String).Trim() 
          } 
         } 
        } 
      } 
      else 
      { 
       $_ 
      } 
     } 
    } 

    if ($NoFormat) 
    { 
     $logObjects 
    } 
    else 
    { 
     $logObjects | Format-List 
    } 
} 

我有這些別名分別爲svns和svnl,分別。我談了幾個其他人here

+0

好的電話。我使用TFS Power Tool cmdlet的次數與上面輸入的次數一樣多,但並非每個人都擁有TFS。如果你的源代碼管理系統確實有某種對象模型,那麼將它與Powershell配合是非常好的學習。 – 2009-07-10 17:09:14

3

這不是一個腳本,但總的來說,瞭解什麼時候可以通過名稱和位置縮短參數是有幫助的。

按名字,PowerShell只需要足夠的縮小到一個。例如,gci -r有效,但gci -f可能是-filter-force

指定的沒有參數標籤的值在位置上應用。所以,如果你想指定-filter,你可以做這一點:

gci -r -fil *.cs 

或提供.位置上爲-path所以你也可以指定-filter位置上:

gci -r . *.cs 
相關問題