I was writing a Powershell script today to automate deployment for application we’re currently working on. Part of the requirement was to get latest files from Visual Studio SourceSafe. VSS provides us only with COM wrappers; however I wanted to do that via .NET objects.
Luckily googleing the problem has produced some valid resources. Check out VSS Code Samples. From there, download OLE Sample Code Written in Visual Basic .NET and C#. This exe contains a .NET solution with 2 projects: .NET VSS Library written in C# and a VB.NET explorer application that is basically a VSS Explorer that uses the Library under the hood. The problem is this has been written for version 6 of VSS and I had version 8 (the 2005 release).
Due to this, the explorer application couldn’t compile, since it relied on some ActiveX that simply wasn’t there anymore. Luckily, the underlying VSS Library still worked. Below is a sample code that I used to do my small task with the VSS Library. The code gets all items (recursively) from the given path.
clIVSSLibrary lib = new clIVSSLibrary();
string errorOccured = null;
errorOccured = lib.OpenDB("Martin.Waligora", "somepassword", "\\192.168.1.75\vss_application");
errorOccured = lib.SetCurrentProject("project");
errorOccured = lib.SetWorkingFolder(@"$/project/DistributionImage", @"C:\VSSTest");
VSSFlags flag = new VSSFlags();
long flags = flag.FlagGetLocalCopyYes() + flag.FlagRecursiveYes()
+ flag.FlagReplaceLocalReplace() + flag.FlagRights_All();
string getPath = null;
errorOccured = lib.GetItem(@"$/project/DistributionImage", ref getPath, (int)flags);
lib.CloseDB();
After writing the code I’ve added it to my Powershell script. Here’s it:
$VssDllPath = 'C:\IVSS_Sample\IVSSDLL\bin\Debug\IVSSLibrary.dll';
"Loading the .NET VSS DLL $VssDllPath"
$ass = [reflection.assembly]::LoadFrom($VssDllPath);
"Creating object of type IVSSFunctionLibrary.clIVSSLibrary"
$VssLib = New-Object IVSSFunctionLibrary.clIVSSLibrary;
#We create a function to help us with calls to the vss library
function ErrorOccured
{
if($args[0].Length -gt 1)
"Error has occured: $args[0]"
}
#We insantiated the object, now let's get all data to specified directory
ErrorOccured $VssLib.OpenDB("Martin.Waligora", "fakepass", "\\192.168.1.75\vss_app");
"Starting to copy all the files"
ErrorOccured $VssLib.SetCurrentProject("project");
ErrorOccured $VssLib.SetWorkingFolder("$/project/DistributionImage", $remotePath);
#$flags = 67117199;
$getPath = "";
ErrorOccured $VssLib.GetItem("$/project/DistributionImage", [ref] $getPath, 67117199);
ErrorOccured $VssLib.CloseDB();
"Success, finished copying files to $remotePath";
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.