Wednesday, February 27, 2019

Running a .PS1 or .CMD file from Visual Studio Project Explorer

Today’s blog is one of those topics that I just needed to get written down somewhere, and I expect that a few readers may find this interesting and want to do the same thing. 

In many of the Visual Studio projects we have where I work, there are Powershell and/or batch CMD files that we use to launch builds of an SSIS package, create directories, build databases, etc. For example our files that build SSIS projects do multiple steps:

  • Build the project
  • Create the folder and variables from an SQL script
  • Deploy the Project to SSDB
  • Map the Parameters from an SQL Script
  • Create a Job To Run the Package, also using an SQL script

These steps are coded into a .PS1 file, running in order and failing the script if there is an issue in any of the steps/scripts. Previously they were a part of a .CMD file, but error handling was not included in the first versions because it was more trouble than it was worth (but now that we are trying to implement continuous integration, we need the process to fail when it is run by an automated process instead of a person eyeballing the script.)

If you are using Visual Studio 2017, you can get to the directory of the solution by right clicking the solution, and choosing Open Folder in File Explorer which will get you close to the files, but you still need to navigate and find the file:

This was not in Visual Studio 2015, which is what we had been using until recently.

But I want to just run the file right from the Miscellaneous folder. For example the _DEV files in this directory will build/deploy the project in our DEV environment:

By default, if you double click the file, it will take you to edit the file in a Visual Studio editor, which is actually what is desired. I don’t want to accidentally kick off a release to DEV, much less to our PROD servers. Normally if I am clicking on a file, and making changes. But when I want to test the file, it is easiest if it can be done right from here. If you right click a .PS1 file, you can open the file in the Powershell ISE (Integrated Scripting Experience… I looked it up just for this blog!). This works, but my goal is to just execute the file.

Another way to do this is to set up an “Open With…” action. For PowerShell if you right click the file, we will set up an option to run the file directly. After you click “Open With…”, click the Add button. Then fill in the blanks in the following form:

I included -noexit because I always want the file to not close the window when the file completes, so I can look over the output easily. Note that you cannot make changes to the Add Program setup if you get it wrong, but you can delete it and re-create it if it doesn’t work as you expect. Now, when you right click the file, and choose “Open With…” and choose the item we just added:

Then just choose that item and the PowerShell window will appear:

For .CMD files, which does not have an integrated scripting environment to test your code, instead of powershell.exe, use “cmd.exe” and “%1”, thusly:

Now, right click, chose “Open With…” and pick “Execute .CMD File “ and the .cmd file will start. Note that if you want it not to automatically end, you will need to include a PAUSE command.

 

The post Running a .PS1 or .CMD file from Visual Studio Project Explorer appeared first on Simple Talk.



from Simple Talk https://ift.tt/2BXk0gG
via

No comments:

Post a Comment