A while ago Powershell Desired State Configurations (DSC) was introduced. Powershell DSC allows you to create a configuration of a specific machine in a powershell file. The Powershell engine takes care of rolling out this configuration.
This configuration snippet makes sure a IIS is present. If it already is it stops, if not, it installs it. Powershell DSC is a great way of scripting your machine configurations and eventually crucial for enabling Continuous Delivery.
It is important to enable DevOps scenarios as well. That’s why Powershell DSC deployments are now part of Visual Studio Release Management Update 3. (check this post)
There are some great resources about Powershell DSC. You’ll find some at the bottom of this post. As a starter you can watch this TechEd video of Kenneth Hansen and Jeffrey Snover. The founders of DSC
Installing a build server
I wanted to install a build server on a new Virtual Machine. I thought I would give DSC a try. With help of a great post of fellow ALM MVP Colin Dembovsky, I managed to do it. The steps I needed to take were
- Download an ISO
- Mount an iso in Powershell and silently install TFS 2013 build server
- Configure Build Controller and Build Agents
- Run Powershell DSC
In the next paragraphs I will further describe my steps
Download an ISO
First I wanted to download an ISO file. From an URL. In my case some location on AzureStorage.
For this you can use this powershell script
function DownloadFile($url, $targetfile) { $webclient = New-Object System.Net.WebClient $webclient.DownloadFile($url,$file) } $tfsServerPath = "c:\Downloads\vs2013.3_tfs_enu.iso" DownloadFile -url "https://azurestorage.blob.core.windows.net/iso/
vs2013.3_tfs_enu.iso" -targetfile $tfsServerPath
Mount an iso in Powershell and silently install TFS 2013
build server
Once downloaded, I wanted to mount an iso. I did this with the following Powershell DSC script
# # Install TFS Server # Script InstallTFSServer { DependsOn = "[File]TFSServerIso" GetScript = { $tfsInstalled = Test-Path –Path
"$env:ProgramFiles\Microsoft Team Foundation Server 12.0\Tools\TfsConfig.exe" $tfsInstalled } SetScript = { # mount the iso $setupDriveLetter =
(Mount-DiskImage -ImagePath C:\Temp\vs2013.3_tfs_enu.iso
-PassThru | Get-Volume).DriveLetter + ":" if ($setupDriveLetter -eq $null) { throw "Could not mount TFS install iso" } Write-Verbose "Drive letter for iso is: $setupDriveLetter" # run the installer $cmd = "$setupDriveLetter\tfs_server.exe /install /quiet" Write-Verbose "Command to run: $cmd" Invoke-Expression cmd | Write-Verbose } TestScript = { $tfsInstalled = Test-Path –Path
"$env:ProgramFiles\Microsoft Team Foundation Server 12.0\Tools\TfsConfig.exe" if ($tfsInstalled) { Write-Verbose "TFS server already installed" } else { Write-Verbose "TFS server is not installed" } $tfsInstalled } }
Configure Build Controller and Build Agents
Once the Build server has been installed you need to configure the Build Controller and agents. This cannot be done with a standard TFS Commandline. For this purpose I wrote an Powershell CmdLet (wonderful resource here). This can be called to configure the Assembly location of the Build Controller and the Source location of the Build Agents.
Import the cmdlet in your Powershell with this command, and then write the script to execute it
Import-Module "<path>\RoadToALM.TfsBuildHelpers.dll" Set-BuildConfiguration -TfsCollectionUrl "http://tfs:8080/defaultcollection" -CustomAssemblyPath "$\teamproject\assemblies" -BuildAgentSourceDirectory "d:\buildsource\"
Of course you can wrap this in a DSC as well. Use the Script Resource configuration for this as well.
You can download the cmdlet binaries + source here
Run DSC
Once you have assembled your DSC, call it as a function and run the Start-DSCConfiguration Command to execute it.
Configuration ConfigurationNameHere { Node ("MACHINE") { #Script ... ... } } #Call Configuration, Builds a "package" ConfigurationNameHere Start-DscConfiguration -Path $PSScriptRoot\ConfigurationNameHere
-Wait -Force -Verbose
This creates a package and executes it.
Download the source code of the cmdlet here:
Summary
As you can see Powershell DSC has a lot of possiblities to execute powerful stuff. Dive into it and start doing it now.
Resources
- Introduction Powershell DSC – http://technet.microsoft.com/en-us/library/dn249912.aspx
- Announcement VS Update 3 – http://technet.microsoft.com/en-us/library/dn249912.aspx
- Teched Video – http://channel9.msdn.com/Events/TechEd/NorthAmerica/2013/MDC-B302#fbid=
- Powershell DSC MSDN Docs – http://technet.microsoft.com/en-us/library/dn249921.aspx
- Colin Dembovsky about DSC – http://www.colinsalmcorner.com/post/install-and-configure-sql-server-using-powershell-dsc
- Building a Poweshell CmdLet – http://www.powershellmagazine.com/2014/03/18/writing-a-powershell-module-in-c-part-1-the-basics/
- http://blogs.msdn.com/b/powershell/archive/2014/08/07/introducing-the-azure-powershell-dsc-desired-state-configuration-extension.aspx
- http://blogs.technet.com/b/privatecloud/archive/2013/08/30/introducing-powershell-desired-state-configuration-dsc.aspx
Great script! You’re missing a $ here though:
Invoke-Expression cmd | Write-Verbose
It should say:
Invoke-Expression $cmd | Write-Verbose
Thanks !!
Hii Rene
I tried to work with this script and modified it with TFS 2012 but it is throwing an error :
ValidateNodeResources : Resource ‘[File]TFSServerIso’ required by
‘[Script]InstallTFSServer’ does not exist. Please ensure that the required
resource exists and the name is properly formed.
At C:\windows\system32\windowspowershell\v1.0\Modules\PSDesiredStateConfiguratio
n\PSDesiredStateConfiguration.psm1:665 char:17
+ ValidateNodeResources
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Write-Error], InvalidOpera
tionException
+ FullyQualifiedErrorId : RequiredResourceNotFound,ValidateNodeResources
Errors occurred while processing configuration ‘TFSAutomation’.
At C:\windows\system32\windowspowershell\v1.0\Modules\PSDesiredStateConfiguratio
n\PSDesiredStateConfiguration.psm1:2088 char:5
+ throw $errorRecord
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (TFSAutomation:String) [], Inva
lidOperationException
+ FullyQualifiedErrorId : FailToProcessConfiguration
———————————————————————————————-
Below is my script which I m trying to execute.
Configuration TFSAutomation{
node(“localhost”)
{
WindowsFeature IIS
{
Ensure =”Present”
Name =”Web-server”
}
# Install TFS Server #
Script InstallTFSServer
{
DependsOn = “[File]TFSServerIso”
GetScript =
{
$tfsInstalled = Test-Path –Path
“$env:Program Files\Microsoft Team Foundation Server 11.0\Tools\TfsConfig.exe”
$tfsInstalled
}
SetScript =
{
# mount the iso #
$setupDriveLetter =
(Mount-DiskImage -ImagePath C:\Temp\vs2012.3_tfs_enu.iso -PassThru | Get-Volume).DriveLetter + “:”
if ($setupDriveLetter -eq $null) {
throw “Could not mount TFS install iso”
}
Write-Verbose “Drive letter for iso is: $setupDriveLetter”
# run the installer #
$cmd = “$setupDriveLetter\tfs_server.exe /install /quiet”
Write-Verbose “Command to run: $cmd”
Invoke-Expression $cmd | Write-Verbose
}
TestScript =
{
$tfsInstalled = Test-Path –Path
“$env:Program Files\Microsoft Team Foundation Server 11.0\Tools\TfsConfig.exe”
if ($tfsInstalled) {
Write-Verbose “TFS server already installed”
} else {
Write-Verbose “TFS server is not installed”
}
$tfsInstalled
}
}
}
}
TFSAutomation
Start-DscConfiguration -Path C:\Users\simittal\Desktop\TFSAutomation -Verbose
Please Help regarding this
Thanks in advance
Siddhartha
You state DependsOn = “[File]TFSServerIso” but I do not see a [File] configuration in your script..
TfsConfig needs an elevated Command Prompt window on Windows server 2012. How did you bypassed it?
Don’t exactly remember. It just worked when running from RM.
How did you configure the Build feature in TFS after running the tfs_server.exe?
You still need to open the TFS Administration Console and click on Build Configuration, Configure Installed Features then you can configure the custom assembly path.