locked
TFS API PowerShell Script RRS feed

  • Question

  • Hi all,

    I have a requirement where I have to get information about all the Files and folder checked into TFS server. I searched for this and found that we can use VersionControlServer for this purpose and tried to achieve it. I want to get the following information.

    Parse through all version control files for a given TPC.
    Gather for latest version:
    Team Project
    Repository Type (optional, TFVC or Git)
    Filename
    FileType
    Size
    Full version control path for the file
    Update Date (last changeset)

    Please help me how can I get above information using TFS API & PowerShell Script.

    Thanks for help.



    Tuesday, December 15, 2015 10:54 AM

Answers

  • Hi Vishwajeet, 

    Thanks for your post. 

    You can use “Microsoft.TeamFoundation.VersionControl.Client” and “Microsoft.TeamFoundation.Client” API to get the files information on TFS server. For example, the following C# code can get all files names from a collection:

    TfsTeamProjectCollection server = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://servername:8080/tfs/CollectionURL/"));
    
                VersionControlServer version = server.GetService(typeof(VersionControlServer)) as VersionControlServer;
    
                ItemSet items = version.GetItems(@"$/", RecursionType.Full);
     
                foreach (Item item in items.Items)
                {
                    
                     System.Console.WriteLine(item.ServerItem);
                    System.Console.WriteLine(item);
                    System.Console.WriteLine("-----------------------");
                }

    The result is like below: 

    >>Repository Type(optional, TFVC or Git) 

    Since the team project has been created and set to TFVC, the files and folder which are under the team project will always use TFVC. 

    Reference: 

    Microsoft.TeamFoundation.VersionControl.Client

    Microsoft.TeamFoundation.Client

    Wednesday, December 16, 2015 8:20 AM
    Moderator
  • Shrek has given the C# code and its giving the expected output. Thanks again Shrek.

    I am sharing the PowerShell script to achieve the same. I have executed the PowerShell script given below and its working fine and the script is exporting all the Properties of the files to a CSV file.

     

    #Clear host
    clear-host

    # Referencing Assemblies in PowerShell Script
    $pathToAss2 = "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0"
    $pathToAss4 = "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v4.5"
    Add-Type -Path "$pathToAss2\Microsoft.TeamFoundation.Client.dll"
    Add-Type -Path "$pathToAss2\Microsoft.TeamFoundation.Common.dll"
    Add-Type -Path "$pathToAss2\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
    Add-Type -Path "$pathToAss2\Microsoft.TeamFoundation.VersionControl.Client.dll"
    Add-Type -Path "$pathToAss4\Microsoft.TeamFoundation.ProjectManagement.dll"

    # Register PowerShell commands

    #Add-PSSnapin Microsoft.TeamFoundation.PowerShell

    # Connect to TFS

    $tfsCollectionUrl = "https://vishwajeetTFS.nc.com/tfs/DefaultCollection"
    $teamProjectName = "TrainingTeamProject";
    $TFS = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($tfsCollectionUrl )
                  
    $TFS.EnsureAuthenticated()

    if (!$TFS.HasAuthenticated)
    {
      Write-Host "Failed to authenticate to TFS"
      exit
    }
    Write-Host "Connected to Team Foundation Server [" $tfsCollectionUrl "]"

    $server = new-object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(New-Object Uri($tfsCollectionUrl))

    $versionControlServer = $server.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])

    $teamProjectsList = $versionControlServer.GetAllTeamProjects($true)

    foreach($projectName in $teamProjectsList)
    {
         if($projectName.Name -eq $teamProjectName)
         {
                 Write-host "Team Project Name : " $projectName.Name

                 #$tim = $versionControlServer.GetItems($projectName.ServerItem)

                 $teamProjectItemsObjects = $versionControlServer.GetItems($projectName.ServerItem, [ Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest,[Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full,[Microsoft.TeamFoundation.VersionControl.Client.DeletedState]::Any,[Microsoft.TeamFoundation.VersionControl.Client.ItemType]::File)
                
                 #$teamProjectItemsObjects.Items | Out-File -FilePath "C:\Scripts\TPC_VersionInfo.csv"
                
                 $totalFiles = $teamProjectItemsObjects.Items.Count

                 Write-host "The total number of files in the Team Project is : " $totalFiles
                
                foreach ($item in  $teamProjectItemsObjects.Items )
                {
                    #$item.ContentLength , $item.CheckinDate,$item.ItemType ,$item.ServerItem,$item.ChangesetId           

                    # Export the value of $item to CSV file

                    $item | Out-File -FilePath C:\Scripts\testdata.csv -Append
                }

         }

    }

    Above PowerShell Script is working fine for me. Let me know if it is also working for others.


    Wednesday, December 16, 2015 9:27 AM

All replies

  • Hi Vishwajeet, 

    Thanks for your post. 

    You can use “Microsoft.TeamFoundation.VersionControl.Client” and “Microsoft.TeamFoundation.Client” API to get the files information on TFS server. For example, the following C# code can get all files names from a collection:

    TfsTeamProjectCollection server = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://servername:8080/tfs/CollectionURL/"));
    
                VersionControlServer version = server.GetService(typeof(VersionControlServer)) as VersionControlServer;
    
                ItemSet items = version.GetItems(@"$/", RecursionType.Full);
     
                foreach (Item item in items.Items)
                {
                    
                     System.Console.WriteLine(item.ServerItem);
                    System.Console.WriteLine(item);
                    System.Console.WriteLine("-----------------------");
                }

    The result is like below: 

    >>Repository Type(optional, TFVC or Git) 

    Since the team project has been created and set to TFVC, the files and folder which are under the team project will always use TFVC. 

    Reference: 

    Microsoft.TeamFoundation.VersionControl.Client

    Microsoft.TeamFoundation.Client

    Wednesday, December 16, 2015 8:20 AM
    Moderator
  • Thanks a lot Shrek for your pointers. Your code snippet given above is working example of listing the properties of version controlled files (items in case of TFS) such as filenames, size of the file, lastcheckindate etc..

    Wednesday, December 16, 2015 9:12 AM
  • Shrek has given the C# code and its giving the expected output. Thanks again Shrek.

    I am sharing the PowerShell script to achieve the same. I have executed the PowerShell script given below and its working fine and the script is exporting all the Properties of the files to a CSV file.

     

    #Clear host
    clear-host

    # Referencing Assemblies in PowerShell Script
    $pathToAss2 = "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0"
    $pathToAss4 = "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v4.5"
    Add-Type -Path "$pathToAss2\Microsoft.TeamFoundation.Client.dll"
    Add-Type -Path "$pathToAss2\Microsoft.TeamFoundation.Common.dll"
    Add-Type -Path "$pathToAss2\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
    Add-Type -Path "$pathToAss2\Microsoft.TeamFoundation.VersionControl.Client.dll"
    Add-Type -Path "$pathToAss4\Microsoft.TeamFoundation.ProjectManagement.dll"

    # Register PowerShell commands

    #Add-PSSnapin Microsoft.TeamFoundation.PowerShell

    # Connect to TFS

    $tfsCollectionUrl = "https://vishwajeetTFS.nc.com/tfs/DefaultCollection"
    $teamProjectName = "TrainingTeamProject";
    $TFS = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($tfsCollectionUrl )
                  
    $TFS.EnsureAuthenticated()

    if (!$TFS.HasAuthenticated)
    {
      Write-Host "Failed to authenticate to TFS"
      exit
    }
    Write-Host "Connected to Team Foundation Server [" $tfsCollectionUrl "]"

    $server = new-object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(New-Object Uri($tfsCollectionUrl))

    $versionControlServer = $server.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])

    $teamProjectsList = $versionControlServer.GetAllTeamProjects($true)

    foreach($projectName in $teamProjectsList)
    {
         if($projectName.Name -eq $teamProjectName)
         {
                 Write-host "Team Project Name : " $projectName.Name

                 #$tim = $versionControlServer.GetItems($projectName.ServerItem)

                 $teamProjectItemsObjects = $versionControlServer.GetItems($projectName.ServerItem, [ Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest,[Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full,[Microsoft.TeamFoundation.VersionControl.Client.DeletedState]::Any,[Microsoft.TeamFoundation.VersionControl.Client.ItemType]::File)
                
                 #$teamProjectItemsObjects.Items | Out-File -FilePath "C:\Scripts\TPC_VersionInfo.csv"
                
                 $totalFiles = $teamProjectItemsObjects.Items.Count

                 Write-host "The total number of files in the Team Project is : " $totalFiles
                
                foreach ($item in  $teamProjectItemsObjects.Items )
                {
                    #$item.ContentLength , $item.CheckinDate,$item.ItemType ,$item.ServerItem,$item.ChangesetId           

                    # Export the value of $item to CSV file

                    $item | Out-File -FilePath C:\Scripts\testdata.csv -Append
                }

         }

    }

    Above PowerShell Script is working fine for me. Let me know if it is also working for others.


    Wednesday, December 16, 2015 9:27 AM
  • Hi Vishwajeet, 

    Thanks for sharing your solution. 

    Thursday, December 17, 2015 1:51 AM
    Moderator