Posts Tagged 'PowerShell'

Creating SVN patches and saving them as ZIP file

I use SVN as source control system for one of my projects. Sometimes I want to create patches in form of a zip file containing the changed/new files. Doing so manually is a pain and error prone. First you have to diff between your branches to find all change since your last release, export those files from SVN, then package them into a proper structured ZIP file.
So here is a PowerShell snippet doing exactly that automatically:

function new-patch
{
    param($from, $to)
    # get diff summary as XML
    [xml] $summary = (svn diff $to $from -x --ignore-eol-style --xml --summarize)
    # loop through all diff/paths/path nodes
    # each node represents a modified/new file
    foreach($item in $summary.diff.paths.path)
    {
        # the SVN url
        $url=$item."#text"
        # the relative filename
        $file=($item."#text".Substring($to.Length))
        # the parent directory
        $dir=($file | split-path -parent)
        # create parent directory if it doesn't exist already
        if((test-path $dir) -eq $false) { mkdir $dir -force}
        # export current files from the SVN repository
        svn export $url $file
    }
    # package the current dir (.) into patch.zip
    sevenzip.exe a patch.zip .
}

The function is then used like this:

PS> new-patch "https://actiongame.svn.sourceforge.net/svnroot/
actiongame/tags/v01_00_00/" "https://actiongame.svn.sourceforge.net/svnroot/actiongame/branches/v01_00_xx/"

I specified the old branch as first argument and the new branch as second argument. Now after processing I get all the changes as proper filetree inside a zip file. Ready to ship..

The whole process could be optimized quite a bit. Right now a new SVN request is sent for each single file, it could as well be batched into one request to improve the export performance.

PowerShell explorer extension

Using this extension, you can select “Command Prompt” when right-clicking a folder in the windows explorer. This will open up a PowerShell instance with the current directory set to the folder you clicked, this is quite useful to quickly jump from GUI to shell.

All you have to do is saving this script to myextension.reg and run it on the target machine.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Folder\shell\CommandPrompt]
@=”Command Prompt”

[HKEY_CLASSES_ROOT\Folder\shell\CommandPrompt\command]
@=”\”C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe\” -NoExit -Command Set-Location -LiteralPath ‘%L'”

Update:
Thx to Jon for providing a better run command.

Display string as hex values

This tiny one-liner displays a custom string as hex values, quite useful..

PS C:\> "HEXIFY ME".ToCharArray() | % { ("0x{0:x}" -f [int]$_) } 0x48 0x45 0x58 0x49 0x46 0x59 0x20 0x4d 0x45

Export installed Printers to HTML

I wrote another useful PowerShell one liner to collect data for a network documentation. It lists the installed printers on a computer and writes it as formatted table to an html file.

PS C:\> Get-WmiObject win32_printer -computer ADRIAN | ? { $_.type -ne 1 } | sort name | ConvertTo-Html -prop Name,DriverName,PortName,Location > c:\out.html

Now just copy&paste the result from the file into your network docs, time saved!

Processes on a remote machine

Get the currently running processes on a remote (ms) machine using WMI and PS:

PS C:\> Get-WmiObject -query "select * from win32_process" -computer ADRIAN | sort -property ProcessName | format-list ProcessName,Path


ProcessName : acrotray.exe
Path : C:\Programme\Adobe\Acrobat 8.0\Acrobat\Acrotray.exe

ProcessName : alg.exe
Path :
[..]

I didn’t think using WMI in PS would be that easy. I’m currently writing a network documentation for a customer and using WMI to collect data is quite useful..