首页 > 解决方案 > 基于 PowerShell 的 Azure 函数输出到 OneDrive

问题描述

我有一个非常简单的“hello world”类型函数。在运行.ps1

$request = Invoke-RestMethod http://ipinfo.io/json | Select -ExpandProperty ip
$out = "$env:TEMP\zhello.txt"
Set-Content -value $request -path $out
Out-File -Encoding ASCII $out

和 function.json 有这个:

{
  "type": "apiHubFile",
  "name": "outputFile",
  "path": "output_{file}",
  "connection": "onedrive_ONEDRIVE",
  "direction": "out"
}

它成功运行,没有错误,但文件不在我的 OneDrive 中。还尝试使用 Azure Blob 存储作为输出。我的代码有什么问题?

标签: powershellazure-functions

解决方案


看起来您的Out-File命令没有任何值可写入$out文件路径。它可能正在擦除由Set-Content.

尝试注释掉Set-Content$request | Out-File -Encoding ASCII $out改用。

如果这不能解决问题,请确保您的第一行实际上是在检索数据并将其分配给$request您认为的那样。

编辑

您也可以尝试写信$out给主机,以确保您确切知道文件放置的位置。实施这些建议会呈现以下代码:

$request = "hello world"
$out = "$env:TEMP\zhello.txt"
Write-Host $out
$request | Out-File -Encoding ASCII $out

这会按我的预期生成文件。

如果这仍然对您不起作用,请通过尝试类似的方法确认您有权写入该文件位置"hello world 2" > "$env:TEMP\zhello.txt"


推荐阅读