Saturday, 26 July 2014

Tool & Action to Run Custom PowerShell Scripts in Deployment Agent – VS 2013 Release Management

Can we run a custom PowerShell script in Release Management Deployment Agent?  One way is to create a PowerShell script and add it as a resource and create a tool and an action in Release Management Inventory. This is more suitable for adding a generic tool. But if we want to run a custom PowerShell script specific to a given release template, copied to deployment machine from the build drop, can we do that? Out of the box set of tools and actions in Release Management Server do not support this. We can do this by creating a generic tool and an action.
Let me show you how to do this step by step.
In Release Management Client go to Inventory tab Tools, click on New.
001

For the execution command type “powershell” and for the arguments type
-command __ScriptToExecute__  __Arguments__
This will add two parameters in the tool “ScriptToExecute” and “Arguments”. Provide a suitable Name and a Description as well.
002

Click on Save & Close to add the new tool.
003

Go to Actions and click on New to add new action.
004

Select the tool we have created and add a new Category for Custom Actions. Fill the Name and Description.
005

Save to create the new Action.
006
 007

Let’s test our Tool and Action in action. First we need a very simple PowerShell script like below.
008

In a test release template now we can see our custom action available.
009

Fill the script name with the path and arguments(for testing purpose script copied manually to deployment machine, this can be a script downloaded to deployment machine from the build drop using an XCOPY action in the release template).
010

Save the release template and click New Release to test. This example does not use a build out put, but the action can be used with a template bound to a build drop as well, as a matter of fact it can be used with any release template.
011 

Start the release by clicking on Start.
012

Script execution succeeded.
013

Click on View Log to see the output from script.
014

To fail a script based on a condition throw an exception like below and release action will fail.



14 comments:

Anonymous said...

Or you could use the "Run Command Line" action, which is built-in.

Chaminda Chandrasekara said...

Hi Anonymous,

Have you tried running PowerShell script with "Run Command Line". I do not believe it understands how to run PowerShell script as of now. I tried using it before creating this action. See the answer here http://social.msdn.microsoft.com/Forums/vstudio/en-US/57774061-0afe-4c6a-bbcd-da15044438a8/unable-to-deploy-sharepoint-solutions-on-release-management?forum=tfsbuild and refer this post http://jaspergilhuis.nl/2013/12/06/release-management-and-powershell/ run powershell script added as a resource in a tool and creating action (whcih is the other way of doing this as mentioned in the top of my post)

Christophe Frey said...

Hi Chaminda,

Thanks for your post.

Do you have an idea on how to make the action fail if something go wrong with my powershell ?

How can I make the script excecution unsuccesfull if there is a problem ?

Thanks

Chaminda Chandrasekara said...
This comment has been removed by the author.
Chaminda Chandrasekara said...

Hi Christophe,

I have updated the post to show how exception can be thrown to fail a script and release management action. My latest post explained more on handling exceptions silently, continue running if required and throw exceptions if any failures in script requires to stop the action and fail it. Tool to Execute Multiple SQL Scripts – VS 2013 Release Management – Part 1 Hope this helps you. If you need more info let me know..will try to help.

Madhukar Rao Gandra said...

Hi

I created the custom powershell as you documented and when I run the test script it works. But We have the powershell deployments and when trying to call them it get stuck in pending state and fails after time out period.

The actual command is C:\Temp\RM\Test\_Deploy\run.ps1 -parameters @{'environment' = 'DEV'}

but in the message in deployment log it says \"Powershell -command C:\Temp\RM\Test\Deploy\run.ps1 -parameters @{'environment' = 'DEV'}\" reached the configuration timeout.

See the difference in command it took out "_" from "_Deploy". Even if i change the filder to Deploy it still not working.

can you please enlighten what is wrong

Chaminda Chandrasekara said...
This comment has been removed by the author.
Chaminda Chandrasekara said...

Hi Madhukar,

IS it actually taking long time to run your script and a timeout? If so try to increase the action timeout and see.

Could you send me a screenshot of the release template where you are passing arguments to the action? and if you could you try to comment all of the script and print the arguments you pass inside run.ps1 ? If this is working successfully, we can say tool & action passing the arguments to your script correctly..then you can enable your scripts step by step to diagnose. If you are ok to send your run.ps1 to me I'll have a look.

Vijesh said...

We are trying to Launch a GUI Application (an EXE) after the deployment.

Options Tried:
1. We tried using "RUN COMMAND LINE" action in our Release Template to invoke a BAT file which internally calls the application EXE. Here we are not able to see the application is launching. But it looks like triggering the BAT file and the EXE process is running under DEPLOYER account.

2. We tried using "RUN COMMAND LINE AS USER" but we are getting an error as follows: "Run Command Line as User The installation command \"powershell -command ./RunCommandLine.ps1 -FilePath 'C:\Temp\Abc.bat' -Arguments '' -UserDomain 'XX' -UserName 'Abc' -UserPassword '*****'\" failed with the exit code \"1\". Failed"

3. I have edited the RunCommandLine.ps1 on the client machine , by updating the [System.Diagnostics.ProcessWindowStyle]to Normal and this works fine, when i manually run this on the machine.
Then Tried to invoke via custom powershell script (as per your blog) but no luck. Error says - "The specified executable is not a valid Win32 application"


Is there a way to launch the application in UI mode?

namita patil said...

Hi,

I am trying to run the Powershell script on a server. Getting the below errro:

The file \\$Server\Copy.ps1 is not digitally signed. The script will not ex
ecute on the system. Please see "get-help about_signing" for more details.

I tried running set-executionpolicy remotesigned.

But didnt helped.
Can anyone tell how this can be solved?

namita patil said...

Hi,

I am trying to run the Powershell script on a server. Getting the below errro:

The file \\$Server\Copy.ps1 is not digitally signed. The script will not ex
ecute on the system. Please see "get-help about_signing" for more details.

I tried running set-executionpolicy remotesigned.

But didnt helped.
Can anyone tell how this can be solved?

Jason Murdick said...

Many of your images on this post are broken. Looks like a very useful post as its what I am currently working on, but isn't usable with broken images.

Jason Murdick said...

Please delete this psot and my previous. Turns out its my corporate firewall blocking some images since they aren't hosted on the same site as others or this blog.

Abhishek said...

Hi Chaminda,

I have a problem similar to this post: http://stackoverflow.com/questions/32220131/vs-release-management-run-console-app-within-existing-user-session

I am trying to run Selenium cases by invoking a batch file, that will trigger the selenium cases.

But when I run the bat file directly in server, the browser opens, and test continues. But this does not happen when I invoke the bat file from the Release Manager (using the PowerShell : Script Executor / Run Command line components).

Please can you guide me here

Thanks
Abhishek