Visual Studio Script



PowerShell in Visual Studio Code. PowerShell is a task-based command-line shell and scripting language built on.NET, which provides a powerful toolset for administrators on any platform. The Microsoft PowerShell extension for Visual Studio Code provides rich language support and capabilities such as completions, definition tracking, and linting analysis for PowerShell versions 3, 4, 5, and 5. Python Tools for Visual Studio is a completely free extension, developed and supported by Microsoft with contributions from the community. Visit our Github page to see or participate in PTVS development.

-->

Note

Build scripts allow you to customize your builds; but issues you find during that process are beyond the scope of App Center support. If one of your scripts is failing, the whole build will report a failure, even if the primary build up to that point were successful.

You can add custom build scripts that run at pre-defined stages during build time: post-clone, pre-build, post-build. Place the scripts with the format specified below next to the project-level (.xcodeproj, .csproj, .sln, or package.json) file or module-level (build.gradle) file that you've selected in the build configuration and we'll run them as custom build steps.

In App Center, non-UWP apps are built on macOS, so they can use Bash scripts. UWP apps are built on Windows, so they can use PowerShell scripts. Many similar tasks can be done in either script format, but you might need to do some conversion if migrating between them. For example, macOS Bash requires the use of UNIX-style line endings (LF).

You can find a collection of build script examples on the dedicated public GitHub repository. Feel free to use the sample scripts, modify them, or submit new pull requests with your most useful scripts for the rest of the community to use.

Warning

When App Center detects build scripts for the first time, or you change the location of scripts, or you change where CocoaPods are stored; you must apply the changes by clicking the Save or Save & Build button in the build configuration. When you do this, App Center runs an analysis to index your repository tree and updates the build definition.

Post-clone

The post-clone script runs immediately after the repository was cloned but before we do anything else on our end.

To run scripts post-clone, add the following file next to the project file in your repository:

  • appcenter-post-clone.sh (Bash for iOS & Android)

  • appcenter-post-clone.ps1 (PowerShell for UWP)

Pre-build

The pre-build script runs before the actual build starts. For React Native apps, the script runs before generating source maps. For other platforms, it runs after we've installed dependencies, for example from NuGet, CocoaPods, or Carthage.

To run scripts pre-build, add the following file next to the project file in your repository:

  • appcenter-pre-build.sh (Bash for iOS & Android)

  • appcenter-pre-build.ps1 (PowerShell for UWP)

Post-build

Build

The post-build script runs after the build has finished and copied all the necessary artifacts to the output directory. The post-build script will run even if the build fails.

To run scripts post-build, add the following file next to the project file in the repository:

Script
  • appcenter-post-build.sh (Bash for iOS & Android)

  • appcenter-post-build.ps1 (PowerShell for UWP)

Visual Studio Scripts

Environment Variables

Visual Studio Script

You can use pre-defined, custom, or in-script environment variables to help write your Build scripts, see our Environment Variables guide for more info.

Customizing Visual Studio for PowerShell users

Why?

I find this a very useful customization to Visual Studio to run project specific tasks from PowerShell while developing. For example I have PowerShell .ps1 scripts in a Tasks or Deployment solution folders that do things like:

  • Refresh nuget packages because I use nuget without committing packages (Update: Nuget.org have finally fixed this!)
  • Seeding my Database/ RavenDb Document stores
  • Testing my Build/ Automated test scripts outside of CI
ScriptBuild script visual studio

It’s nice to be able to right-click them (or use a shortcut key) to execute them in place just like this:

So on to how to set it up:

##

Step 1: Adding “run powershell script” as an external tool

  1. In Visual Studio go to the menu: ToolsExternal Tools
  2. Click the “Add” button
  3. Add the following form values:
    • Title: “Run Powershell script in output window”
    • Command: “C:windowssystem32windowspowershellv1.0powershell.exe”
    • Arguments: “ -file “$(ItemPath)”
    • Initial Directory: “$(ItemDir)”
    • Tick “Use Output window”
    • (Close on exit will now be automatically on)
  4. Click the “Apply” button
  5. Click the “Add” button
  6. Add the following form values:
    • Title: “Run powershell script outside of VS”
    • Command: “C:windowssystem32windowspowershellv1.0powershell.exe”
    • Arguments: “ -file “$(ItemPath)”
    • Initial Directory: “$(ItemDir)”
    • Don’t tick “Use Output window”
    • Tick “Close on exit”
  7. Click the “Ok” button

They should look something like this:

Step 2: Weird Step, trust me!

Check the index position it is in the external tools list. By default mine are at positions 6 and 7. (I think by default Create GUID is no. 1!)

Step 3: Hook it up to the context menus

  1. Go to the menu: ToolsCustomizeCommands
  2. Click the “Context menu” radio option
  3. Scroll down to “Project and Solution Context MenusItem” (nightmare long menu, type “Proj” to get roughly to the right place)
  4. Click the “Add Command” button
  5. Select the category: “Tools” and Command: “External Command 7” (or whatever your position is you got from the “Weird Step 2”)
  6. Hit the “Ok” button
  7. Then to set up the 2nd command:
  8. Select Category: “Tools” and Command: “External Command 8” (or whatever your position is for the other one)
  9. Hit the “Ok” button again
  10. Move them around till you are happy with their order (I usually put them somewhere below “Open With…”)

Step 4: Add your keyboard shortcuts

  1. Go to the menu: ToolsOptions
  2. Select the EnvironmentKeyboard section
  3. Find the Tools.ExternalCommandN item in the list (nightmare long list again, type “Tools” to get you roughly there again)
  4. Select your shortcut key for each command: I like <CTRL> <SHIFT> <P> and <CTRL> <SHIFT> <ALT> <P> respectively

You are all done, enjoy your PowerShell efficiency!

##

Visual Studio Script Error

What kind of PowerShell scripts do you find useful to run straight from Visual Studio?

Build Script Visual Studio

Sorry, we run comments powered by Disqus which require JavaScript. Alternatively you could submit it to hacker news to comment on it?