Check-Out files from TFS 2010 with powershell

2019-04-26 15:19发布

问题:

I only want to check out a path with powershell and also check in this path one minute later at Team Foundation Server.

How can i do this?

I have installed the tfs power tools at my server.

回答1:

You don't need the power tools. Just use the tf.exe command line util that comes with Visual Studio with TFS Team Explorer. tf edit <file> /noprompt to check out and tf checkin <file> /comment:$comment /noprompt to check in. Look at the command line usage on tf.exe for more info tf /? and tf checkin /?. You will need to configure your PowerShell session with the Path to tf.exe. This is usually done by the VS vars batch files. But you should be able to simply add to the path like so: $PATH += "${VS110COMNTOOLS}..\Ide".



回答2:

Here is a function which will check to see if the snapin is installed. If you installed the powertools, it uses that, otherwise, it uses the command line tool tf.exe

function checkout-file([string] $file)
{
    "Checking out $file"
    if ((Get-PSSnapin -Name  Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null )
    {
        Add-PsSnapin  Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue

        if ((Get-PSSnapin -Name  Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null )
        {
            #try to check out the code using command line tf.exe
            &"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\TF.exe" checkout $file | Out-Null
        }
        else{
            #checkout the file using snapin
            Add-TfsPendingChange -Edit $file | Out-Null
        }
    }else{
        #checkout the file using snapin
        Add-TfsPendingChange -Edit $file | Out-Null
    }
}


回答3:

If you have the power tool commandlets installed for powershell. You don't need the Path like Kieth mentions, part of the commandlets aliases tf for the full tf.exe path. So, simply use the tf.exe command line reference here and all of these should work if you have the powershell commandlets installed correctly.

You should make sure your powershell has them installed by using this command though

 Add-PSSnapin Microsoft.TeamFoundation.PowerShell


回答4:

This is my solution:

$tf = &"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe"     checkout C:\setup\folder /recursive
$tf | Out-null


回答5:

Another option is to use the wild-card if you're trying to handle multiple files in the same command without picking up other files or looping over them.

   tf.exe undo AssemblyInfo* /recursive
   tf.exe checkout AssemblyInfo* /recursive
   #changes files
   tf.exe checkin AssemblyInfo* /recursive