I have written three functions so far. They cover the main tasks I perform when working with my Vault for my Powershell scripts.
They are:
- In-VaultFile
- Out-VaultFile
- Get-VaultCheckoutList
A couple of items to note:
- Getting the command-line to work was a bit tricky
- The ‘vault.exe’ app outputs XML, which made generating feedback really nice. Of course, I didn’t know about [xml]$var at first. But, once I read about that, things moved along nicely.
- I don’t like that I have to store my username and password in the cleartext in my script file. Not sure what options I have on that one. I certainly don’t want to have to type it out every time!
- The Vault Command Line Client has a lot of other functions available. I will probably be adding wrappers for some others in the near future… Like creating new folders, viewing history, etc.
I love that we can expose only certain functions from within a module. That is cool!
Lastly, I just want to say THANK YOU to Mr. Snover and his team! I haven’t had this much fun working on computers in quite a while! Sys Admin is FUN AGAIN!
Anyway, here’s the code…
################################################################# # FUNCTION: In-VaultFile # # WRITTEN BY: Derek Mangrum # # 2009-01-08 : Initial Version ################################################################# function In-VaultFile { <# .SYNOPSIS Checks a file in to SourceGear Vault .DESCRIPTION Checks a file in to SourceGear Vault .NOTES Author : Derek Mangrum derek.mangrum@gmail.com Requires : PowerShell V2 CTP3 or later .LINK http://grinding-it-out.blogspot.com/ .EXAMPLE In-VaultFile c:\scripts\file01.ps1 "This is my comment" .EXAMPLE dir c:\scripts\Modules -Recurse -Filter *.psm1 | In-VaultFile -Comment "My comments" .PARAMETER File The file that you want to check in. .PARAMETER Comment Comments for the check in operation. Required. #> param ( [Parameter(Position=0, Mandatory=$true, ValueFromPipeLine=$true)] [string]$File, [Parameter(Position=1, Mandatory=$true, ValueFromPipeLine=$false)] [string]$Comment ) BEGIN { } #END BEGIN PROCESS { checkIn-File $File $Comment } #END PROCESS END { } #END END } function checkIn-File { param ( [string]$File, [string]$Comment ) if (Test-Path $File) { $item = (Resolve-Path $File) -replace 'C:', '$' $item = $item -replace '\\', '/' $user = userInfo $Comment = $Comment -replace " ", "_" $command = "cmd.exe /C `"C:\Program Files\SourceGear\Vault Client\vault.exe`" CHECKIN -host $($user.Host) -user $($user.name) -password $($user.pass) -ssl -repository $($user.Repository) -comment $Comment $item" [xml]$result = Invoke-Expression $command if ($result.vault.result.success -eq "yes") { Write-Host "SUCCESS: " -ForegroundColor Green $result.vault.'#comment' } else { Write-Host "FAIL: " -ForegroundColor Red $result.vault.'#comment' } } else { Write-Host "No such file: " -ForegroundColor Red -NoNewline $File } } ################################################################# # FUNCTION: Out-VaultFile # # WRITTEN BY: Derek Mangrum # # 2009-01-08 : Initial Version ################################################################# function Out-VaultFile { <# .SYNOPSIS Checks a file out from SourceGear Vault .DESCRIPTION Checks a file out from SourceGear Vault .NOTES Author : Derek Mangrum derek.mangrum@gmail.com Requires : PowerShell V2 CTP3 or later .LINK http://grinding-it-out.blogspot.com/ .EXAMPLE Out-VaultFile c:\scripts\file01.ps1 .EXAMPLE dir c:\scripts\Modules -Recurse -Filter *.psm1 | Out-VaultFile .PARAMETER File The file that you want to check in. #> param ( [Parameter(Position=0, Mandatory=$true, ValueFromPipeLine=$true)] [string]$File ) BEGIN { } #END BEGIN PROCESS { checkOut-File $File } #END PROCESS END { } #END END } function checkOut-File { param ( [string]$File ) if (Test-Path $File) { $item = (Resolve-Path $File) -replace 'C:', '$' $item = $item -replace '\\', '/' $user = userInfo $command = "cmd.exe /C `"C:\Program Files\SourceGear\Vault Client\vault.exe`" CHECKOUT -host $($user.Host) -user $($user.name) -password $($user.pass) -ssl -repository $($user.Repository) $item" [xml]$result = Invoke-Expression $command if ($result.vault.result.success -eq "yes") { Write-Host "SUCCESS: " -ForegroundColor Green $result.vault.'#comment' } else { Write-Host "FAIL: " -ForegroundColor Red $result.vault.'#comment' } } else { Write-Host "No such file: " -ForegroundColor Red -NoNewline $File } } ################################################################# # FUNCTION: Get-VaultCheckoutList # # WRITTEN BY: Derek Mangrum # # 2009-01-08 : Initial Version ################################################################# function Get-VaultCheckoutList { <# .SYNOPSIS Lists all items currently checked out. .DESCRIPTION Lists all items currently checked out. .NOTES Author : Derek Mangrum derek.mangrum@gmail.com Requires : PowerShell V2 CTP3 or later .LINK http://grinding-it-out.blogspot.com/ .EXAMPLE Get-VaultCheckoutList #> BEGIN { } #END BEGIN PROCESS { getList } #END PROCESS END { } #END END } function getList { $user = userInfo $command = "cmd.exe /C `"C:\Program Files\SourceGear\Vault Client\vault.exe`" LISTCHECKOUTS -host $($user.Host) -user $($user.Name) -password $($user.pass) -ssl -repository $($user.Repository)" [xml]$result = Invoke-Expression $command Write-Host "The following items are currently checked out" Write-Host "---------------------------------------------" if ($result.vault.result.success -eq 'yes') { foreach ($item in $result.vault.checkoutlist.checkoutitem) { $item.checkoutuser.localpath } } else { Write-Host "ERROR" -ForegroundColor Red } } function userInfo { $user = @{} $user.Name = 'MyName' $user.Pass = 'MyPassword' $user.Host = 'MyHost' $user.Repository = 'MyRepository' return $user } Export-ModuleMember In-VaultFile Export-ModuleMember Out-VaultFile Export-ModuleMember Get-VaultCheckoutList

2 comments:
re: the password issue -- look into vault's -rememberlogin command; you'll at least only have to enter the user info when changing repositories.
Also, you might take a look here and here where I call the Vault and Fortress APIs directly from PowerShell, bypassing the client.
Thank you for the comments. I will be sure to read your posts as well. Always looking to learn and find better ways of doing things.
Post a Comment