0
votes

How can one deploy a Powershell api to Azure Functions. The official documentation only talks about deploying a JavaScript function.

One doesn't get an option to select 'Powershell' during language selection while creating a 'Project' thru the Azure Functions extension.

This is a default Powershell api when deployed from the Azure Portal.

# POST method: $req
$requestBody = Get-Content $req -Raw | ConvertFrom-Json
$name = $requestBody.name
# GET method: each querystring parameter is its own variable
if ($req_query_name) 
  {$name = $req_query_name}
Out-File -Encoding Ascii -FilePath $res -inputObject "Hello $name"

I tried to deploy the below code directly using the VSC Azure Function's 'Deploy' button. But I get the below error.

Unable to write Workspace settings because no workspace is opened. Please open a workspace first and try again
1

1 Answers

1
votes

Unable to write Workspace settings because no workspace is opened. Please open a workspace first and try again.

In VSCode, we have to open a folder or create a function project first, or the file to hold Workspace settings can't be created, and we will get the error when trying to modify Workspace settings like runtime, language and template filter.

Detailed Steps of Developing Azure Function in VSCode(references from official tutorial):

Tips

  1. Languages like Powershell are experimental so don't use them in production. Powershell/Python support on 2.x runtime are also in progress.

  2. Install functions core tools(cli) to debug functions locally.

    1.x for OS Windows .NET Framework, 2.x for cross platform .NET Core.

Install Azure Function Extension

Input vscode:extension/ms-azuretools.vscode-azurefunctions in brower, it will open VScode and install extensions.

Create a Function project

  1. Create an empty folder in File explorer ; say MyFunctionApp

  2. Log into Azure Account.

  3. In Azure Function Extension, Click Create New Project and point to created folder MyFunctionApp.

  4. Select language JavaScript

Create a Function

  1. Click Create Function and browser MyFunctionApp folder. Then you may see the dropdown menu as below. Project runtime is automatically set based on cli installed.

    enter image description here

  2. Click Change template filter and change it to All so that we can see all templates available.

  3. Click Change project language to select experiment language like Powershell.

  4. Follow the rest prompt to create an Http trigger.

Debug or run function locally

For c#, javascript and java, feel free to F5.

To run function in experimental language locally, open terminal in VSCode(Ctrl+`) and run func host start, no debug support so don't use F5.

To test a httptrigger, suffix function url with ?name=World! to get Hello World response from your URL.

Deploy

Click Deploy to Function App button and follow the prompt.