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

标签 windows powershell scripting

<分区>

Windows PowerShell 已经发布很长时间了。与良好的旧 Windows 外壳相比,它更强大。 作为开发人员,您是否使用任何脚本来加速和简化您的日常工作?如果您可以使用 PowerShell 施展魔法 -> 请与我们分享!

更新 不是真正的脚本,但也很有用PowerShell Community Extensions .该软件包包含许多新的 Cmdlet 和 PowerShell 修改。

最佳答案

我将一堆脚本放在一起,以便在命令行中使用 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 .

关于windows - 每个开发人员都应该知道的 PowerShell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1109766/

相关文章:

windows - psql shell 使用代码页 850,windows 使用 1252。如何解决更改控制台代码页?

C++ Windows 凭据提供程序进度屏幕

powershell - TFS vNext build agent安装

Ruby:从 bash 脚本运行脚本?

eclipse - 在 Eclipse 中调试已编译的 Groovy 脚本

python - Windows Python 版本和 VC++ Redistributable 版本

c++ - 在 Windows/C++ 中强制显示分辨率

PowerShell:如何取消注册孤立的 WMI 事件

PowerShell 任务不会使用 VSTS 任务组变量,但会使用 - 为什么?

ASP.NET : What does the '#' starting a <% %> mean?