Resizing a virtual hard disk
Recently, I had to resize a VHD (virtual hard disk) that I installed Windows Server 2008 on. I soon found out that the size of 10GB is not such a hot idea when installing Team Foundation Server 2010 and SQL Server 2008 on the same instance. Hence, I decided to resize the disk instead of re-installing all of the software.
Below is the recipe to resize the virtual hard disk:
Part 1:
The first part is to resize the container of the partition, which is done by means of a sector copy. After the resize completed, the container will have increased but the partition will not be fully extended yet.
- Download VHD Resizer from VMToolkit.
- Install and run VHD Resizer.
- In the file open dialog select the VHD that you want to resize.
- Type in the new filename to use for the extended VHD.
- Type in the size of the new container to create.
- Click on Resize. The process takes a few minutes…
Part 2:
Now that the container is resized, we must now extend the partition to make use of the full container size.
- Attach the new extended VHD as a non-primary drive to another virtual machine. Do not extend the partition using the virtual machine that will make use of it.
- Start the virtual machine to which the extended VHD was attached as the non-primary drive and open a command prompt.
- Type in diskpart and press Enter.
- Type in list disk.
- Ensure that disk 1 or greater is the extended disk.
- Type in select disk 1.
- Type in list partition.

- Ensure that the partition sizes matches the old size.
- Type in extend.
- Wait for the process to complete and type exit.
Remember to change the disk on the original virtual machine to now use the extended disk. The disk is now resized and ready to be used from the original virtual machine.
Sinking your interview before it starts
During a series of interviews I discovered some things about reading CV’s that downright irritate me. I share them here with you just in case your interviewer is anything like me :).
- Keep it short and to the point. CV’s that span more than four pages is too long. I had the displeasure of reading a CV of seventeen pages. That makes me unhappy. Your interviewer simply does not have the time to read your entire life story.
- Observe the DRY (Don’t repeat yourself) principle in your CV. Do not repeat the fact that you have a degree three times.
- Keep the details relevant. I want a programmer. I’m not interested in the fact that you are a landlord or that you are the chairperson of your body corporate.
- If you have been between jobs frequently and they have been contract based, make sure you state it clearly.
- Check and check again for spelling mistakes. There is nothing more annoying than spelling mistakes in a CV.
If you are working through a recruitment agency, insist on reviewing your CV before they send it out. Often the candidate will send the recruitment agency a two page CV and then they will apply power word magic and out comes a CV of seventeen pages. Recruitment agents also tend to embellish CV’s. This is the worst mistake they can make as it will paint this picture-perfect image of you that might not match up to the reality when you are interviewed.
Skills matrices are my absolute favorite. Rating yourself an expert in C# will make me raise an eyebrow if your name is not Anders Hejlsberg or Eric Lippert. Call yourself an expert if you can almost recite the ECMA specifications off by heart.
How to sink yourself in the interview
Sinking your interview isn’t all that hard. When answering questions in an interview, remember that there is a time to shut up. Ten minutes to answer a question is too long. Lookout for the interviewers glazed stare. That is a sign that you have crossed the shut-up-already limit a couple of miles ago.
Remember that you don’t know the people that are interviewing you. They have their opinions and stances and you can offend them in no time. Always keep this in mind.
Programming pragmatically
Don’t program if… you have not yet read The Pragmatic Programmer: From Journeyman to Master by Andy Hunt and David Thomas. I never understood what all the fuss was about and here I am making a fuss about it.
pragmatic 1540s, from M.Fr. pragmatique , from L. pragmaticus ”skilled in business or law,” from Gk. pragmatikos ”versed in business,” from pragma (gen. pragmatos ) “civil business, deed, act,” from prassein ”to do, act, perform.” (1)
Pragmatic programmer
So, what makes a pragmatic programmer? A pragmatic programmer is someone who exhibits certain attitudes and practices when thinking, writing, testing and deploying software. This includes being an early adopter or fast adapter, being inquisitive and thinking critically. It also means being realistic and being anything-ready. And to be anything -ready one must learn continuously.
One of the major philosophies is to always place the problem and the solution in a larger context. This means to always think beyond the immediate problem and see the bigger picture. Why? Simply, so that you can make informed decisions such as deciding where to comprise and where to apply the Pareto principle.
A pragmatic programmer takes responsibility for everything they do. They don’t just sit there and watch the code rot. A pragmatic programmer will always endeavor to keep the code pristine.
Instigating change is another important skill that a pragmatic programmer must learn to apply. People are naturally opposed to change and in order to be a pragmatic in a software project, it is sometimes necessary to break the “we have always done it like this” mentality.
Skills and Tools
Using the appropriate tools is one of the best ways to amplify your productivity. Try various tools and pick the ones that work for you. Then, learn them extremely well. Mold them to the shape of your hand. Consider the following categories of tools:
- Source control
- Text manipulation
- Debugging
Another important tool is to be able to touch type. This is a worthwhile skill to learn, so be prepared to put in a lot of time.
Life as a pragmatic programmer
Being a pragmatic programmer does not make you write perfect programs. Accept it. Things will go wrong and when they do, we should ensure that the data always remain consistent. As the book says “dead programs tell no lies” and sometimes it is best to just simply kill it.
As long as there are people, software will change, continuously. That’s life. Pragmatic programmers guard themselves against the impact of these changes by observing the Law of Demeter (it’s not a dot counting exercise) and decoupling dependencies. Keep separate concepts separate.
Coding is not just a matter of transcribing requirements into source code. It is a continuous process of making decisions. Decisions that will either pave your way or cause you grief later on. The choice is yours. Think critically about every line of code and remember to refactor continuously. Test, test and test some more.
While building software, there are two important principles that a pragmatic programmer always observes. These are avoiding duplication at all costs and avoid splitting a piece of knowledge across multiple components. Failing to observe these two principles will invariably bring you headaches in the future as the source code spirals out of control.
This is the way of the pragmatic programmer.
By the way, there are loads of tips in the book that will not only benefit you now, but for as long you write code.
(1) Dictionary.com, “pragmatic,” in Online Etymology Dictionary. Source location: Douglas Harper, Historian. http://dictionary.reference.com/browse/pragmatic. Available: http://dictionary.reference.com. Accessed: June 13, 2010.
Mocking multiple interfaces using Rhino Mocks
A while back on StackOverflow I asked a question on how to create a mock object with Rhino Mocks that implements multiple interfaces. In other words, I want to generate a mock that implements more than one interface. This baffled me for a while and I was shown the light by one of my colleagues.
A multi-mock is created like so:
var mocker = new MockRepository(); var mock = mocker.CreateMultiMock<IPrimaryInterface>(typeof(IFoo), typeof(IBar)); mock.Expect(x => x.AnswerToUniverse()).Return(42); mocker.ReplayAll();
Note the call to ReplayAll. Without this call the mock will not be setup with the intended values.
Unit test deployment issue when file is untrusted
Recently, I decided to use NHibernate in a project so that I could achieve database affinity. That provided me with another benefit and that was the ability to create a database in-memory for testing purposes. Sounded great, but while trying to run the unit tests using MsTest and SQLite, I received the following error: “Test Run deployment issue: The location of the file or directory ‘C:\projects\myproject\SQLite\sqlite3.dll’ is not trusted.“.
That problem was easily solved by simply unblocking the file. Below are the steps on how to unblock the file:
- Right-click the blocked file (sqlite3.dll), and then click Properties.
- This will open the properties dialog:
- In the General tab, click Unblock.
- Click on OK.
That’s it, the file is now trusted and the runtime should be able to load the file successfully.
Kindle review
I finally took the plunge and purchased the Kindle 6″ Global Wireless edition from Amazon. I simply couldn’t wait for it to arrive. Something akin to a 7 year old getting his first bicycle. Finally, the day arrived!
Packaging
The packaging was so impressive it is almost worth its own blog post – the attention to detail was outstanding.
For example, how much attention do you pay to bar codes? Well, Amazon didn’t slip on bar code detail. Have a closer look and you’ll see that little man sitting under the tree reading his book…
The next interesting part is the “Certified frustration-free packaging” seal. That seal lives up to the promise. Opening the box is done with a peel-off strip. What a neat way to open the box. Again, Amazon had the detail going – the opening strip read “Once upon a time…”. Mission accomplished: frustration-free packaging.
Cover
I decided to get the well-worth-it leather cover for the additional $31.99. At the bottom right there is a metal branding insert. Nice idea, although, I was a little disappointed with the top right hand corner as the leather wasn’t properly fitted around the plate. The stitching around the edges is very neat and the feel of the cover is luxurious.
Inside, it has a soft padded inner; front and back; that is essential to protect the screen from accidental bumps. Before I bought the cover, I was perplexed about how the device attaches to the cover. Maybe with elastic bands around each corner, I thought?! The website wasn’t too detailed about it and for the price of $31.99 I would expect it to be a decent cover with a proper attachment mechanism. And so it was. The attaching mechanism of the cover just simply blew me away.
With only two ingenious hinges the cover and the device will have a hard time separating by itself. The bottom hinge slides into the device at an angle and the top hinge slides into the device. It has a spring action and releases when pulled downwards. So, the device attaches by sliding in the bottom hinge at an angle and secured by the top hinge. Ingenious.
Power adapter
B
attery powered devices have this tendency to run down from time to time and needs to be charged every so often. Luckily the Kindle ships with more than one way to get this much needed charge. Firstly, there is charging by USB and secondly, charging by plugging into the mains. The latter is done with an American style two pin plug. Having ordered the global edition, it is a little unusual to find an American style plug in the packaging. With this in mind Amazon realized that this is not quite optimal and included another adapter that converts the American style to an European two pin style plug. Great, I live in South Africa where we have huge three pin plugs. Nothing that another adapter can’t fix. A downside is that the battery is not easily replaced, except by sending it in to Amazon. I suspect that this is by design. Annuity income?
The device
When taking the device out of the packaging, I was surprised that instructions were printed on the screen. How amazing to realize that E-Ink doesn’t consume any power to display static text or images. As can be seen in the photo, a random image is rendered on the display every time it is switched off.
I was pleasantly surprised at the quality of the display. It renders fonts with a very nice smooth precise anti alias effect. The display can be read from almost any angle. The downside is that it doesn’t have a back light and I hope that it will make an appearance in future editions. Although I can easily justify it by saying “you can’t read a book in the dark, can you?”. Another little annoying thing with the display is the “clearing” effect when paging. With e-ink, the reader first has to black the current page, clear the page and then render the text or image. However, this process is fairly quick on the Kindle and one gets used to the effect quickly.
In South Africa I was also surprised to find the 3G connection worked first time round without any hassles and automatically registered the Kindle at Amazon when the first connection was established.It seems to be making use of the Vodacom network. Using the 3G connection is weighs down on the battery and I would recommend using it only when necessary.
The buttons are perfectly placed on the device – when you are not using the cover. Using the left-hand buttons with the cover on is not user friendly. It is rather silly that the previous page-button is only on the left. Not a problem if you use the device without the cover. The five-way scroll button is perhaps a little on the small side but quite functional. Don’t expect to use the keyboard like an ordinary one. The buttons are tiny. Why is there a keyboard on Kindle?
When reading a book on the Kindle, you’ll be surprised to know that it comes with a dictionary that is automatically invoked when you scroll to a word and hover. Brilliant. You can do manual searches in the dictionary, make annotations on pages. Setting a bookmark on a page makes a little dog-ear shape appear in the top right-hand corner.
Another surprise was the back of the device. It has a very neat brushed metal finish and makes it feel expensive. The serial number is printed at the bottom, in case you are wondering. Beside that the Kindle has built-in text-to-speech functionality which works brilliantly.
Loading your own PDF documents to the Kindle is a cinch. It mounts the same as a flash drive in Windows and books and PDF documents are copied to the documents folder. One thing that I found is that when plugging it into the USB port the device becomes “unusable” until you eject it from Windows. I have tried to eject it to get it usable while plugged in but with no success. Maybe it’s just me?
Being able to load PDF documents (from version 2.3) is great. Not being able to zoom them is not so great. I hope to see a zoom feature added to the PDF documents reader. The display can be rotated but the reader just doesn’t feel right being read in a rotated manner.
Shopping on the Kindle is great experience with custom layouts specially formatted for the Kindle. Beware that the quickest way to being broke is to enable the Buy now with 1-Click facility. It’s ease of use is addictive.
When reading a book the Kindle disappears once you get into the book. It’s a great reading device and at the price of $259 I strongly recommend it.
Automating a ClickOnce Deployment – Part3
Continuing the series on Automating a ClickOnce Deployment it’s time to implement the deployment scheme in a build file.
After the build file compiled the solution we are ready to start the copying of the installation files to the deployment server. Before we start the copy process we have to create a new directory for the new application version. In the following snippet I simply generate a new version number and then create a directory where we will copy the installation files to:
<Target Name="DeployApplication"> <Message Text="Deploying the client application to $(PublishServer)" /> <MakeDir Directories="$(DeploymentServer)\Published\App\$(VersionNumber)" />
We are ready to copy the application files to the server:
<Message Text="Copying files to deployment directory [$(DeploymentServer)\Published\App\$(VersionNumber)]" /> <Copy DestinationFiles="@(ApplicationFiles->'$(DeploymentServer)\Published\App\$(VersionNumber)\%(Filename)%(Extension)')" SourceFiles="@(ApplicationFiles->'$(BinariesFolder)\%(Filename)%(Extension)')" />
The ApplicationFiles is an item group with the installation files that we are copying. Note: we do not transform the filename to include the .deploy extension because we first need to generate the deployment manifest with the files in the right location before changing the name of the files. (See Gotcha #3). Next, we create the application manifest with the Mage utility which will be stored in the application directory (not in the version directory). Mage is a manifest generator and is used to generate both application and deployment manifests. In the snippet below we are using Mage explicitly to generate the application manifest:
<CreateProperty Value="$(DeploymentServer)\Published\App\$(VersionNumber)\App.exe.manifest"> <Output TaskParameter="Value" PropertyName="ApplicationManifestFile"/> </CreateProperty> <Message Text="Generating application manifest [$(ApplicationManifestFile)] using [$(DeploymentServer)\Published\App\$(VersionNumber)]" /> <Exec Command="mage.exe -New Application -p msil -TrustLevel FullTrust -ToFile $(ApplicationManifestFile) -Name "App" -Version $(VersionNumber) -FromDirectory $(DeploymentServer)\Published\App\$(VersionNumber)" />
So far so good, we have now managed to generate the deployment manifest from the files located in the …\Published\App directory and placed the file in the …\Published\App\$(VersionNumber) directory. We will now point the application manifest to this deployment manifest so that the correct files are installed on the client machine.
We now have everything that we need in order to generate the application manifest.
<CreateItem Include="$(ApplicationManifestFile)" AdditionalMetadata="TargetPath=$(VersionNumber)\App.exe.manifest"> <Output TaskParameter="Include" ItemName="RelativeApplicationManifestFile"/> </CreateItem> <GenerateDeploymentManifest AssemblyName="App.exe.application" AssemblyVersion="$(VersionNumber)" DeploymentUrl="http://deployment-server/Published/App/App.exe.application" Product="App" Description="App" Publisher="thirdshelf.com" Install="true" UpdateEnabled="true" UpdateMode="Foreground" OutputManifest="$(DeploymentServer)\Published\App\App.exe.application" MapFileExtensions="true" EntryPoint="@(RelativeApplicationManifestFile)" />
If you read part 2 of this series you should be able to map these properties straight to their positions in the application manifest file and their functions. The only thing that remains is to rename the files to include their .deploy extensions. This can easily be done in the following way:
<Message Text="Replacing files in [$(DeploymentServer)\Published\App\$(VersionNumber)\] with [.deploy]" /> <Delete Files="@(ApplicationFiles->'$(DeploymentServer)\Published\App\$(VersionNumber)\%(Filename)%(Extension)')" /> <Copy SourceFiles="@(ApplicationFiles->'$(BinariesFolder)\%(Filename)%(Extension)') DestinationFiles="@(ApplicationFiles->'$(DeploymentServer)\Published\App\$(VersionNumber)\%(Filename)%(Extension).deploy')" />
Visiting the URL http://deployment-server/Published/App/App.exe.manifest and clicking on the Install button you should see the following dialog:
Gotchas
There are a few gotchas that you might find on your way to success and I’ve listed the most common ones here and their solutions for your convenience:
Gotcha #1: Reference in the manifest does not match the identity of the downloaded assembly …
This is easily resolved by adding the NoWin32Manifest property to solution in the SolutionsToBuild group:
<SolutionToBuild Include="$(SolutionRoot)\Application.sln"> <Properties>NoWin32Manifest=true</Properties> </SolutionToBuild>
Gotcha #2: XML files are marked as data files, by default.
This means that XML files are published to the data directory whose location can found by querying the System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory property.
Gotcha #3: The customHostSpecified attribute is not supported for Windows Forms applications.
Not a very helpful error, but chances are that you generated the deployment manifest with the installation files having the .deploy extension and not on the original filenames.
Automating a ClickOnce Deployment – Part2
Continuing the series on Automating a ClickOnce Deployment it’s time to have a look at the manifest files. There are two required manifest files that give clickonce the necessary information to deploy the application:
- an application manifest (.application);
- a deployment manifest (.manifest).
Application Manifest
The application manifest describes the application that we are going to install. So, let’s have a look at the contents of this file:
xml version="1.0" encoding="utf-8"?>
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1" xmlns="urn:schemas-microsoft-com:asm.v2" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xrml="urn:mpeg:mpeg21:2003:01-REL-R-NS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity name="App.application" version="1.0.903.18476" publicKeyToken="0000000000000000" language="neutral" processorArchitecture="msil" xmlns="urn:schemas-microsoft-com:asm.v1" />
asmv2:publisher="App Publishers Company" asmv2:product="App" xmlns="urn:schemas-microsoft-com:asm.v1">App
mapFileExtensions="true">
<subscription>
<update>
<beforeApplicationStartup />
</update>
</subscription>
<deploymentProvider codebase="http://deployment-server/Published/App/app.exe.application" />
</deployment>
<dependency>
<dependentAssembly dependencyType="install" codebase="1.0.903.18476\App.exe.manifest" size="33600">
<assemblyIdentity name="App.exe" version="1.0.903.18476" publicKeyToken="0000000000000000" language="neutral" processorArchitecture="msil" type="win32" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<dsig:DigestValue>DrLiyVjtlKhOmtuzuXhFSTqi/9Q=
</hash>
</dependentAssembly>
</dependency>
</asmv1:assembly>
One of the important things to note in this manifest is the mapFileExtensions attribute in the deployment tag. This attribute indicates that our installation files have a .deploy extension – MyApplication.exe is copied to the deployment server as MyApplication.exe.deploy and is renamed when installed on the client machine. In the deploymentProvider tag we specify where the application can find future updates and from where this application was installed.
The next interesting tag is dependentAssembly, which points to the deployment manifest to use when installing the application on the client machine. We specify the deployment manifest in the codeBase attribute which describes how the application installs. Note that the codebase attribute points to a version folder in which the deployment manifest resides – for every new release of the application this application manifest is either overwritten or updated to point to the new version of the installation files and deployment manifest.
Deployment Manifest
The deployment manifest describes the how the application installs and what files to install on the client. Once again, let’s have a look at the contents:
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns="urn:schemas-microsoft-com:asm.v2" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1">
<asmv1:assemblyIdentity name="App.exe" version="1.0.903.18476" publicKeyToken="0000000000000000" language="neutral" processorArchitecture="msil" type="win32" />
<application />
<entryPoint>
<assemblyIdentity name="App" version="1.0.903.18476" language="neutral" processorArchitecture="msil" />
<commandLine file="App.exe" parameters="" />
</entryPoint>
<trustInfo>
<security>
<applicationRequestMinimum>
<PermissionSet Unrestricted="true" ID="Custom" SameSite="site" />
<defaultAssemblyRequest permissionSetReference="Custom" />
</applicationRequestMinimum>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<dependency>
<dependentOS>
<osVersionInfo>
<os majorVersion="5" minorVersion="1" buildNumber="0" servicePackMajor="0" />
</osVersionInfo>
</dependentOS>
</dependency>
<dependency>
<dependentAssembly dependencyType="preRequisite" allowDelayedBinding="true">
<assemblyIdentity name="Microsoft.Windows.CommonLanguageRuntime" version="2.0.50727.0" />
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="DevExpress.Data.v9.1.dll" size="855552">
<assemblyIdentity name="App" version="9.1.5.0" publicKeyToken="B88D1754D700E49A" language="neutral" processorArchitecture="msil" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<dsig:DigestValue>CXehkPHON56TeTj7ZWsy++QsymE=
</hash>
</dependentAssembly>
</dependency>
<file name="GlobalConfiguration.xml" size="21074">
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<dsig:DigestValue>6hHtbL9F9sfgWmKLYrWRpYmILCE=
</hash>
</file>
</asmv1:assembly>
The first tag of interest here is the entryPoint tag that identifies the application to run once the installation completes. In order to start the application clickonce will execute the command with the parameters specified in the commandLine tag.
Every application has dependencies that must either be installed with the program or should have been installed. In the first dependency tag we see that the minimum operating system version required is Windows XP. In the second dependency tag we see that the dependencyType attribute instructs clickonce to verify that the .NET Framework 2.0 is already installed on the client.
In the third dependency tag we see that the dependencyType is install, which instructs clickonce to install this dependency on the client. Remember that the .deploy extension will automatically be removed during the installation process.
The next tag is a file tag that specifies a non-assembly file that is copied into the application installation directory. Note: by default when clickonce encounters a xml file it assumes it as a configuration file and will copy it to a data directory; along the application directory; instead of the application directory. You can find the data directory location by querying the System.Deployment.ApplicationApplicationDeployment.DataDirectory property.
In the next installment of this series we will show a build file example and generate a deployment manifest with the Mage tool.
Executing a task remotely with MsBuild
Recently, we were in need of executing a command on a remote server as part of our automated build process. For this we decided to make use of Windows Management Instrumentation (WMI). Before jumping to the code, copying and pasting it, there are always some security issues that need to be addressed. For this task there are also some service configurations that are required on the remote machine.
Note: All of the following settings are configured on the remote machine on which the command will be run remotely.
Security
Follow the steps below to set the WMI permissions for the build service account:
- Click on Start > Control Panel > Administrative Tools > Computer Management.
- Expand Service and Applications.
- Right-click on WMI Control and click on Properties.
- On the property dialog select the Security tab.
- Expand Root > CIMV2.
- Click on the Security button.
- Click on Add and add <TFSSERVICE> account.
- In the list of permissions, allow the following permissions:
- Execute methods
- Remote enable
- Click on OK.
Follow the step below to set the Local Security Policy for the build service account:
- Click on Start > Control Panel > Administrative Tools > Local Security Policy.
- Expand Local Policies > User Rights Assignment.
- Find and double-click on Log on as a service.
- Click on Add User or Group and add <TFSSERVICE> account.
- Click on OK.
- Find and double-click Log on as a batch job.
- Click on Add User or Group and add <TFSSERVICE> account.
- Click on OK and close the Local Security Policy window.
Services
Follow the steps below set the dependent services to automatically start:
- Click on Start > Control Panel > Administrative Tools.
- Double click on Services.
- Ensure that the following services are Started, and are set to start Automatic.
- COM+ Event System
- Remote Access Auto Connection Manager
- Remote Access Connection Manager
- Remote Procedure Call (RPC)
- Remote Procedure Call (RPC) Locator
- Remote Registry
- Server
- Windows Management Instrumentation
- Windows Management Instrumentation Driver Extensions
- WMI Performance Adapter
- Workstation
- Close the services dialog.
Here is the code for the build task that will execute a command on a remote machine:
/// <summary>
/// Executes a command line on a remote machine.
/// </summary>
public class RemoteExec : Task
{
/// <summary>
/// Initialises a new instance of the <see cref="RemoteExec"/> class.
/// </summary>
public RemoteExec()
{
RemoteMachine = Environment.MachineName;
}
/// <summary>
/// Gets or sets the name of the machine on which to execute the command.
/// </summary>
public string RemoteMachine
{
get;
set;
}
/// <summary>
/// Gets or sets the command to execute on the remote machine.
/// </summary>
[Required]
public string Command
{
get;
set;
}
/// <summary>
/// Executes the task on the remote machine.
/// </summary>
/// <returns>true if the task succeeded, otherwise, false</returns>
public override bool Execute()
{
if (string.IsNullOrEmpty(Command))
{
Log.LogError("Command property was not set.");
throw new ArgumentNullException("Command");
}
var connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
var managementScope = new ManagementScope(
string.Format(@"\\{0}\ROOT\CIMV2", RemoteMachine), connOptions);
managementScope.Connect();
Log.LogMessage(string.Format(CultureInfo.CurrentCulture, "Connected to {0}: {1}",
RemoteMachine, managementScope.IsConnected));
if (!managementScope.IsConnected)
return false;
var objectGetOptions = new ObjectGetOptions();
var managementPath = new ManagementPath("Win32_Process");
var processClass = new ManagementClass(managementScope, managementPath, objectGetOptions);
var processParameters = processClass.GetMethodParameters("Create");
processParameters["CommandLine"] = Command;
try
{
var outParams = processClass.InvokeMethod("Create", processParameters, null);
var processID = Convert.ToUInt32(outParams["processId"]);
Log.LogMessage(string.Format(CultureInfo.CurrentCulture,
"Creation of the process {0} returned: {1}", processID, outParams["returnValue"]));
var stopQuery = new WqlEventQuery(string.Format(CultureInfo.CurrentCulture,
"select * from Win32_ProcessStopTrace where ProcessID={0}", processID));
var processStopEvent = new ManagementEventWatcher(managementScope, stopQuery);
processStopEvent.Options.Timeout = new TimeSpan(0, 1, 0);
Log.LogMessage("Waiting for process to complete...");
processStopEvent.WaitForNextEvent();
processStopEvent.Stop();
Log.LogMessage("Completed.");
}
catch (Exception ex)
{
Log.LogError(ex.Message);
}
return true;
}
}
The task can then be used as follows:
<UsingTask TaskName="RemoteExec" AssemblyFile="BuildTasks.dll"/> <RemoteExec RemoteMachine="$(Server)" Command="application.exe" />
There we have it, executing a command remotely from MsBuild!
Creating JunctionPoints with MsBuild
With our build process we decided on a deployment folder structure that required us to create NTFS Junction Points. Junction Points are links to other folders, also known as soft links. Below is an example of how these links appear in Windows 7. It also shows our deployment folders for various build types:
All the folders with shortcut arrows are junction points or links to folders in the Published folder. For each build, the binaries are copied to a build numbered folder in the Published folder. Only when a build is successful; meaning that all code compiled and unit tests passed; do we update the junction point to the relevant build numbered folder in Published.
By using the code written by Jeff Brown in this article, I was able to create a custom build task that enabled us to the create and delete junction points with relative ease. Here is a link to the source code for an MsBuild task that will enable you to manage junction points with MsBuild.



leave a comment