Using the Inline PowerShell VSTS task

Run PowerShell in a VSTS pipeline from a textbox

Advertisements

The Inline PowerShell VSTS task enables you to execute PowerShell from a textbox within your build or release pipeline. You can run a PowerShell script on you agent or on Azure. The task can be installed from the Marketplace.

How to use the task

First add the task to you build or release pipeline. Select the +Add tasks -> Utility -> Run Inline PowerShell:

AddTask

Next you can enter the PowerShell script into the textbox:

EnterPowerShellRun your build or release to see the ‘Hello world’ in the logs.

Adding arguments

Next you will be able to add argument to pass custom build variables. You need to pass them as arguments if they are marked as secret.

EnterPowerShellArguments

This will give you the following output:

EnterPowerShellOutput

Variables marked a secret will be shown as ********. This to prevent them to be reviled to everybody with access to the logs.

Feedback

If you want to see the sources or contribute, go to GitHub. And if you like the task, please leave a review at the marketplace.

18 thoughts on “Using the Inline PowerShell VSTS task”

  1. I am trying to use this task to trigger a WebJob as part of a build (using Invoke-RestMethod), however I always get the error “No default subscription has been designated. Use Select-AzureSubscription -Default to set the default subscription.”. I would have expected the task to have already selected the subscription based on the fact it is required as part of the configuration. Any ideas ?

    Like

    1. @Dave what method do you use to login to azure? At logon it tries to select a subscription. Check if you have a default subscription for your Azure account. In the configuration of the task you do select the way you want to connect to Azure, not which subscription you connect to.

      Like

      1. @Peter I use the “Azure Resource Manager” connection type and then select the appropriate subscription for the “Azure RM Subscription” option. I can see in the logs the following line, which i’m assuming is your task setting things up..

        ##[command]Add-AzureRMAccount -ServicePrincipal -Tenant ******** -Credential System.Management.Automation.PSCredential
        2017-01-13T14:09:38.4854633Z ##[command]Select-AzureRMSubscription -SubscriptionId xxxxx-xxx-xxx-xxx-xxxxxx -TenantId ********

        Like

      2. @Peter

        Hi Peter, I had to remove my call to Get-Website, i just could get it to work.

        The reason I needed it was to extract the .PublishingUsername and .PublishingPassword properties so I could build the auth header for the Invoke-RestMethod. Instead I now just pass these as arguments to the script and it works – triggers my WebJob which in turn runs an executable on the remote Web App which has just been built and deployed.

        Great stuff.

        Like

  2. Hi Peter,

    I’m faced with the problem of wanting to forward a Team Build release description to iTunes Connect. The way this is done using the App Store Release task is by way of a file, and hence I found your extension task that could help me transform the $(Release.ReleaseDescription) variable into a text file on the file system. The idea is that I could then use this file as input to the App Store Release task, in order to pass on “what to test?”.

    Unfortunately, however, although PowerShell seems capable of escaping quotation marks passed in variables on the command line (i.e. first define the variable with quotation marks in it, then execute a script using this variable as a parameter), it does not seem to do so for your task. Hence, since I escape my $(Release.ReleaseDescription) by enclosing it in double quotes, using double quotes in the latter value breaks the script and – therefore – the release.

    Is there anything I can do to correct or prevent this problem from occurring? It would be really nice to have this functionality, as we would then have a single point of reference of what would have been passed on to iTunes Connect.

    Thanks in advance!

    Like

      1. Hi Peter,

        The script itself is rather straight-forward, as below:

        — begin —
        [CmdletBinding()]

        param(
        [Parameter(Mandatory=$True, Position=1)]
        [string]$file,
        [Parameter(Mandatory=$True, Position=2)]
        [string]$content
        )

        Write-Verbose -Verbose “Writing text file $file.”;
        Write-Verbose -Debug $MyInvocation.MyCommand.Path;

        Set-Content -Path $file -Value $content;
        — end —

        The parameters I’m passing along are as follows:
        WhatToTest.txt “$(Release.ReleaseDescription)”

        Like

      2. Ah, and just for sake of being complete, I’m testing with $(Release.ReleaseDescription) = ‘Een “voorbeeld” omschrijving.’ (single quotes excluded).

        Like

  3. Hi,
    I was using this task as part of my release definition to stop website in IIS with the following script:
    Invoke-Command -ComputerName TargetServer -ScriptBlock {Import-Module WebAdministration; Stop-Website -Name “Websitename”}
    And I am getting error stating “Connecting to remote server “xyz” failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting”.
    Made sure that PSRemoting is enabled and also the WsMan is running.
    However, when I run the same script directly from Powershell, it works.
    Any thoughts please.

    Like

  4. I had created a PSCredential with script to manage web server remotely

    $UserID = “xyz”
    $PswdFile = “\\Server1\Sharedpath\Pswd.txt”
    $KeyFile = “\\Server1\Sharedpath\AES.txt”
    $Key = New-Object byte[] 16
    [System.Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
    $Key | Out-File $KeyFile
    $Key = Get-Content $KeyFile
    $Pswd = “password” | ConvertTo-SecureString -AsPlainText -Force |
    ConvertFrom-SecureString -Key $Key | Out-File $PswdFile
    $TFSAdminCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserId, (Get-Content $PswdFile | ConvertTo-SecureString -Key $Key)
    $TFSAdminCred = Export-Clixml -Path “\\server\sharedpath\cred.txt”

    and to use it I simply ran
    $TFSAdminCred = Import-Clixml -Path “\\server\sharedpath\cred.txt”
    Invoke-Command -computername xyz.com -credential $TFSadminCred -scriptblock {import-module webadministration; stop-website -name “Websitename”}

    This works manually! but when i tried to use it with “Inline PowerShell Script”, got error!
    “##[error]Key not valid for use in specified state.”

    Like

  5. Is it possible to change the values of Build/Release variables in an Inline Azure Powershell Script to have that ‘new’ value available in a later Build/Release step?
    I am trying to retrieve the Key from my Application Insights instance so that I can inject it into my UI code at release time ( using Replace Token task).

    In the Microsoft provided “Azure Powershell” task, I can do this:
    ##vso[task.setvariable variable=OutputVar;]UpdatedValueInScript

    …but I cannot figure out how to do this with this Task.

    Do you have any recommendations?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s