Powershell 使用Azure函数输出文件

Powershell 使用Azure函数输出文件,powershell,azure-functions,Powershell,Azure Functions,我正在尝试Azure功能。基本上,我的用例是使用GUID作为GET参数调用函数,让函数下载WIX工具箱DLL和MSI文件,更新MSI文件中的参数,并将该文件返回给函数的调用方(例如,作为下载提示) 我大部分时间都在那里,只是需要一些帮助来实现下载提示/发送,我的代码到目前为止: $urlWix = "http://domain/wix.dll" $outputWix = "$Env:TEMP\wix.dll" Invoke-WebRequest -Uri $urlWix -OutFile $o

我正在尝试Azure功能。基本上,我的用例是使用GUID作为GET参数调用函数,让函数下载WIX工具箱DLL和MSI文件,更新MSI文件中的参数,并将该文件返回给函数的调用方(例如,作为下载提示)

我大部分时间都在那里,只是需要一些帮助来实现下载提示/发送,我的代码到目前为止:

$urlWix = "http://domain/wix.dll"
$outputWix = "$Env:TEMP\wix.dll"

Invoke-WebRequest -Uri $urlWix -OutFile $outputWix
try{Add-Type -Path $outputWix}catch{$Null}
$urlMSI = "http://domain/file.msi"
$outputFile = "$Env:TEMP\file.msi"

Invoke-WebRequest -Uri $urlMSI -OutFile $outputFile

$oDatabase = New-Object Microsoft.Deployment.WindowsInstaller.Database($outputFile,[Microsoft.Deployment.WindowsInstaller.DatabaseOpenMode]::Direct);

$sSQLQuery = "SELECT * FROM Property WHERE Property= 'MYPROPERTY'"

[Microsoft.Deployment.WindowsInstaller.View]$oView = $oDatabase.OpenView($sSQLQuery)
$oView.Execute()

$oRecord = $oView.Fetch() 
$oRecord.SetString("Value","MyCustomValue")
$oView.Modify([Microsoft.Deployment.WindowsInstaller.ViewModifyMode]::Update,$oRecord)

$oView.Close();
$oDatabase.Dispose();
$file = get-item $outputFile
write-output $file

不幸的是,由于内容类型问题,这在powershell中是不可能的。您可以通过C#、F#或Node()函数来实现这一点。问题是您需要通过JSON响应格式指定标题,这将把任何非文本数据转换成base64字符串

如果要通过powershell发送文本文件,可以:

$response = ConvertTo-JSON @{
    Body="your file data";
    Headers=@{
        # unfortunately it seems functions does not support 'filename=...'
        'Content-Disposition'='attachment'; 
        # you would use application/octet-stream, but because it's converted to JSON you lose binary content
        'Content-Type'='text/plain';        
    };
}
Out-File -Encoding Ascii -FilePath $res -inputObject $response

如果您的返回数据来自
ConvertTo Csv
ConvertTo Csv
将被视为文本,因为在尝试从
$res
绑定文件中读取json时失败。您能否仅附加base64编码的字符串正文并设置适当的标题?