简体   繁体   中英

Azure Function with PowerShell, Storage Queue trigger, and OneDrive for Business

I'm trying to create an Azure Function that executes PowerShell with a Storage Queue trigger. For testing purposes, I want this function to manipulate a file in my OneDrive for Business account. To copy the file at aapdftoimage/ThreePages.pdf to aapdftoimage/output_ThreePages.pdf .

When OneDrive for Business is integrated as an Input, I get errors any time the function is triggered by a new message in the queue. If I disconnect OneDrive as input I don't get any errors and $triggerInput contains the message.

The errors are:

2017-05-25T22:24:38.484 Function started (Id=a0c37fdf-ed3c-473c-9c79-236d63531e7e)
2017-05-25T22:24:38.499 Function completed (Failure, Id=a0c37fdf-ed3c-473c-9c79-236d63531e7e, Duration=1ms)
2017-05-25T22:24:38.562 Exception while executing function: Functions.QueueTriggerPowerShell1. Microsoft.Azure.WebJobs.Host: No value for named parameter 'file'.

Here's my PowerShell:

$inData = Get-Content $triggerInput
$inFile = Get-Content $inputFile

Write-Output "PowerShell script processed queue message '$inData'"
Write-Output "inFile: $inFile"

Here's function.json:

{
  "bindings": [
    {
      "name": "triggerInput",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "samples-powershell-pdftoimage",
      "connection": "<storageaccount>_STORAGE"
    },
    {
      "type": "apiHubFile",
      "name": "inputFile",
      "path": "aapdftoimage/{file}",
      "connection": "onedriveforbusiness1_ONEDRIVEFORBUSINESS",
      "direction": "in"
    }
  ],
  "disabled": false
}

As I'm writing this, I think part of my confusion is over the Input and Output (not connected in my test) integrations of OneDrive for Business.

I know what $triggerInput is. It's the content of the message. But what is $inputFile ? And where does {file} come from?

I thought maybe I would do the following but it too doesn't work (same errors):

$file = Get-Content $triggerInput

I thought this might define $inputFile as "aapdftoimage/$file" but it does nothing of the sort.

Needless to say, I'm at a standstill. Can anyone give me some guidance and straighten me out?

@Henry Hamid Safi is correct. Using C#, you can leverage the Binder object to dynamically name the file.

In your use-case, the only way to dynamically provide the name of the file is to pass it as a JSON object in your trigger payload. Here is a sample setup that worked for me.

function.json:

{
  "bindings": [
    {
      "name": "triggerInput",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "samples-powershell",
      "connection": "AzureWebJobsStorage"
    },
    {
      "type": "apiHubFile",
      "name": "inputFile",
      "path": "aapdftoimage/{file}",
      "connection": "onedriveforbusiness_ONEDRIVEFORBUSINESS",
      "direction": "in"
    },
    {
      "type": "apiHubFile",
      "name": "outputFile",
      "path": "aapdftoimage/output_{file}",
      "connection": "onedriveforbusiness_ONEDRIVEFORBUSINESS",
      "direction": "out"
    }
  ],
  "disabled": false
}

run.ps1:

$in = Get-Content $triggerInput
Write-Output "PowerShell script processed queue message '$in'"
Copy-Item $inputFile $outputFile

Request body (if using Test pane in Portal) or Queue trigger payload:

{
  "file":"ThreePages.pdf"
}

Log entries:

2017-05-26T22:27:53.984 Function started (Id=032c4469-8378-44ce-af9e-5a941afb0d82)
2017-05-26T22:27:54.875 PowerShell script processed queue message '{   "file":"ThreePages.pdf" }'
2017-05-26T22:27:54.891 Function completed (Success, Id=032c4469-8378-44ce-af9e-5a941afb0d82, Duration=899ms)

OneDrive folder screen shot: 在此处输入图片说明

Working example

Function.json:

{
  "bindings": [
    {
      "name": "triggerInput",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "test",
      "connection": "AzureWebJobsDashboard"
    },
    {
      "type": "apiHubFile",
      "name": "inputFile",
      "path": "aapdftoimage/ThreePages.pdf",
      "connection": "onedrive_ONEDRIVE",
      "direction": "in"
    },
    {
      "type": "apiHubFile",
      "name": "outputFile",
      "path": "aapdftoimage/output_ThreePages.pdf",
      "connection": "onedrive_ONEDRIVE",
      "direction": "out"
    }
  ],
  "disabled": false
}

run.ps1:

$in = Get-Content $triggerInput
Write-Output "PowerShell script processed queue message '$in'"

Copy-Item $inputFile $outputFile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM