Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell scripts every developer should know [closed]

Windows PowerShell is out a quite long time now. In comparison to the the good old windows shell it's much more powerful. Are there any scripts you use to speed up and simplify your every day work as an developer? If you can do magic with PowerShell -> please share it with us!

Update Not really a script, but also very useful are PowerShell Community Extensions. The package contains a lot of new Cmdlets and PowerShell modifications.

like image 982
Alexander Avatar asked Jul 10 '09 14:07

Alexander


People also ask

Do hackers use PowerShell?

Most of the malware is using PowerShell in different phases of malware attacks.

Is PowerShell useful for developers?

PowerShell is an open-source, command-line interface (CLI) based tool that allows developers, IT admins, and DevOps professionals to automate tasks and configurations using code. PowerShell is a bifunctional attribute built on Microsoft . NET technology.


1 Answers

I put together a bunch of scripts to work with Subversion at the command line. Most of them just use the --xml option to put various information in object form. Here are a couple of examples:

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

I have these aliased to svns and svnl, respectively. I talk about a few others here.

like image 86
Jeff Hillman Avatar answered Oct 21 '22 10:10

Jeff Hillman