Using the Visual Studio 2010 Power Tools and PowerShell to reduce the brain-numbing setup work
If you, like the Visual Studio ALM Rangers, create a lot of hands-on-lab (HOL) content you will be familiar with the brain-numbing setup work to create team projects, create workspaces, check-in code, branch and create work items. One of the initiatives we have been working on is trying to get to a point where we can run one script and setup all Rangers HOLs on a virtual machine … and we are getting close.
Branching Guidance HOL Extract
We create a team project and setup a number of user rights, whereby the Team Foundation Server Power Tools April 2010 come into play on line 105:
1: # Copyright © Microsoft Corporation. All Rights Reserved.
2: # This code released under the terms of the
3: # Microsoft Public License (MS-PL, https://opensource.org/licenses/ms-pl.html.)
4: #
5: #<#
6: #.SYNOPSIS
7: # This script configures the enviroment for the hands on labs scenarios.
8: # It is only intended to be run after the TFS enviroment is fully configured.
9: #
10: #.DESCRIPTION
11: # This script performs the following steps:
12: # 1) Creates the sample users
13: # 2) Adds the sample users to the correct TFS groups
14: #
15: #.NOTES
16: # File Name : 3-hol.ps1
17: # Author : VSTS Rangers
18: # Requires : PowerShell V1
19: #
20: #.LINK
21: # This script posted to:
22: # https://???.codeplex.com
23: # License:
24: # https://opensource.org/licenses/ms-pl.html
25: #>
26:
27:
28: function createUser([string]$accountName, [string]$password, [string]$name)
29: {
30: # create user
31: $computer = [adsi] 'WinNT://localhost'
32: $user = $computer.Create('User', $accountName)
33: $user.SetPassword($password)
34: $user.SetInfo()
35: $user.FullName = $name
36: $user.put('UserFlags',65536 -bor 64)
37: $user.SetInfo()
38:
39: # add user to Remote desktop group to allow RD access
40: net localgroup 'Remote Desktop Users' $accountName /add
41:
42: # add user to Power Users group to allow them to logon locally on TR images
43: net localgroup 'Power Users' $accountName /add
44: }
45:
46: function createSampleAccounts
47: {
48: createUser 'michaf' '#############' 'Michael Affronti (PM)'
49: createUser 'aprist' '#############' 'April Stewart (Dev Lead)'
50: createUser 'dorikr' '#############' 'Doris Krieger (Dev)'
51: createUser 'abuobe' '#############' 'Abu Obeida Bakhach (Dev)'
52: createUser 'chriko' '#############' 'Christine Koch (Tester)'
53: createUser 'chriba' '#############' 'Chris Barry (Business Stakeholder)'
54: createUser 'robiwo' '#############' 'Robin Wood (End User)'
55: }
56:
57: function addToTFSGlobal([string]$accountName, [string]$groupname )
58: {
59: $currentLocation = Get-Location
60: Set-Location 'c:\Program Files\Microsoft Team Foundation Server 2010\Tools'
61: .\tfssecurity /g+ $groupname n:$accountName /server:localhost
62: Set-Location $currentLocation
63: }
64:
65: function addToTFSProject([string]$accountName, [string]$groupname, [string]$collection = '', [string]$targetProjectName = '')
66: {
67: $target = ''
68: if ($targetProjectName -ne '')
69: {
70: $target = '[' +$targetProjectName + ']\' +$groupname
71: }
72: else
73: {
74: $target = $groupname
75: }
76:
77: if (!$accountName.Contains('\'))
78: {
79: $accountName = $hostname + '\' + $accountName
80: }
81:
82: $currentLocation = Get-Location
83: Set-Location 'c:\Program Files\Microsoft Team Foundation Server 2010\Tools'
84: $argument = 'https://localhost:8080/tfs/' + $collection
85: .\tfssecurity /g+ $target n:$accountName /collection:$argument /server:$hostname
86: Set-Location $currentLocation
87: }
88:
89: function addSampleAccountsToGroups
90: {
91: addToTFSGlobal 'michaf' 'Team Foundation Administrators'
92: addToTFSGlobal 'aprist' 'Team Foundation Administrators'
93: addToTFSProject 'michaf' 'Project Administrators' '/defaultcollection' 'BranchingGuide'
94: addToTFSProject 'aprist' 'Project Administrators' '/defaultcollection' 'BranchingGuide'
95: addToTFSProject 'dorikr' 'Contributors' '/defaultcollection' 'BranchingGuide'
96: addToTFSProject 'abuobe' 'Contributors' '/defaultcollection' 'BranchingGuide'
97: addToTFSProject 'chriko' 'Contributors' '/defaultcollection' 'BranchingGuide'
98: addToTFSProject 'chriba' 'Contributors' '/defaultcollection' 'BranchingGuide'
99: addToTFSProject 'robiwo' 'Readers' '/defaultcollection' 'BranchingGuide'
100: }
101:
102: function SetupHOL
103: {
104: #Create team project with tfpt
105: &'\Program Files\Microsoft Team Foundation Server 2010 Power Tools\TFPT.EXE' createteamproject /settingsfile:'C:\HOL\Branching Guidance\HOLSetup\Project_Branch.XML'
106:
107: createSampleAccounts
108: addSampleAccountsToGroups
109: }
110:
111: Write-Host -Object:""
112: Write-Host -ForegroundColor Red -Object "Setting up the Branching Guidance HOL Environment"
113: Write-Host -Object:"-------------------------------------------------"
114: SetupHOL
TFS Integration Platform HOL Setup Script Extract
Apart from creating a number of team projects (excluded in the extract), we also check-in code … the PowerTools assist us on lines 9-11 and 32.
1: # Copyright © Microsoft Corporation. All Rights Reserved.
2: # This code released under the terms of the
3: # Microsoft Public License (MS-PL, https://opensource.org/licenses/ms-pl.html.)
4: #
5:
6: function SetupHOL
7: {
8: #Create team project with tfpt
9: &'\Program Files\Microsoft Team Foundation Server 2010 Power Tools\TFPT.EXE' createteamproject /settingsfile:'C:\HOL\TFS Integration Platform\HOLSetup\Project_TP-A.XML'
10: &'\Program Files\Microsoft Team Foundation Server 2010 Power Tools\TFPT.EXE' createteamproject /settingsfile:'C:\HOL\TFS Integration Platform\HOLSetup\Project_TP-B.XML'
11: &'\Program Files\Microsoft Team Foundation Server 2010 Power Tools\TFPT.EXE' createteamproject /settingsfile:'C:\HOL\TFS Integration Platform\HOLSetup\Project_TP-C.XML'
12:
13: #Create Workspace
14: &'\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe' workspace /new TP-A /noprompt /collection:https://localhost:8080/tfs/DefaultCollection
15: &'\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe' workfold /unmap $/
16: &'\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe' workspace /new TP-B /noprompt /collection:https://localhost:8080/tfs/DefaultCollection
17: &'\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe' workfold /unmap $/
18: &'\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe' workspace /new TP-C /noprompt /collection:https://localhost:8080/tfs/DefaultCollection
19: &'\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe' workfold /unmap $/
20:
21: #Create the VC space as per HOL
22: &'\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe' workfold /map $/TP-A 'C:\HOL\TFS Integration Platform\Source Code\Demo\Sandbox-A' /workspace:TP-A
23: &'\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe' add 'C:\HOL\TFS Integration Platform\Source Code\Demo\Sandbox-A\Main' /recursive
24: &'\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe' checkin 'C:\HOL\TFS Integration Platform\Source Code\Demo\Sandbox-A\Main' /comment:'HOL Automated Checkin' /recursive /noprompt
25: &'\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe' branch 'C:\HOL\TFS Integration Platform\Source Code\Demo\Sandbox-A\Main’ ‘$/TP-A/Dev’
26: &'\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe' checkin 'C:\HOL\TFS Integration Platform\Source Code\Demo\Sandbox-A\Dev' /comment:'HOL Automated Branch' /noprompt
27: copy 'C:\HOL\TFS Integration Platform\HOLSetup\Raw\HelloWorldDemo' 'C:\HOL\TFS Integration Platform\Source Code\Demo\Sandbox-A\Dev' -recurse
28: &'\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe' add 'C:\HOL\TFS Integration Platform\Source Code\Demo\Sandbox-A\Dev\HelloWorldDemo' /recursive
29: &'\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe' checkin 'C:\HOL\TFS Integration Platform\Source Code\Demo\Sandbox-A\Dev' /comment:'HOL Automated Checkin' /recursive /noprompt
30:
31: #create workitem
32: &'\Program Files\Microsoft Team Foundation Server 2010 Power Tools\TFPT.EXE' workitem /new TP-A\Bug /Fields:"Title=Change the string goodbye world to hello world;Assigned To= Administrator" /collection:'https://localhost:8080/tfs/defaultcollection'
33: }
34:
35: Write-Host -Object:""
36: Write-Host -ForegroundColor Red -Object "Setting up the TFS Integration Platform HOL Environment"
37: Write-Host -Object:"-------------------------------------------------------"
38: SetupHOL
What does a typical XML file look like as referred to in the branching script when creating a team project?
1: <?xml version="1.0" encoding="utf-8"?>
2: <Project xmlns="ProjectCreationSettingsFileSchema.xsd">
3: <TFSName>https://LocalHost:8080/tfs/defaultcollection</TFSName>
4: <LogFolder>c:\HOL\Logs</LogFolder>
5: <ProjectName>BranchingGuide</ProjectName>
6: <ProjectSiteTitle>Visual Studio 2010 Branching Guidance</ProjectSiteTitle>
7: <ProjectSiteDescription>Visual Studio 2010 Branching Guidance</ProjectSiteDescription>
8: <SccCreateType>new</SccCreateType>
9: <SccBranchFromPath></SccBranchFromPath>
10: <ProcessTemplateName>MSF for Agile Software Development v5.0</ProcessTemplateName>
11: </Project>
While we still have to sanitize and cleanup the scripts … an initiative that the South-African Rangers are focused on … we can now run a script, go away for coffee and return to a fully configured Rangers Base HOL image.