简体   繁体   English

带有PowerShell,存储队列触发器和OneDrive for Business的Azure功能

[英]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. 我正在尝试创建一个Azure函数,该函数通过存储队列触发器执行PowerShell。 For testing purposes, I want this function to manipulate a file in my OneDrive for Business account. 出于测试目的,我希望此功能可以处理OneDrive for Business帐户中的文件。 To copy the file at aapdftoimage/ThreePages.pdf to aapdftoimage/output_ThreePages.pdf . 要将文件aapdftoimage/ThreePages.pdfaapdftoimage/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. 将OneDrive for Business集成为输入时,只要队列中的新消息触发该功能,我都会收到错误消息。 If I disconnect OneDrive as input I don't get any errors and $triggerInput contains the message. 如果我断开OneDrive作为输入的连接,则不会出现任何错误,并且$triggerInput包含该消息。

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: 这是我的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: 这是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. 在撰写本文时,我认为部分困惑在于OneDrive for Business的输入和输出(在测试中未连接)集成。

I know what $triggerInput is. 我知道$triggerInput是什么。 It's the content of the message. 这是消息的内容。 But what is $inputFile ? 但是$inputFile什么? And where does {file} come from? {file}是哪里来的?

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. 我认为这可能将$inputFile定义为“ aapdftoimage / $ file”,但它没有任何作用。

Needless to say, I'm at a standstill. 不用说,我处于停滞状态。 Can anyone give me some guidance and straighten me out? 谁能给我一些指导并理顺我?

@Henry Hamid Safi is correct. @亨利·哈米德·萨菲(Henry Hamid Safi)是正确的。 Using C#, you can leverage the Binder object to dynamically name the file. 使用C#,可以利用Binder对象动态命名文件。

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. 在您的用例中,动态提供文件名的唯一方法是在触发器有效负载中将其作为JSON对象传递。 Here is a sample setup that worked for me. 这是为我工作的示例设置。

function.json: 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: 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: 请求正文(如果在Portal中使用“测试”窗格)或队列触发器有效负载:

{
  "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: OneDrive文件夹屏幕截图: 在此处输入图片说明

Working example 工作实例

Function.json: 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: run.ps1:

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

Copy-Item $inputFile $outputFile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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