This is going to be a quick post that shows the use of the PowerShell task in a Pipeline. Nothing in the post is really specific to the Azure DevOps Project we have been using over the last few weeks, but just in case you’re totally new to Azure DevOps and/or this series you can use the following posts to get started.
Getting Started with Azure DevOps
Pipeline Creation in Azure DevOps
Azure DevOps Publish Artifacts for ASP.NET Core
Azure DevOps Pipelines: Multiple Jobs in YAML
Azure DevOps Pipelines: Reusable YAML
Azure DevOps Pipelines: Use YAML Across Repos
Azure DevOps Pipelines: Conditionals in YAML
Azure DevOps Pipelines: Naming and Tagging
Azure DevOps Pipelines: Manual Tagging
Azure DevOps Pipelines: Depends On with Conditionals in YAML
PowerShell Task
The PowerShell task will allow you to do pretty much anything. If there isn’t an existing DevOps task that fits your needs more than likely you can find a way to use the PowerShell task to accomplish what you need within the context of the computer the task is running on and even external computers that depending on your networking and security setup. The following is a sample task I added to a Pipeline that will output all the environment variables to the logs. This is an inline script, but you can also run scripts from files. Also, note that this works on both Windows and Linux agents.
- task: PowerShell@2 inputs: targetType: 'inline' script: 'Get-ChildItem -Path Env:\'
While this script isn’t super useful for a production Pipeline I often use it when setting up a Pipeline to get a good feel for what is available variable wise. Also, keep in mind that depending on the trigger of the run these variables can be different. For example, if a run was triggered by a pull request you will have a number of pull request related variables. The following is the output of this command on my test project that was triggered via a pull request and therefore contains a bunch of SYSTEM_PULLREQUEST_x variables with information about the pull request. The agent was running Linux.
Name Value ---- ----- AGENT_ACCEPTTEEEULA True AGENT_BUILDDIRECTORY /home/vsts/work/1 AGENT_DISABLELOGPLUGIN_TESTFI… true AGENT_DISABLELOGPLUGIN_TESTRE… true AGENT_HOMEDIRECTORY /home/vsts/agents/2.165.2 AGENT_ID 9 AGENT_JOBNAME Build WebApp1 AGENT_JOBSTATUS Succeeded AGENT_MACHINENAME fv-az563 AGENT_NAME Hosted Agent AGENT_OS Linux AGENT_OSARCHITECTURE X64 AGENT_READONLYVARIABLES true AGENT_RETAINDEFAULTENCODING false AGENT_ROOTDIRECTORY /home/vsts/work AGENT_TEMPDIRECTORY /home/vsts/work/_temp AGENT_TOOLSDIRECTORY /opt/hostedtoolcache AGENT_VERSION 2.165.2 AGENT_WORKFOLDER /home/vsts/work agent.jobstatus Succeeded ANDROID_HOME /usr/local/lib/android/sdk ANDROID_SDK_ROOT /usr/local/lib/android/sdk ANT_HOME /usr/share/ant AZURE_EXTENSION_DIR /opt/az/azcliextensions AZURE_HTTP_USER_AGENT VSTS_08ccc6b2-4e5e-4621-8f5b-3fe0de2efa22_build… BOOST_ROOT_1_69_0 /usr/local/share/boost/1.69.0 BOOST_ROOT_1_72_0 /usr/local/share/boost/1.72.0 BUILD_ARTIFACTSTAGINGDIRECTORY /home/vsts/work/1/a BUILD_BINARIESDIRECTORY /home/vsts/work/1/b BUILD_BUILDID 73 BUILD_BUILDNUMBER merge_20200422.1 BUILD_BUILDURI vstfs:///Build/Build/73 BUILD_CONTAINERID 3453972 BUILD_DEFINITIONNAME Playground BUILD_DEFINITIONVERSION 4 BUILD_QUEUEDBY Microsoft.VisualStudio.Services.TFS BUILD_QUEUEDBYID 00000002-0000-8888-8000-000000000000 BUILD_REASON PullRequest BUILD_REPOSITORY_CLEAN False BUILD_REPOSITORY_GIT_SUBMODUL… False BUILD_REPOSITORY_ID ff7a6325-1129-42e3-b095-6a39ef6a6bd3 BUILD_REPOSITORY_LOCALPATH /home/vsts/work/1/s BUILD_REPOSITORY_NAME Playground BUILD_REPOSITORY_PROVIDER TfsGit BUILD_REPOSITORY_URI https://[email protected]/ericlanders… BUILD_REQUESTEDFOR Eric Anderson BUILD_REQUESTEDFOREMAIL [email protected] BUILD_REQUESTEDFORID 45247cb1-8f49-4c03-a4c5-b03ac3286c99 BUILD_SOURCEBRANCH refs/pull/10/merge BUILD_SOURCEBRANCHNAME merge BUILD_SOURCESDIRECTORY /home/vsts/work/1/s BUILD_SOURCEVERSION 3e2b77c27f31a4c729a5f195b49d2e108500399d BUILD_SOURCEVERSIONAUTHOR Eric Anderson BUILD_SOURCEVERSIONMESSAGE Merge pull request 10 from docChanges into mast… BUILD_STAGINGDIRECTORY /home/vsts/work/1/a BUILDCONFIGURATION Release BUILDWEBAPP2 false CHROME_BIN /usr/bin/google-chrome CHROMEWEBDRIVER /usr/local/share/chrome_driver COMMON_TESTRESULTSDIRECTORY /home/vsts/work/1/TestResults CONDA /usr/share/miniconda DEBIAN_FRONTEND noninteractive DOTNET_SKIP_FIRST_TIME_EXPERI… 1 ENDPOINT_URL_SYSTEMVSSCONNECT… https://dev.azure.com/ericlanderson/ GECKOWEBDRIVER /usr/local/share/gecko_driver GIT_TERMINAL_PROMPT 0 GOROOT /usr/local/go1.14 GOROOT_1_11_X64 /usr/local/go1.11 GOROOT_1_12_X64 /usr/local/go1.12 GOROOT_1_13_X64 /usr/local/go1.13 GOROOT_1_14_X64 /usr/local/go1.14 GRADLE_HOME /usr/share/gradle HOME /home/vsts ImageOS ubuntu18 ImageVersion 20200406.2 INPUT_ARGUMENTS INVOCATION_ID 3e6abb812a484ab39fadc9e8721258ee JAVA_HOME /usr/lib/jvm/zulu-8-azure-amd64 JAVA_HOME_11_X64 /usr/lib/jvm/zulu-11-azure-amd64 JAVA_HOME_12_X64 /usr/lib/jvm/zulu-12-azure-amd64 JAVA_HOME_7_X64 /usr/lib/jvm/zulu-7-azure-amd64 JAVA_HOME_8_X64 /usr/lib/jvm/zulu-8-azure-amd64 JOURNAL_STREAM 9:30085 LANG C.UTF-8 LEIN_HOME /usr/local/lib/lein LEIN_JAR /usr/local/lib/lein/self-installs/leiningen-2.9… M2_HOME /usr/share/apache-maven-3.6.3 MSDEPLOY_HTTP_USER_AGENT VSTS_08ccc6b2-4e5e-4621-8f5b-3fe0de2efa22_build… PATH /opt/microsoft/powershell/7:/usr/share/rust/.ca… PIPELINE_WORKSPACE /home/vsts/work/1 POWERSHELL_DISTRIBUTION_CHANN… Azure-DevOps-ubuntu18 PSModulePath /home/vsts/.local/share/powershell/Modules:/usr… RUNNER_TOOLSDIRECTORY /opt/hostedtoolcache SELENIUM_JAR_PATH /usr/share/java/selenium-server-standalone.jar SWIFT_PATH /usr/share/swift/usr/bin SYSTEM build SYSTEM_ARTIFACTSDIRECTORY /home/vsts/work/1/a SYSTEM_COLLECTIONID 08ccc6b2-4e5e-4621-8f5b-3fe0de2efa22 SYSTEM_COLLECTIONURI https://dev.azure.com/ericlanderson/ SYSTEM_CULTURE en-US SYSTEM_DEFAULTWORKINGDIRECTORY /home/vsts/work/1/s SYSTEM_DEFINITIONID 5 SYSTEM_DEFINITIONNAME Playground SYSTEM_ENABLEACCESSTOKEN SecretVariable SYSTEM_HOSTTYPE build SYSTEM_ISSCHEDULED False SYSTEM_JOBATTEMPT 1 SYSTEM_JOBDISPLAYNAME Build WebApp1 SYSTEM_JOBID 98395c9e-7365-5c3f-03de-ec42b09a8a98 SYSTEM_JOBIDENTIFIER WebApp1.__default SYSTEM_JOBNAME __default SYSTEM_JOBPARALLELISMTAG Private SYSTEM_JOBPOSITIONINPHASE 1 SYSTEM_PHASEATTEMPT 1 SYSTEM_PHASEDISPLAYNAME Build WebApp1 SYSTEM_PHASEID a142d6c6-ff80-5cff-8292-5044e2c5b0ef SYSTEM_PHASENAME WebApp1 SYSTEM_PIPELINESTARTTIME 2020-04-22 06:11:44-05:00 SYSTEM_PLANID 726fda14-a3a2-45b1-b745-bef8cf17bdaa SYSTEM_PULLREQUEST_ISFORK False SYSTEM_PULLREQUEST_PULLREQUES… 10 SYSTEM_PULLREQUEST_PULLREQUES… 1 SYSTEM_PULLREQUEST_SOURCEBRAN… refs/heads/docChanges SYSTEM_PULLREQUEST_SOURCECOMM… ba11cb768bc75ae65ff6b7ac6afb8a2950063f07 SYSTEM_PULLREQUEST_SOURCEREPO… https://[email protected]/ericlanders… SYSTEM_PULLREQUEST_TARGETBRAN… refs/heads/master SYSTEM_SERVERTYPE Hosted SYSTEM_STAGEATTEMPT 1 SYSTEM_STAGEDISPLAYNAME __default SYSTEM_STAGEID 96ac2280-8cb4-5df5-99de-dd2da759617d SYSTEM_STAGENAME __default SYSTEM_TASKDEFINITIONSURI https://dev.azure.com/ericlanderson/ SYSTEM_TASKDISPLAYNAME PowerShell SYSTEM_TASKINSTANCEID 6417fa85-e8cf-55f9-817e-d698bd79d6f7 SYSTEM_TASKINSTANCENAME PowerShell SYSTEM_TEAMFOUNDATIONCOLLECTI… https://dev.azure.com/ericlanderson/ SYSTEM_TEAMFOUNDATIONSERVERURI https://dev.azure.com/ericlanderson/ SYSTEM_TEAMPROJECT Playground SYSTEM_TEAMPROJECTID 7550ca2f-9ffe-45b7-abd5-c4e92a4a5f4e SYSTEM_TIMELINEID 726fda14-a3a2-45b1-b745-bef8cf17bdaa SYSTEM_TOTALJOBSINPHASE 1 SYSTEM_WORKFOLDER /home/vsts/work TASK_DISPLAYNAME PowerShell TF_BUILD True USER vsts VCPKG_INSTALLATION_ROOT /usr/local/share/vcpkg VSTS_AGENT_PERFLOG /home/vsts/perflog VSTS_PROCESS_LOOKUP_ID vsts_54420f58-c41f-4a43-8ce8-bbbac5023620
I don’t know about you but being able to see what paths the built-in path variables actually map to helps me a lot especially when files need to be moved around.
Wrapping Up
As stated above you can do just about anything with the PowerShell task. I have used it for everything from reading a JSON file to building a VM for QA. If you hadn’t used this task before I hope this post helped you get started and opened your eyes to the huge range of things you can do with the PowerShell task.
Also published on Medium.