简体   繁体   中英

Automating IISRESET via remote desktop connection after a TFS build

I want to automate a process, which is invoked after a successful build on TFS. The process will RDP to a test server, then call a C# application on that server, and reset IIS on that server. Each step will return the result so whether or not to call next step is based on the previous step.

There are a few obstacles in implementing it. Below is what I want to know if it is possible, and how to code it.

1) Invoking the process via a build on TFS

There is an option in Build definition to invoke automated test. I assume that the process can be invoked by implementing it as a test.

2) RDP to remote server

I found the links below, which might be a solution

Process rdcProcess = new Process();
rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe");
rdcProcess.StartInfo.Arguments = "/generic:TERMSRV/192.168.0.217 /user:" + "username" +  " /pass:" + "password";
rdcProcess.Start();

rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\mstsc.exe");
rdcProcess.StartInfo.Arguments = "/v " + "192.168.0.217"; // ip or name of computer to connect
rdcProcess.Start();

Run mstsc.exe with specified username and password

Automating remote desktop connection

3) IISReset

I think it should be simply invoke " IISRESET " after RDP, but the problem is that, HOW to captrue the result of running IISRESET.

The tools that might be suitable are:

1) Powershell - I don't know much about Powershell but am willing to learn if required

2) C#

My question is that how to implement it, any code example, and idea would be very much appreciated.

in most cases you do not need to run iisreset

if you want to upgrade an asp.net application, try to put app_offline.htm in the application folder, it will stop an application and application files will be unlocked

after upgrading an application, it will restart automatically, or you can "touch" web.config to force restart

You might be better using the Lab Build to run the scripts as part of an environment ob the target computer. It can run any powershell against that machine as well as deploy and execute applications....

Question: HOW to capture the result of running IISRESET

I believe the old fashioned way, Hope this is what you are looking for

c:> IISRESET >> C:\\temp.log

You can use the above either from CMD or powershell

In the past I have used Psexec to run commands against a remote server and where ever we need to control flow on the result of that command, we simply piped the console out to a shared folder and checked for our success flag.

I am not sure if TFS can run commands in this manner but we implemented it on hudson/jenkins.

This won't answer your question directly but it may offer a better way forward

An Example: psexec.exe \\remoteserver "iisreset > h:\\iisreset.log"

Then run a grep or similar against the iisreset.log with your success flag as a condition to run the next step.

Check my answer here which is somewhat related: Answer

If the user which runs the TFSBuild Service on the build server have enough rights on the test server then you can use psexec or powershell to run your commands remotely. Read the below links:

PSEXEC

PowerShell Remote commands

There is no inbuilt activity/process which can help you run scripts on remote machines in TFS build workflow.

Step 1 for you is to identify how you are going to run scripts on the remote machine, as mentioned above you can either use PSEXEC or Powershell (though running PowerShell on remote computers may be a little more complicated to set up).

Step2, write the actual scripts to do the work, stop services, install MSI etc.

Step3, Edit your current build defintion - create a new custom activity or make use of InvokeProcess activity from within your build definition to invoke the script that you have created in Step 2. InvokeProcess Activity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM