每个开发人员应该知道的PowerShell脚本

Ale*_*der 10 windows powershell scripting

Windows PowerShell现在已经很长时间了.与优质的旧windows外壳相比,它更强大.是否有任何脚本用于加速和简化您作为开发人员的日常工作?如果你能用PowerShell做魔术 - >请与我们分享!

更新 不是真正的脚本,但也非常有用的是PowerShell社区扩展.该软件包包含许多新的Cmdlet和PowerShell修改.

Jef*_*man 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
    }
}
Run Code Online (Sandbox Code Playgroud)

我分别对svns和svnl这些别名.我在这里谈谈其他几个人.


Ray*_*gus 3

我一直使用这个,因为 Windows 资源管理器搜索文件内容对我来说从来不起作用:

Get-ChildItem -Recurse -Filter *.extension |
    Select-String -List somestring |
    Format-Table filename,linenumber -AutoSize
Run Code Online (Sandbox Code Playgroud)

只需将“extension”替换为您感兴趣的文件类型的文件扩展名(或完全删除 -Filter 参数),并将“somestring”替换为您想要在文件中查找的文本。