2009-07-10 11 views
10

Windows PowerShell è fuori molto tempo ormai. In confronto alla buona vecchia shell di Windows è molto più potente. Ci sono degli script che usi per velocizzare e semplificare il tuo lavoro quotidiano come sviluppatore? Se puoi fare magie con PowerShell -> condividilo con noi!Script di PowerShell che ogni sviluppatore dovrebbe conoscere

Aggiornamento Non proprio uno script, ma anche molto utile sono PowerShell Community Extensions. Il pacchetto contiene molti nuovi cmdlet e modifiche di PowerShell.

risposta

3

Io uso questo tutto il tempo perché di ricerca di Esplora risorse di Windows per il contenuto dei file non funziona per me:

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

Basta sostituire "estensione" con l'estensione del file del tipo di file che ti interessa (o rimuovere completamente il parametro -Filter e sostituire "somestring" con il testo che si desidera trovare nel file.

+0

come ottengo che smettesse abortire sulle directory Non ho accesso a? – Maslow

2

Ogni volta che si vede qualcosa con le maiuscole corrette, è un'indicazione che ho usato il completamento del TAB. Dovresti sapere quali cose PS completerà per te - è abbastanza buono in V2.

Ogni volta che si vedono gli alias in minuscolo, è qualcosa che ho digitato dalla memoria. Dovresti anche memorizzarlo.

# 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

ho messo insieme un gruppo di script per lavorare con Subversion nella riga di comando. Molti di loro usano semplicemente l'opzione --xml per inserire varie informazioni in forma di oggetto. Qui ci sono un paio di esempi:

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 
    } 
} 

ho questi alias di svns e svnl, rispettivamente. Parlo di alcuni altri here.

+0

Buona chiamata. Uso i cmdlet di TFS Power Tool con la stessa frequenza con cui ho digitato sopra, ma non tutti hanno TFS. Se si dispone di una sorta di modello a oggetti per il proprio sistema di controllo sorgente preferito, accoppiarlo a PowerShell è estremamente buono da apprendere. –

3

Non è uno script, ma in generale è utile apprendere quando è possibile tagliare i parametri, sia per nome che per posizione.

Per nome, PowerShell ha solo bisogno di sufficiente per ridurlo a uno. Ad esempio, gci -r funziona ma gci -f potrebbe essere -filter o -force.

I valori specificati senza un'etichetta parametro sono applicati in posizione. Quindi, se si desidera specificare -filter si potrebbe o fare questo:

gci -r -fil *.cs 

O fornire . positionally come -path modo è anche possibile specificare -filter positionally:

gci -r . *.cs 
Problemi correlati