Create your first PowerShell function in Azure

This quickstart article walks you through how to create your first serverless PowerShell function using Visual Studio Code.

Azure Functions code in a Visual Studio Code project

You use the Azure Functions extension for Visual Studio Code to create a PowerShell function locally and then deployed it to a new function app in Azure. The extension is currently in preview. To learn more, see the Azure Functions extension for Visual Studio Code extension page.

The following steps are supported on macOS, Windows, and Linux-based operating systems.

Prerequisites

To complete this quickstart:

If you don't have an Azure subscription, create a free account before you begin.

Install the Azure Functions extension

You can use the Azure Functions extension to create and test functions and deploy them to Azure.

  1. In Visual Studio Code, open Extensions and search for azure functions, or select this link in Visual Studio Code.

  2. Select Install to install the extension for Visual Studio Code:

    Install the extension for Azure Functions

  3. After installation, select the Azure icon on the Activity bar. You should see an Azure Functions area in the Side Bar.

    Azure Functions area in the Side Bar

Create a function app project

The Azure Functions project template in Visual Studio Code creates a project that can be published to a function app in Azure. A function app lets you group functions as a logical unit for easier management, deployment, scaling, and sharing of resources.

  1. In Visual Studio Code, select the Azure logo to display the Azure: Functions area, and then select the Create New Project icon.

    Create a function app project

  2. Choose a location for your Functions project workspace and choose Select.

    Note

    This article was designed to be completed outside of a workspace. In this case, do not select a project folder that is part of a workspace.

  3. Select the Powershell as the language for your function app project and then Azure Functions v2.

  4. Choose HTTP Trigger as the template for your first function, use HTTPTrigger as the function name, and choose an authorization level of Function.

    Note

    The Function authorization level requires a function key value when calling the function endpoint in Azure. This makes it harder for just anyone to call your function.

  5. When prompted, choose Add to workspace.

Visual Studio Code creates the PowerShell function app project in a new workspace. This project contains the host.json and local.settings.json configuration files, which apply to all function in the project. This PowerShell project is the same as a function app running in Azure.

Run the function locally

Azure Functions Core Tools integrates with Visual Studio Code to let you run and debug an Azure Functions project locally.

  1. To debug your function, insert a call to the Wait-Debugger cmdlet in the function code before you want to attach the debugger, then press F5 to start the function app project and attach the debugger. Output from Core Tools is displayed in the Terminal panel.

  2. In the Terminal panel, copy the URL endpoint of your HTTP-triggered function.

    Azure local output

  3. Append the query string ?name=<yourname> to this URL, and then use Invoke-RestMethod in a second PowerShell command prompt to execute the request, as follows:

    PS > Invoke-RestMethod -Method Get -Uri http://localhost:7071/api/HttpTrigger?name=PowerShell
    Hello PowerShell
    

    You can also execute the GET request from a browser from the following URL:

    http://localhost:7071/api/HttpExample?name=PowerShell

    When you call the HttpTrigger endpoint without passing a name parameter either as a query parameter or in the body, the function returns a BadRequest error. When you review the code in run.ps1, you see that this error occurs by design.

  4. Information about the request is shown in Terminal panel.

    Function execution in Terminal panel

  5. To stop debugging, press Ctrl + C to stop Core Tools.

After you've verified that the function runs correctly on your local computer, it's time to publish the project to Azure.

Note

Remember to remove any calls to Wait-Debugger before you publish your functions to Azure.

Publish the project to Azure

In this section, you create a function app and related resources in your Azure subscription and then deploy your code.

  1. Choose the Azure icon in the Activity bar, then in the Azure: Functions area, choose the Deploy to function app... button.

    Publish your project to Azure

  2. Provide the following information at the prompts:

    • Select subscription: Choose the subscription to use. You won't see this if you only have one subscription.

    • Select Function App in Azure: Choose + Create new Function App (not Advanced). This article doesn't support the advanced publishing flow.

    Important

    Publishing to an existing function app overwrites the content of that app in Azure.

    • Enter a globally unique name for the function app: Type a name that is valid in a URL path. The name you type is validated to make sure that it's unique in Azure Functions.
    • Select a runtime: Choose the version of Python you've been running on locally. You can use the python --version command to check your version.
    • Select a runtime: Choose the version of Node.js you've been running on locally. You can use the node --version command to check your version.
    • Select a location for new resources: For better performance, choose a region near you.
  3. When completed, the following Azure resources are created in your subscription:

    • Resource group: Contains all of the created Azure resources. The name is based on your function app name.
    • Storage account: A standard Storage account is created with a unique name that is based on your function app name.
    • Hosting plan: A consumption plan is created in the West US region to host your serverless function app.
    • Function app: Your project is deployed to and runs in this new function app.
    • Application Insights: An instance, which is connected to your function app, is created based on your function name.

    A notification is displayed after your function app is created and the deployment package is applied.

  4. Select View Output in this notification to view the creation and deployment results, including the Azure resources that you created. If you miss the notification, select the bell icon in the lower right corner to see it again.

    Create complete notification

Run the function in Azure

To verify that your published function runs in Azure, execute the following PowerShell command, replacing the Uri parameter with the URL of the HTTPTrigger function from the previous step. As before, append the query string &name=<yourname> to the URL, as in the following example:

PS > Invoke-WebRequest -Method Get -Uri "https://glengatest-vscode-powershell.azurewebsites.net/api/HttpTrigger?code=nrY05eZutfPqLo0som...&name=PowerShell"

StatusCode        : 200
StatusDescription : OK
Content           : Hello PowerShell
RawContent        : HTTP/1.1 200 OK
                    Content-Length: 16
                    Content-Type: text/plain; charset=utf-8
                    Date: Thu, 25 Apr 2019 16:01:22 GMT

                    Hello PowerShell
Forms             : {}
Headers           : {[Content-Length, 16], [Content-Type, text/plain; charset=utf-8], [Date, Thu, 25 Apr 2019 16:01:22 GMT]}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 16

Next steps

You have used Visual Studio Code to create a PowerShell function app with a simple HTTP-triggered function. You may also want to learn more about debugging a PowerShell function locally using the Azure Functions Core Tools. Check out the Azure Functions PowerShell developer guide.