Checking Out Files From TFS 2013 Using PowerShell

Hi there,

I’ve been trying for days to get a PowerShell script together that will CheckOut AssemblyInfo files recursively then modify the File and Version numbers. This would be part of a prebuild script in a TFS (2013) Build Definition. The version information is set based on the Build Number Format attribute or setting in the Definition.

I found this script that works to version the output files as desired…

Although this gets me part of the way to where I want to be, it does not retain the information in the AssemblyInfo files as they are not checked out. If I got that working, I was simply planning on checking the files back in via a post-build script.

I’m not using the Power Tools as I was just going to call tf.exe directly since I don’t have a lot of heavy lifting to do via script in TFS.

I’ve tried…

`&“C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Ide\TF.exe” checkout $/CITest/AssemblyInfo.* /recursive | Out-Null`

That would work from the PowerShell command line, but not from the script. So, I changed the TFS path to the local path and it appeared that the file would be checked out…

`&“C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Ide\TF.exe” checkout C:\src\CITest\AssemblyInfo.cs /recursive | Out-Null`

When I try to jam this somewhere in the above linked script, I get errors. If anyone can guide me to a script that will checkout the AssemblyInfo files recursively and modify them with version, that would be AWESOME!! If I can get that far, all I will need is a post build script to check them in.

I’m not even sure if this is doable in TFS as I’m not sure what happens to checked out files once the actual TFS build completes (before the post build script is run). Maybe I have to do all the work in the pre-build script.

Anyway, ANY help appreciated!

Thanks in advance

Your command line looks a bit weird to me. Can you try it like this:

$exe = 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Ide\TF.exe'
&$exe --% checkout $/CITest/AssemblyInfo.* /recursive

OK, I tried the command line suggestions and I am able to get them to execute properly from the command line and from a script containing only those lines. When I try to combine it with my overall versioning script I get the following error…

Exception Message TF270015: UpdateVersions.ps1 returned an unexpected exit code. Expected 0 actual 1. See the build logs for more details. (type UnexpectedExitCodeException)

Here is the script I tried at this point. Its a hack, I know, but I just wanted to see the files get checked out, which never occurs…

[string]$pathToSearch = $env:TF_BUILD_SOURCESDIRECTORY,
[string]$buildNumber = $env:TF_BUILD_BUILDNUMBER,
[string]$searchFilter = “AssemblyInfo.*”,
[regex]$pattern = “\d+.\d+.\d+.\d+”

if ($buildNumber -match $pattern -ne $true) {
Write-Host “Could not extract a version from [$buildNumber] using pattern [$pattern]”
exit 1
} else {
$extractedBuildNumber = $Matches[0]
Write-Host “Using version $extractedBuildNumber”

    gci -Path $pathToSearch -Filter $searchFilter -Recurse | %{
        Write-Host "  -> Changing $($_.FullName)"
        # remove the read-only bit on the file
        #sp $_.FullName IsReadOnly $false

        $exe = &"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\TF.exe"
        &$exe --% checkout c:\src\citest\assemblyinfo.* /recursive

        # run the regex replace
        (gc $_.FullName) | % { $_ -replace $pattern, $extractedBuildNumber } | sc $_.FullName

    Write-Host "Done!"

catch {
Write-Host $_
exit 1

You might also notice that I had to change the TFS path to files to the local path to the tiles. I’m not sure why that is the case either.

Any further help would be appreciated!! I was hoping this would be fairly easy, but I’m starting to wonder if this should be something handled by altering the TFS build template instead. I was pumped about using PowerShell though as it seemed like a lighter solution.

Thanks for the suggestion so far.

Superfreak3, have you been able to solve this issue?
I’m trying exactly the same, but tf checkout writes to stderr, which TFS interprets as errors.