Subversion In Powershell

Subversion in Powershell0

I recently had a need to develop some Powershell scripts to use Subversion in Powershell. I needed to automate checking out content from Subversion to a server. I decided to use SharpSVN, which is a great .NET client library for Subversion. So it also works well in Powershell. Because I had several places I wanted to hook into SVN, I created a library script that I could reuse in my other scripts.

Library Script

The library script maintains the SVN credentials, the SharpSVN DLL location, and the SVN server URL. The Checkout function accepts the SVN URL and the path to Checkout to.

### Variables ###
$svnUser = "svnuser"
$svnPwd = "svnpassword"
$svnDllPath = "C:\Shared\SharpSvn.dll"
$svnServer = "https://sourcecode.svn.com"

### Load SharpSVN ###
if (!(Test-Path $svnDllPath)){
 Write-Error "Could not locate $svnDLLPath. Check if present and rerun"
 exit
}
$currentScriptDirectory = Get-Location
[System.IO.Directory]::SetCurrentDirectory($currentScriptDirectory)
$a = [Reflection.Assembly]::LoadFile($svnDllPath)

# Setup Variables
$mode = [System.IO.FileMode]::Create
$access = [System.IO.FileAccess]::Write
$sharing = [IO.FileShare]::Read

# Create SharpSVN Client Object
$svnClient = New-Object SharpSvn.SvnClient
$svnClient.Authentication.DefaultCredentials = New-Object System.Net.NetworkCredential($svnUser, $svnPwd)

function Download-File($svnPath, $localPath){
 # Build SVN URL
 [string]$svnUrl = $svnServer + $svnPath
 $repoUri = New-Object SharpSvn.SvnUriTarget($svnUrl)

 Write-Host "$repoUri..." -ForegroundColor Green -NoNewline

 # create the FileStream and StreamWriter objects
 $fs = New-Object IO.FileStream($localPath, $mode, $access, $sharing)

 $res = $svnClient.Write($repoUri, $fs)

 if ($res -eq "True"){
  Write-Host "Done" -ForegroundColor Green
 }else{
  Write-Warning "Failed"
 }
 $fs.Dispose()
}

function Checkout-Folder($svnDir, $localDir){
 # Build SVN URL
 [string]$svnUrl = $svnServer + $svnDir
 $repoUri = New-Object SharpSvn.SvnUriTarget($svnUrl)

 Write-Host "$repoUri..." -ForegroundColor Green -NoNewline

 $res = $svnClient.Checkout($repoUri, $localDir)

 if ($res -eq "True"){
  Write-Host "Done" -ForegroundColor Green
 }else{
  Write-Warning "Failed"
 }
}

Include-Subversion.ps1



Main Script

The Main Script loads Include-Subversion.ps1 as defined above. If the Include Script can’t be found it will exit with an error. Creates the checkout directory, and clears it if it already exists. Then calls the Checkout function from out Include script.


### Include Scripts ###
Write-Host "Loading Included Scripts"

if (!(Test-Path "C:\scripts\Include-Subversion.ps1")){Write-Error "Include Script Not Found: C:\scripts\Include-Subversion.ps1"; exit script}
. "C:\scripts\Include-Subversion.ps1"

Write-Host ""

$dllstage = "E:\TEMP\DLL-DEPLOY\"

# Get SVN Path from Config File
$SVNPath = "https://sourcecode.svn.com/svn/project/release"

# Download from SVN
if (Test-Path $dllstage){
 Remove-Item $dllstage -Force -recurse | Out-Null
}

New-Item $dllstage -ItemType Directory | Out-Null

Write-Host "Downloading From Subversion" -ForegroundColor Magenta
Checkout-Folder $SVNPath $dllstage
Write-Host ""


My New Stories

March 2016 Web Hosting Deals
Powershell AD Group Management
Troubleshooting 403