Monday, November 13, 2017

http://it-by-doing.blogspot.com/2015/07/using-powershell-to-get-latest-from.html

Copyright from: it-by-doing.blogspot.com

While working on SharePoint 2013 and all deployments we had to do, I could see a big need to get all PowerShell scripts into a repository, we use Microsoft TFS.
But why only use it as respository for versioning control, why not also let PowerShell/Orcestrator do a get latest, pack it in a SCCM package for distribution to all needed servers?

And out came a very simple script that can do a "get latest" from TFS.

You need to have Visual Studio Team Explorer installed in order to use this script.
Create your own script and load this scrip in and execute with your needed parameters.
Enjoy!



if ((Get-PSSnapIn -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin Microsoft.TeamFoundation.PowerShell
}

#Load Reference Assemblies
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")  
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")  
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Common")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.WorkItemTracking.Client")


Function Get-TFSLatestFromVersionControl
{
param
(
    #FQDN to SCCM Server
    [Parameter(Mandatory=$true)][string]$YourServerPath = "$/Path To Your Department",
    [Parameter(Mandatory=$true)][string]$YourLocalPath = "E:\TFS\SI",
    [Parameter(Mandatory=$false)][string]$tfsCollectionUrl = "http://fqdn.to.your.server.com:8080/tfs/url/"
)

    #Delete the old local copy
    if((Test-Path -Path ($YourLocalPath)) -eq 1 )
    {
        Remove-Item -Path $YourLocalPath -Recurse
        New-Item -Path $YourLocalPath -Type directory
    }


    #Get Team Project Collection
    $teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionUrl)

    #enter a path to your tfs server
    $tfsServer = $tfsCollectionUrl
     # get an instance of TfsTeamProjectCollection
    $tfs=get-tfsserver $tfsServer
    # get an instance of VersionControlServer
    $vCS = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
    $TeamProject = $YourServerPath.Remove(0,2)
    $tfsProject = $vcs.GetTeamProject($TeamProject)


    $workspace = $vcs.GetWorkspace($YourLocalPath)
    if($workspace -eq $null)
    {
        $vcs.DeleteWorkspace("TFS-"+$env:COMPUTERNAME,$env:USERNAME)
        $workspace = $vcs.CreateWorkspace("TFS-"+$env:COMPUTERNAME, $env:USERNAME)
    }

    $workspace.Map($YourServerPath, $YourLocalPath)

    $workspace.Get([Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest ,[Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::Overwrite)

}

No comments: