最经在研究PS访问TFS的问题,理出一点头绪来,先整理下:
环境:PS2.0+TFS2010
1,获取单个文件和获取整个文件下文件
# ------------------------------------------------------------------------------------- # Credit for this code goes to: # James Manning (http://blogs.msdn.com/jmanning/archive/2006/09/28/776141.aspx) function Get-TFS_X ( [string] $serverName = $(throw 'Get-TFS_X: serverName is required') ) { begin { # load the required dll [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client") $propertiesToAdd = ( ('VCS' , 'Microsoft.TeamFoundation.VersionControl.Client' , 'Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer'), ('WIT' , 'Microsoft.TeamFoundation.WorkItemTracking.Client', 'Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore'), ('CSS' , 'Microsoft.TeamFoundation', 'Microsoft.TeamFoundation.Server.ICommonStructureService'), ('GSS' , 'Microsoft.TeamFoundation', 'Microsoft.TeamFoundation.Server.IGroupSecurityService'), ('REG' , 'Microsoft.TeamFoundation', 'Microsoft.TeamFoundation.Server.IRegistration'), ('AUTH' , 'Microsoft.TeamFoundation', 'Microsoft.TeamFoundation.Server.IAuthorizationService'), ('EVENT', 'Microsoft.TeamFoundation', 'Microsoft.TeamFoundation.Server.IEventService') ) } process { # Fetch the TFS instance, but add some useful properties to make life easier # Make sure to "promote" it to a psobject now to make later modification easier [psobject] $tfsLocal = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($serverName) foreach ($entry in $propertiesToAdd) { #write-debug ("Adding property: " + $entry[0]) $scriptBlock = ' [System.Reflection.Assembly]::LoadWithPartialName("{0}") > $null $this.GetService([{1}]) ' -f $entry[1],$entry[2] $tfsLocal | add-member scriptproperty $entry[0] $ExecutionContext.InvokeCommand.NewScriptBlock($scriptBlock) } return $tfsLocal } } Write-Host "Get TFS Server Script installed!" #------------------------------------------------------------------------------ # If $sPath is a relative path, add our current working directory as a # prefix to the $sPath. If $sPath is already a full absolute path, # don't add the prefix. function MakePathAbsolute( [string]$sPath ) { #"Initial path: " + $sPath | out-string # Outputing stuff here will mess up the function return value !! $sPath = ($sPath.Trim()).Trim('"') # " Trim double-quotes too. $sAbsPath = $sPath if( ($sAbsPath -match '^[A-Za-z]:') -or ($sAbsPath.StartsWith("\\")) ) # If it is already an absolute path... { } else { $sAbsPath = [System.IO.Path]::Combine($PWD,$sAbsPath); # Note: $PWD is the current dir that the caller may have CD'ed to. } return $sAbsPath } # ----------------------------------------------------------------------------- # 'Get' a single file from the TeamFoundationServer source control database. # $sLocalDir is the *relative* path on our local machine where the file will be deposited. # -'Get' the single file and put it into $sLocalDir. # If $sTFSlabel is empty, then the 'latest version' is fetched. # Return value (in $sPathPlusFilenameFetched): The fetched filename if successful, empty string otherwise. # function GetSingleFileFromTFS( [ref]$sPathPlusFilenameFetched, [string] $sTFSpathPlusFilename, [string] $sTFSlabel, [string] $sLocalDir ) { begin { [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Common") [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Client") } process { $iRtn = -4; # Initial Error value $sPathPlusFilenameFetched.value = "" # initial condition: failure $oTFS = $g_tfs if (! $oTFS ) { HaltScript "The creation of the new TFS object for (" + $g_myServer + ") failed." } $sTFSFolderName = [Microsoft.TeamFoundation.VersionControl.Common.VersionControlPath]::GetFolderName( $sTFSpathPlusFilename ) if($sTFSlabel -eq "") # Empty string means 'get latest version'. { write-host "The Label is empty." $verSpec = [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest } else { write-host "The Label is: " $sTFSlabel #[Microsoft.TeamFoundation.VersionControl.Client.LabelVersionSpec] $verSpec = new-object -typeName "Microsoft.TeamFoundation.VersionControl.Client.LabelVersionSpec" -argumentList $sTFSlabel } if($verSpec -ne $null) { #"verSpec is: " +$verSpec | out-string $sFilename = [Microsoft.TeamFoundation.VersionControl.Common.VersionControlPath]::GetFileName( $sTFSpathPlusFilename ) $sRelativePathPlusFilename = [System.IO.Path]::Combine($sLocalDir, $sFilename); $sAbsolutePathPlusFilename = MakePathAbsolute $sRelativePathPlusFilename write-host "Getting single file from TFS...`n TFSfolder : " $sTFSpathPlusFilename "`n LocalFilename: " $sAbsolutePathPlusFilename -foregroundcolor darkgreen $oTFS.vcs.DownloadFile( $sTFSpathPlusFilename, 0, $verSpec, $sAbsolutePathPlusFilename ); #Note: if the specified workingFolder string is not an existing folder in TFS, the above call will # throw an exception. We need to handle it and let our callers # know that the call failed. (Or do a $ErrorActionPreference = "stop") $iRtn = 0 # Success indicator } else { write-host "The call to create the verSpec object failed." write-host "verSpec is: " $verSpec } if($iRtn -eq 0) # If everything worked so far, assign the returned filename. { $sPathPlusFilenameFetched.value = $sAbsolutePathPlusFilename; # This is returned. } } # end process } Write-Host "Get Single File From TFS Script installed!" # ----------------------------------------------------------------------------- # Using an existing TFS workspace, $oWorkspace (created elsewhere), # map $sProjName to the workspace in the $sProjLocalDir. # Then 'Get' the source code from the project and put it into $sProjLocalDir. # Return value: 0 if successful, non-zero otherwise. # function GetMapWorkFolderAndCode( [ref]$iRtn, $oWorkspace, [string]$sProjName, [string]$sProjLocalDir, [string]$sProjTreeToGet, [string]$sLabel ) { $iRtn.value = -4 # initial condition: failure #[Microsoft.TeamFoundation.VersionControl.Client.WorkingFolder] $myworkingfolder = new-object -typeName "Microsoft.TeamFoundation.VersionControl.Client.WorkingFolder" -argumentList $sProjName, $sProjLocalDir if($myworkingfolder -ne $null) { # Create a mapping between the workspace and the working folder: $oWorkspace.CreateMapping($myworkingfolder); if($sLabel -eq "") # Empty string means 'get latest version'. { write-host "The Label is empty." $verSpec = [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest } else { write-host "The Label is: " $sLabel #[Microsoft.TeamFoundation.VersionControl.Client.LabelVersionSpec] $verSpec = new-object -typeName "Microsoft.TeamFoundation.VersionControl.Client.LabelVersionSpec" -argumentList $sLabel } if($verSpec -ne $null) { #"verSpec is: " +$verSpec | out-string write-host "Getting Project: " $sProjName " ProjLocalDir: " $sProjLocalDir " ProjTreeToGet: " $sProjTreeToGet -foregroundcolor darkgreen # Get the files from the repository. $items = ($sProjTreeToGet) [Microsoft.TeamFoundation.VersionControl.Client.GetStatus]$Status = $oWorkspace.Get( $items, $verSpec, [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full, [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::Overwrite -bor [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::GetAll ) ; write-host "Results from GET: " $Status ; $iRtn.value = ($Status.GetFailures()).Length # This is returned. #$oWorkspace.Get(); # This works too, to get the latest version } else { write-host "The call to create the verSpec object failed." write-host "verSpec is: " $verSpec } } else { write-host "Failed creating workingfolder for Project: " $sProjName " ProjLocalDir: " $sProjLocalDir -foregroundcolor red } } Write-Host "Get Map Work Folder and Code From TFS Script installed!"
2.循环获取文件夹下文件:
cls $tfs=Get-TfsServer -name http://TFSServer:8080/ # ItemType="Folder" $f="TFSPath" $folder=Get-TfsChildItem -Item $f -Deleted -server $tfs |select -Skip 1 |where {$_.ItemType -eq "Folder"}|select ServerItem #$folder foreach($f1 in $folder) { Get-TfsChildItem -Item $f1.ServerItem -Deleted -server $tfs |select -Skip 1 |where {$_.ItemType -ne "Folder"}|select ServerItem,ItemType,ChangesetId
这些对于基本的bulid项目应该可以了。