Wednesday, 17 February 2016

ASP.Net 5 (RC1 Update1) Deploy to Azure Website with Visual Studio Team Services Release

How to setup a build with Visual Studio Team Services, for building ASP.Net 5 (ASP.Net Core 1.0 now) is explained in here. Let’s look at steps necessary to create a release pipeline to deploy, ASP.Net 5 website to Azure, with VS Team Service Release.
First requirement is to develop a poweshell script, whcih is capable of deploying, the built ASP.Net package to Azure. Information on how to write such script is available here.image
Script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
param($websiteName, $packOutput)

$website = Get-AzureWebsite -Name $websiteName

# get the scm url to use with MSDeploy.  By default this will be the second in the array
$msdeployurl = $website.EnabledHostNames[1]


$publishProperties = @{'WebPublishMethod'='MSDeploy';
                        'MSDeployServiceUrl'=$msdeployurl;
                        'DeployIisAppPath'=$website.Name;
                        'Username'=$website.PublishingUsername;
                        'Password'=$website.PublishingPassword}


$publishScript = "${env:ProgramFiles(x86)}\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Web Tools\Publish\Scripts\default-publish.ps1"


. $publishScript -publishProperties $publishProperties  -packOutput $packOutput
Add this script to version control.image
Modify our release build in previous blog to copy deploy script to build output.image
image
image
Let’s create Azure Web Application as the target.image
image
Site created and ready.image
In Visual Studio Team Services, Release tab create a new empty Release Definition.image
image
To link the release definition to the build created earlier, click on Link to a build definition.image
Select the build definition and link it to release definition.image
Rename the Default Environment to DevInt(Or what ever name you prefer).image
In the DevInt environment create a configuratoin varibale to hold the Azure web site name.image
image
It is possible to set approvers to the stage. So that an approval requires to get the build deploy to the stage/environment DevInt. Let’s set it to autimated and after deployment set a manual approval, to allo get it approved before deploying to next stage/enviornment in the pipeline.image
image
In the release definition add a Azure Powershell task to execute the deployment script. Azure subscription how to link to VS Team Services account can be found here. Set the deployment script to Azure Powershell release task in Dev Intimage
image
For Web Site Name environment variable can be used in the cript arguments.
-websiteName $(WebSiteName) -packOutput $(System.DefaultWorkingDirectory)\Event.Rel\EventRelOutput\image
Eventhough trigger can be set to continuous deployment like below.image
Let’s leave trigger to manua for the time being.image
Now you can trigger a release with the any successful build output you have using the release definition.image
image
image
Build get deloyed to DevInt environment and wait for the post deployment approval.image
DevInt environment site is running now.image
An Improvement to the build number can be made like following.image
Release name format also can be set to use build number for it to be more meaningful.
Release No $(Build.DefinitionName)-$(Build.BuildNumber)-R-$(rev:r)image
Multiple environements in the pipeline can be configured.image
Deployement logs in the pipeline. You can view these logs, while a release is happening.image
Release history can be viewed.image
Release definition change history is available as well t diagnose any issue which might have ocuured with a change to the definition.image
Difference between two defnitions.image
image
New release managment services with VS Team Services is awsome, and let’s explore more in the coming posts.

Tuesday, 2 February 2016

Build ASP.Net 5 (RC1 Update1) Project with Visual Studio Team Services Build

Building ASP.Net 4 MVC witth VS Team Services Build and deploying it to Azure web site with VS Team Services Release, is somewhat straight forward. But building an ASP.Net 5 MVC 6 (EF 7) web application with VS Team Services hosted build services, and deploying it with VS Team Services Release, requires some addtional steps.

(Asp.Net 5 is now no more and named as ASP.Net Core 1.0. It is a name change and still the ASP.Net 5 RC1 Update1 is the available version. ASP.Net Core 1.0 is not released yet.)

Let’s look at steps required to build ASP.Net 5 RC1 Update1, MVC 6 web application with Visual Studio Team Services build.

Simple web application with ASP.Net 5 should be created and checked in to TFS.image

CI Build

First try to create a CI build which builds each changeset checked in. Create a build definition and add a Visual Studio Build step.image

When a build is triggered with above defintition it fails giving below error.

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DNX\Microsoft.DNX.targets(126,5): Error : The Dnx Runtime package needs to be installed. See output window for more details.image

This is because hosted build server does not have dnx Runtime packages available in it. As explained in article here use the script to download the required packages before build. Slight modification done to support the releative path of the project, with the location of the script in the verision control.image

image

A gobal.json file added to supply the dnx version that is required to dnu restore.image

image

A new build step should be added to execute the Pre Build script, to install the dnx packages using dnu restore.image

Dnx packages (rc1 update1) get installed with the pre build step. image

But the build step still look for beta8 of dnx packages, and dnu restore does not seem to be working.image

To install the latest dnx in the hosted build server, add to “dnvm upgrade -r clr” Pre Build script.image

Because of a missing mistake in the path of the project.json location, in the script below error occured.

CS0234: The type or namespace name 'AspNet' does not exist in the namespace 'Microsoft'image

This error fixed by making sure relative path to the is correct.image

Final script here.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# bootstrap DNVM into this session.
&{$Branch='dev';iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.ps1'))}

# load up the global.json so we can find the DNX version
$globalJson = Get-Content -Path $PSScriptRoot\..\..\EventBooking\global.json -Raw -ErrorAction Ignore | ConvertFrom-Json -ErrorAction Ignore

if($globalJson)
{
    $dnxVersion = $globalJson.sdk.version
}
else
{
    Write-Warning "Unable to locate global.json to determine using 'latest'"
    $dnxVersion = "latest"
}

# install DNX
# only installs the default (x86, clr) runtime of the framework.
# If you need additional architectures or runtimes you should add additional calls
# ex: & $env:USERPROFILE\.dnx\bin\dnvm install $dnxVersion -r coreclr
& $env:USERPROFILE\.dnx\bin\dnvm install $dnxVersion -Persistent

# run DNU restore on all project.json files in the src folder including 2>1 to redirect stderr to stdout for badly behaved tools
Get-ChildItem -Path $PSScriptRoot\..\..\EventBooking -Filter project.json -Recurse | ForEach-Object { & dnu restore $_.FullName 2>1 }

dnvm upgrade -r clr

Then works dnu restore and build succeeds.image

image

With CI build the build output is not made available. But for release bbuild we need artifacts to be able to downloaded and deployed with Visual Studio Team Services Release.image

Release Build and Publich Artifacts

Let’s clone this CI build and create a release build. The steps to publish build artifacts for an ASP.Net 5 application is explained here. Using the script available in the article, a modification added to make it work without having a Team Project namea and releative path to project is set.image

Modified script.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
param($projectName, $buildConfiguration, $buildStagingDirectory)
 
$VerbosePreference = "continue"
$ErrorActionPreference = "Stop"
     
&{$Branch='dev';iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.ps1'))}
$globalJson = Get-Content -Path $PSScriptRoot\..\..\EventBooking\global.json -Raw -ErrorAction Ignore | ConvertFrom-Json -ErrorAction Ignore
 
if($globalJson)
{
    $dnxVersion = $globalJson.sdk.version
}
else
{
    Write-Warning "Unable to locate global.json to determine using 'latest'"
    $dnxVersion = "latest"
}
 
& $env:USERPROFILE\.dnx\bin\dnvm install $dnxVersion -Persistent
 
$dnxRuntimePath = "$($env:USERPROFILE)\.dnx\runtimes\dnx-clr-win-x86.$dnxVersion"
     
#& "dnu" "build" "$PSScriptRoot\src\$projectName" "--configuration" "$buildConfiguration"
Write-Warning "Path is $PSScriptRoot\..\..\EventBooking\$projectName"
 
& "dnu" "publish" "$PSScriptRoot\..\..\EventBooking\$projectName" "--configuration" "$buildConfiguration" "--out" "$buildStagingDirectory" "--runtime" "$dnxRuntimePath"

THis script is added to source control to a Post Build script folder.image

A build post step added to execute the post build ASP.Net 5 publish step and below arguments passed to script.

-projectName "BookMyEvents" -buildConfiguration $(BuildConfiguration) -buildStagingDirectory $(build.stagingDirectory)

$(build.stagingDirectory) is temporary location to publish and it is a predefined agent build variable.image

image

Above will publish the ASP.net 5 application but it is not available to download yet.To get the published output available to download, add Copy and Publish Build Artifact step. For this specify the $(build.stagingDirectory) as the Copy Root and provide an Artifact Name. Set the Artifact Type to Server.image

Downloaded build artifact containes the published ASP.Net 5 application.image

image

image

In the next post let’s look at how to get the ASP.Net 5 RC1 Update 1 application, deployed as Azure web application using Visual Studio Team Services Release.

Tuesday, 19 January 2016

Update Script Timeout in MTM Test Settings

You could run into default script timeout of five minutes, while running scripts before running tests using MTM.

image

image

Error    1/19/2016 11:10:39 AM    The setup batch file on agent machine 'vstfs:///LabManagement/TestMachine/19' exceeded the execution timeout period.    vstfs:///LabManagement/TestMachine/19

How to Resolve

To increase this timeout there is no straight forward way using MTM. However update deployment timeout is explained in here, utilizing the tool created by Aseem Bansal. But setting Script timeout not available with this particular tool.

As explained in this MSDN Q&A, I have made changes to the code downloaded from the post here by Aseem Bansal and improved it to enable setting the Script Timeout for test settings. You can download the new utility from Technet Gallery here, and use it as explained below (VS 2013 source code for the tool available here). 

UpdateTestSettings /collection:<ProjectCollectionUri> /teamProject:<ProjectName> /settingsname:<TestSettingsName> [/bucketSize:<value>] [/deploymentTimeout:<value>] [/scriptTimeout:<value>]

Examples:

UpdateTestSettings /collection:http://abc:8080/tfs/DefaultCollection /teamProject:myProject /settingsname:My2_0_App /bucketSize:200 /deploymentTimeout:600000

UpdateTestSettings /collection:http://abc:8080/tfs/DefaultCollection /teamProject:myProject /settingsname:My2_0_App /bucketSize:200 /deploymentTimeout:600000 /scriptTimeout:900000

UpdateTestSettings /collection:http://abc:8080/tfs/DefaultCollection /teamProjec:myProject /settingsname:My2_0_App /scriptTimeout:900000

Utility improved to provide Test Settings infor before and after update.
 image

How the code is modified

You can download the original code for the utility from the link provided in the post here.

image

image

When this is built with VS 2013 there could be below errors if you do not have VS 2010 in your machine.image

With or without having above errors remove the references for TeamFoundation dlls.image

There is no Microsoft.TeamFoundation.dll for VS 2013. Microsoft.TeamFoundation.Common.dll would add all thats is added previously by Microsoft.TeamFoundation.dll. Add the references to Team Foundation dlls.image

Remove “Read only” from all code files downloaded before change framework.image

Set the framework to 4.5 and you can build the code with VS 2013 successflully.image

Replace the code in Program.cs with the below updated Program.cs code.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// ---------------------------------------------------------------------------
// <copyright file="Program.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <summary>
//   Updates the specified properties in the tcm test settings.
// 
//   MTM UI does not expose updation of bunch of test settings properties
//   but the same can be achieved via the TCM APIs. This utility uses those
//   APIs to update the following properties in tcm test settings. 
// 
//   1. Deployment timeout. 
//   2. Bucket size. 
//   3. Apartment state. 
// 
// Introduced ScriptTimeout - Chaminda Chandrasekara 19/01/2016
// </summary>
// ---------------------------------------------------------------------------

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
using Microsoft.VisualStudio.TestTools.Common;
using Microsoft.VisualStudio.TestTools.Common.Xml;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Xml;

namespace UpdateTestSettings
{
    class Program
    {
        static void Main(string[] args)
        {
            CommandParameters commandParameters;

            Console.WriteLine();
            Console.WriteLine();

            // Validate the parameters
            ValidateArguments(args, out commandParameters);
            Console.WriteLine("Using parameters: {0}", commandParameters);


            // Connect to the team project. 
            TfsTeamProjectCollection collection = new TfsTeamProjectCollection(new Uri(commandParameters.ProjectCollectionUri));
            TestManagementService tcmService = collection.GetService<TestManagementService>();
            ITestManagementTeamProject teamProject = tcmService.GetTeamProject(commandParameters.ProjectName);
            Console.WriteLine("Connected to the team project {0}.", commandParameters.ProjectName);


            // Find the specified test settings.
            TestRunConfiguration testRunConfiguration = null;
            ITestSettings testSetting = FindTestSettings(commandParameters.TestSettingsName, teamProject, out testRunConfiguration);
            
            // Get ScriptTimeout hidden property info
            PropertyInfo pInfo = testRunConfiguration.GetType().GetProperty("ScriptTimeout", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
            Console.WriteLine("==============================================================");
            Console.WriteLine("Current TestSettings {0} has bucketSize={1}, deploymentTimeout={2}, scriptTimeout={3}  and ApartmentState={4}.", testSetting.Name, testRunConfiguration.BucketSize, testRunConfiguration.DeploymentTimeout, pInfo.GetValue(testRunConfiguration, null), testRunConfiguration.ApartmentState);
            Console.WriteLine("==============================================================");

            Console.WriteLine("Found the testsettings named {0}.", commandParameters.TestSettingsName);

            // If it is an update command, then update the settings
            if (commandParameters.IsUpdate)
            {
                UpdateTestSettings(testSetting, testRunConfiguration, commandParameters);
                Console.WriteLine("Updated the testsettings.");
                Console.WriteLine();
            }

            // Show the new values to the user
            
            // Reload test configurations after update
            FindTestSettings(commandParameters.TestSettingsName, teamProject, out testRunConfiguration);

            Console.WriteLine("==============================================================");
            Console.WriteLine("Updated TestSettings {0} has bucketSize={1}, deploymentTimeout={2}, scriptTimeout={3}  and ApartmentState={4}.", testSetting.Name, testRunConfiguration.BucketSize, testRunConfiguration.DeploymentTimeout, pInfo.GetValue(testRunConfiguration, null),testRunConfiguration.ApartmentState);
            Console.WriteLine("==============================================================");
        }

        /// <summary>
        /// Update the tcm test settings as per the specified command-line parameters
        /// </summary>
        private static void UpdateTestSettings(ITestSettings testSetting, 
                                               TestRunConfiguration testRunConfiguration, 
                                               CommandParameters commandParameters)
        {
            if (commandParameters.HasBucketSize)
            {
                testRunConfiguration.BucketSize = commandParameters.BucketSize;
            }
            if (commandParameters.HasDeploymentTimeout)
            {
                testRunConfiguration.DeploymentTimeout = commandParameters.DeploymentTimeout; 
            }
            if (commandParameters.HasApartmentState)
            {
                testRunConfiguration.ApartmentState = commandParameters.ApartmentState;
            }
            if (commandParameters.HasScriptTimeout)
            {
                PropertyInfo pInfo = testRunConfiguration.GetType().GetProperty("ScriptTimeout", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);

                pInfo.SetValue(testRunConfiguration, commandParameters.ScriptTimeout, null);
            }

            // Tcm test settings contains the TestRunConfiguration xml which has all the settings information. 
            // During the update, we update the testRunConfiguration xml in the test settings
            //
            XmlElement element = LocalXmlPersistence.SaveTestRunConfiguration(testRunConfiguration);
            testSetting.Settings = element;

            testSetting.Save();
        }


        /// <summary>
        /// Find the parameter test settings on the team project
        /// </summary>
        private static ITestSettings FindTestSettings(string testSettingsName, 
                                                      ITestManagementTeamProject teamProject, 
                                                      out TestRunConfiguration testRunConfiguration)
        {
            Console.WriteLine("Finding the testsettings named {0}.", testSettingsName);
            ITestSettings testSetting = null;
            IEnumerable<ITestSettings> testSettings = teamProject.TestSettings.Query("SELECT * FROM TestSettings");

            StringBuilder availableSettings = new StringBuilder();
            foreach (ITestSettings setting in testSettings)
            {
                if (string.Equals(setting.Name, testSettingsName, StringComparison.OrdinalIgnoreCase))
                {
                    if (!setting.IsAutomated)
                    {
                        Console.Error.WriteLine("Warning!! Ignoring testsettings named {0} (with id {1}) as it is a manual testsettings. You should specify only the automated testsettings.", setting.Name, setting.Id);
                        continue;
                    }

                    testSetting = setting;
                    break;
                }

                // We dont want to pick the manual test settings. 
                if (!setting.IsAutomated)
                {
                    continue;
                }
                else
                {
                    if (availableSettings.Length > 0)
                    {
                        availableSettings.Append(",");
                    }

                    availableSettings.Append("\"");
                    availableSettings.Append(setting.Name);
                    availableSettings.Append("\"");
                }
            }

            // Throw error if setting is not found. 
            //
            if (testSetting == null)
            {
                testRunConfiguration = null;

                if (string.IsNullOrEmpty(availableSettings.ToString()))
                {
                    throw new Exception(string.Format(CultureInfo.InvariantCulture, "No automated testsettings found with name {0}. Additionally there is no automated testsettings available in this project. So create an automated testsettings using 'Microsoft Test Manager' and then try again.", testSettingsName));
                }
                else
                {
                    throw new Exception(string.Format(CultureInfo.InvariantCulture, "No automated testsettings found with name {0}. The available automated testsettings are: {1}", testSettingsName, availableSettings));
                }
            }

            // Load the xml from the txm settings into TestRun configuration
            testRunConfiguration = new TestRunConfiguration(testSetting.Name, "dummy");
            testRunConfiguration.Load(testSetting.Settings, XmlTestStoreParameters.GetParameters());
            testRunConfiguration.Name = testSetting.Name;

            return testSetting;
        }


        /// <summary>
        /// Validate the command-line parameters
        /// </summary>
        private static void ValidateArguments(string[] args, out CommandParameters commandParameters)
        {
            if (args.Contains("?", StringComparer.OrdinalIgnoreCase)
                || args.Contains("/?", StringComparer.OrdinalIgnoreCase))
            {
                ShowHelpMessage();
                Environment.Exit(-1);
            }

            string collectionSwitch = "/collection:";
            string projectSwitch = "/teamProject:";
            string testSettingsSwitch = "/settingsname:";
            string bucketSizeSwitch = "/bucketsize:";
            string deploymentTimeoutSwitch = "/deploymentTimeout:";
            string scriptTimeoutSwitch = "/scriptTimeout:";
            string apartmentStateSwitch = "/apartmentState:";
            

            string projectCollectionUri = null;
            string teamProjectName = null;
            string testSettingsName = null;
            int bucketSize = -1;
            int deploymentTimeout = -1;
            int scriptTimeout = -1;
            ApartmentState apartmentState = ApartmentState.Unknown;

            foreach (string argument in args)
            {
                if (argument.StartsWith(collectionSwitch, StringComparison.OrdinalIgnoreCase))
                {
                    projectCollectionUri = argument.Substring(collectionSwitch.Length);
                }
                else if (argument.StartsWith(projectSwitch, StringComparison.OrdinalIgnoreCase))
                {
                    teamProjectName = argument.Substring(projectSwitch.Length);
                }
                else if (argument.StartsWith(testSettingsSwitch, StringComparison.OrdinalIgnoreCase))
                {
                    testSettingsName = argument.Substring(testSettingsSwitch.Length);
                }
                else if (argument.StartsWith(bucketSizeSwitch, StringComparison.OrdinalIgnoreCase))
                {
                    string bucketSizeValue  = argument.Substring(bucketSizeSwitch.Length);
                    bucketSize = int.Parse(bucketSizeValue);
                }
                else if (argument.StartsWith(deploymentTimeoutSwitch, StringComparison.OrdinalIgnoreCase))
                {
                    string deploymentTimeoutValue = argument.Substring(deploymentTimeoutSwitch.Length);
                    deploymentTimeout = int.Parse(deploymentTimeoutValue);
                }
                else if (argument.StartsWith(scriptTimeoutSwitch, StringComparison.OrdinalIgnoreCase))
                {
                    string scriptTimeoutValue = argument.Substring(scriptTimeoutSwitch.Length);
                    scriptTimeout = int.Parse(scriptTimeoutValue);
                }
                else if (argument.StartsWith(apartmentStateSwitch, StringComparison.OrdinalIgnoreCase))
                {
                    string apartmentStateValue = argument.Substring(apartmentStateSwitch.Length);
                    apartmentState = (ApartmentState)Enum.Parse(typeof(ApartmentState), apartmentStateValue, true);
                }
            }

            bool projectCollectionUriError = false, teamProjectError = false, testSettingsError = false;
            if (string.IsNullOrEmpty(projectCollectionUri))
            {
                projectCollectionUriError = true;
            }
            else if (string.IsNullOrEmpty(teamProjectName))
            {
                teamProjectError = true;
            }
            else if (string.IsNullOrEmpty(testSettingsName))
            {
                testSettingsError = true;
            }

            if (projectCollectionUriError || teamProjectError || testSettingsError)
            {
                if (projectCollectionUriError)
                {
                    Console.Error.WriteLine("ProjectCollectionUri argument is not specified");
                }
                else if (teamProjectError)
                {
                    Console.Error.WriteLine("TeamProjectName argument is not specified");
                }
                else if (testSettingsError)
                {
                    Console.Error.WriteLine("Settingsname argument is not specified");
                }

                ShowHelpMessage();
                Environment.Exit(-1);
            }

            commandParameters = new CommandParameters(projectCollectionUri, teamProjectName, testSettingsName);
            commandParameters.BucketSize = bucketSize;
            commandParameters.DeploymentTimeout = deploymentTimeout;
            commandParameters.ScriptTimeout = scriptTimeout;
            commandParameters.ApartmentState = apartmentState;
        }

        private static void ShowHelpMessage()
        {
            Console.Error.WriteLine("Usage: UpdateTestSettings /collection:<ProjectCollectionUri> /teamProject:<ProjectName> /settingsname:<TestSettingsName> [/bucketSize:<value>] [/deploymentTimeout:<value>] [/apartmentState:<value>]");
            Console.Error.WriteLine();
            Console.Error.WriteLine("Examples:");
            Console.Error.WriteLine("UpdateTestSettings /collection:http://abc:8080/tfs/DefaultCollection /teamProject:myProject /settingsname:My2_0_App /bucketSize:200 /deploymentTimeout:600000 /apartmentState:MTA");
            Console.Error.WriteLine();
            Console.Error.WriteLine("UpdateTestSettings /collection:http://abc:8080/tfs/DefaultCollection /teamProject:myProject /settingsname:My2_0_App");
            Console.Error.WriteLine();
            Console.Error.WriteLine("Default values: BucketSize=100 & DeploymentTimeOut=300000 & ApartmentState=STA.");
        }


    }


    /// <summary>
    /// Helper class to capture the command-line inputs
    /// </summary>
    public class CommandParameters
    {
        private string m_projectCollectionUri;
        private string m_teamProjectName;
        private string m_testSettingsName;

        private int m_bucketSize = -1;
        private int m_deploymentTimeout = -1;
        private int m_scriptTimeout = -1;
        private ApartmentState m_apartmentState = ApartmentState.Unknown;


        public CommandParameters(string projectCollectionUri, string teamProjectName, string testSettingsName)
        {
            Debug.Assert(!string.IsNullOrEmpty(projectCollectionUri), "Project collection uri cannot be empty");
            Debug.Assert(!string.IsNullOrEmpty(teamProjectName), "Team Project name cannot be empty");
            Debug.Assert(!string.IsNullOrEmpty(testSettingsName), "TestSettings name cannot be empty");

            m_projectCollectionUri = projectCollectionUri;
            m_teamProjectName = teamProjectName;
            m_testSettingsName = testSettingsName;
        }

        public string ProjectCollectionUri
        {
            get { return m_projectCollectionUri; }
        }

        public string ProjectName
        {
            get { return m_teamProjectName; }
        }
        public string TestSettingsName
        {
            get { return m_testSettingsName; }
        }

        public int BucketSize
        {
            get { return m_bucketSize; }
            set { m_bucketSize = value; }
        }

        public bool HasBucketSize
        {
            get { return m_bucketSize != -1; }
        }

        public int DeploymentTimeout
        {
            get { return m_deploymentTimeout; }
            set { m_deploymentTimeout = value; }
        }

        public bool HasDeploymentTimeout
        {
            get { return m_deploymentTimeout != -1; }
        }

        public int ScriptTimeout
        {
            get { return m_scriptTimeout; }
            set { m_scriptTimeout = value; }
        }

        public bool HasScriptTimeout
        {
            get { return m_scriptTimeout != -1; }
        }

        public ApartmentState ApartmentState
        {
            get { return m_apartmentState; }
            set { m_apartmentState = value; }
        }

        public bool HasApartmentState
        {
            get { return m_apartmentState != ApartmentState.Unknown; }
        }

        public bool IsUpdate
        {
            get { return (HasBucketSize || HasDeploymentTimeout || HasScriptTimeout|| HasApartmentState); }
        }


        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(string.Format(CultureInfo.CurrentCulture, "Collection={0},", m_projectCollectionUri));
            builder.Append(string.Format(CultureInfo.CurrentCulture, "TeamProject={0},", m_teamProjectName));
            builder.Append(string.Format(CultureInfo.CurrentCulture, "SettingsName={0}", m_testSettingsName));

            if (HasBucketSize)
            {
                builder.Append(",");
                builder.Append(string.Format(CultureInfo.CurrentCulture, "BucketSize={0},", m_bucketSize));
            }

            if (HasDeploymentTimeout)
            {
                builder.Append(",");
                builder.Append(string.Format(CultureInfo.CurrentCulture, "DeploymentTimeout={0},", m_deploymentTimeout));
            }

            if (HasScriptTimeout)
            {
                builder.Append(",");
                builder.Append(string.Format(CultureInfo.CurrentCulture, "ScriptTimeout={0},", m_scriptTimeout));
            }

            if (HasApartmentState)
            {
                builder.Append(",");
                builder.Append(string.Format(CultureInfo.CurrentCulture, "ApartmentState={0},", m_apartmentState));
            }

            return builder.ToString();
        }
 
    }
}

Monday, 18 January 2016

Add to GAC (Global Assembly Cache) with Poweshell Remoting

Script made available here, by me allows the files to be added to GAC (Global Assembly Cache) in a remote machine, using poweshell (This script was created on a great sample script available in this article, which explaines how to copy files using PowerShell remoting). Script can run on Server A and It can copy required assemblies from Server A, to Server B and then add to GAC in Server B. This can be used in a standalone installer script created with poweshell and can be utilized in Release Management.

In order to run the script first you need to enable powershell remoting in Server B (target remote server). To do that run in Server B.

Enable-PSRemotingimage

Max size issue

When running the script you could run in to max file size default limit per file if your assembly is larger than 10MB.

WARNING: Command failed. Sending data to a remote command failed with the following error message: The current deserialized object size of the data received from remote client exceeded allowed maximum object size. The current deserialized object size is 1057
2800. Allowed maximum object size is 10485760. For more information, see the about_Remote_Troubleshooting Help topic.
image

As explained in this stackoverflow Q&A, run below script in remote target server (Server B), to enable larger files.

Register-PSSessionConfiguration -Name AsmNoLimits #or the name you like.image
Set the max file size to 500MB. this is run in Server B (Target remote server).
Set-PSSessionConfiguration -Name AsmNoLimits ` -MaximumReceivedDataSizePerCommandMB 500 -MaximumReceivedObjectSizeMB 500image
In the script this configuration “AsmNoLimits” used, when creating the remote PowerShell session.image

With this change large files (upto 500MB) can be copied .image

image

Adding to GAC

Below script segment allows adding to GAC in the remote computer (Server B).image

Once executed in Server A, file copied from Server A to B and getting added to GAC in Server B.

dir C:\temp\GAC\*.* | .\AddAssembliesToGac-Remote.ps1 -Computername "dolphintfsa.domainx.local" -Credential "domainx\chamindac" -Passthru -Verboseimage

image

You can download the script from technet gallery.

A sample script to call this script would look like below.image

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<#
need to execute below in remote computer(Admin PS Window) to enable PS Remote

 PS C:\Windows\system32> Enable-PSRemoting

 Then execute the below to allow file size more than 10MB
 
 PS C:\Windows\system32> Register-PSSessionConfiguration -Name AsmNoLimits #or the name you like.
 
 PS C:\Windows\system32> Set-PSSessionConfiguration -Name AsmNoLimits ` -MaximumReceivedDataSizePerCommandMB 500 -MaximumReceivedObjectSizeMB 500

 AsmNoLimits is used in AddAssembliesToGac-Remote.ps1

#>
$username = "yourdomain\yourdomainuser"
$password = ConvertTo-SecureString "yourPassword" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $username, $password

dir C:\temp\GAC\*.dll | .\AddAssembliesToGac-Remote.ps1 -Computername "YourRemoteMachine" -Credential $cred -Passthru -Verbose

Popular Posts