带有 PowerShell、存储队列触发器和 OneDrive for Business 的 Azure Function
Azure Function with PowerShell, Storage Queue trigger, and OneDrive for Business
我正在尝试创建一个使用存储队列触发器执行 PowerShell 的 Azure 函数。出于测试目的,我希望此函数能够操作我的 OneDrive for Business 帐户中的文件。将位于 aapdftoimage/ThreePages.pdf
的文件复制到 aapdftoimage/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",但它没有任何作用。
不用说,我处于停滞状态。任何人都可以给我一些指导并理顺我吗?
工作示例
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
}
运行.ps1:
$in = Get-Content $triggerInput
Write-Output "PowerShell script processed queue message '$in'"
Copy-Item $inputFile $outputFile
@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
}
运行.ps1:
$in = Get-Content $triggerInput
Write-Output "PowerShell script processed queue message '$in'"
Copy-Item $inputFile $outputFile
请求正文(如果在门户中使用测试窗格)或队列触发器负载:
{
"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 文件夹屏幕截图:
我正在尝试创建一个使用存储队列触发器执行 PowerShell 的 Azure 函数。出于测试目的,我希望此函数能够操作我的 OneDrive for Business 帐户中的文件。将位于 aapdftoimage/ThreePages.pdf
的文件复制到 aapdftoimage/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",但它没有任何作用。
不用说,我处于停滞状态。任何人都可以给我一些指导并理顺我吗?
工作示例
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
}
运行.ps1:
$in = Get-Content $triggerInput
Write-Output "PowerShell script processed queue message '$in'"
Copy-Item $inputFile $outputFile
@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
}
运行.ps1:
$in = Get-Content $triggerInput
Write-Output "PowerShell script processed queue message '$in'"
Copy-Item $inputFile $outputFile
请求正文(如果在门户中使用测试窗格)或队列触发器负载:
{
"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 文件夹屏幕截图: