簡體   English   中英

帶有PowerShell,存儲隊列觸發器和OneDrive for Business的Azure功能

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

我正在嘗試創建一個Azure函數,該函數通過存儲隊列觸發器執行PowerShell。 出於測試目的,我希望此功能可以處理OneDrive for Business帳戶中的文件。 要將文件aapdftoimage/ThreePages.pdfaapdftoimage/output_ThreePages.pdf

將OneDrive for Business集成為輸入時,只要隊列中的新消息觸發該功能,我都會收到錯誤消息。 如果我斷開OneDrive作為輸入的連接,則不會出現任何錯誤,並且$triggerInput包含該消息。

錯誤是:

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'.

這是我的PowerShell:

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

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

這是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
}

在撰寫本文時,我認為部分困惑在於OneDrive for Business的輸入和輸出(在測試中未連接)集成。

我知道$triggerInput是什么。 這是消息的內容。 但是$inputFile什么? {file}是哪里來的?

我以為也許我會做以下事情,但它也不起作用(相同的錯誤):

$file = Get-Content $triggerInput

我認為這可能將$inputFile定義為“ aapdftoimage / $ file”,但它沒有任何作用。

不用說,我處於停滯狀態。 誰能給我一些指導並理順我?

@亨利·哈米德·薩菲(Henry Hamid Safi)是正確的。 使用C#,可以利用Binder對象動態命名文件。

在您的用例中,動態提供文件名的唯一方法是在觸發器有效負載中將其作為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:

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

請求正文(如果在Portal中使用“測試”窗格)或隊列觸發器有效負載:

{
  "file":"ThreePages.pdf"
}

日志條目:

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文件夾屏幕截圖: 在此處輸入圖片說明

工作實例

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM