如何通过Azure Function Apps自动修改Power Apps的.msapp文件?
核心需求
实现脚本修改Microsoft Power Platform中Canvas App的.msapp源文件,核心目标是:向Azure Function Apps上传新的theme.json文件后,生成可正常使用的.msapp文件。
常规方法的局限
修改.msapp文件的常规流程是:解压文件→替换theme.json→重新压缩并改为.msapp扩展名。本地使用PowerShell 5.1脚本可完成此操作:
$AppName = Get-ChildItem -Directory -Exclude "NewMSAPP", "*RECYCLE.BIN" -Name $NewMSAPP = ".\NewMSAPP" # [...] # # Compress & change extension for new MSAPP file in NewMSAPP folder Compress-Archive -Path ".\$AppName\*" -DestinationPath "$NewMSAPP\$AppName.zip" -Update Rename-Item -Path "$NewMSAPP\$AppName.zip" -NewName $AppName".msapp"
但使用bash(Mac/Linux)、Python或C#执行此流程时均报错;改用PowerShell编写Azure Function Apps脚本也出现相同错误,经本地测试验证,原因是Azure Functions默认的PowerShell 7.2中Compress-Archive存在异常行为,导致生成的.msapp文件损坏无法使用。
尝试Power Apps CLI(pac)工具的问题
为规避上述问题,尝试使用Microsoft Power Apps CLI(pac)工具(一款.NET CLI工具,支持.msapp文件的打包和解包),本地执行以下命令可正常完成操作:
dotnet tool update --global Microsoft.PowerApps.CLI.Tool
pac canvas pack --msapp ToApp.msapp --sources FromSourceFolder
但在Azure Function Apps中执行dotnet tool update命令时,触发UnauthorizedAccessException异常,尝试用runas命令以管理员身份执行也失败。由于该工具不在PowerShell Gallery中,无法添加到requirements.psd1文件进行依赖安装,因此需要解决两个问题:
- 是否有办法在Azure Function Apps中安装Microsoft Power Apps CLI(pac)工具?
- 是否有其他编程语言实现该需求的可行方案?
附完整PowerShell脚本(已注释pac工具的安装与使用部分):
$sourceDir = "TemplateTablet" #$sourceDir = "TemplateTablet2" # Change "References" to "Src" if TemplateTablet2 is used $appName = New-Guid # Decodes base64 string $decodedBytes = [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($Base64String)) Write-Output "App name: $($appName)" # Creates new folder New-Item -ItemType Directory -Path "tmp\$($appName)" # Copies template to new folder Get-ChildItem -Path $sourceDir | Copy-Item -Destination "tmp\$($appName)" -Recurse -Container # Check if Themes.json file exists if(!(Test-Path "tmp\$($appName)\References\Themes.json")) { #"tmp\$($appName)\Src\Themes.json" # Creates themes file New-item "tmp\$($appName)\References\Themes.json" -ItemType File -Value $decodedBytes #"tmp\$($appName)\Src\Themes.json" Write-Output "Themes.json file does not exists. Creating one." } else { Remove-item "tmp\$($appName)\References\Themes.json" #"tmp\$($appName)\Src\Themes.json" New-item "tmp\$($appName)\References\Themes.json" -ItemType File -Value $decodedBytes #"tmp\$($appName)\Src\Themes.json" Write-Output "Themes.json already exists. Deleting and creating new one." } # zip and rename folder Compress-Archive -Path "tmp\$($appName)\*" -DestinationPath "tmp\$($appName).zip" -Update Rename-Item -Path "tmp\$($appName).zip" -NewName $appName".msapp" # dotnet tool update --global Microsoft.PowerApps.CLI.Tool # pac canvas pack --sources "tmp\$($appName)" --msapp "HelloWorld.msapp" # pac canvas pack --sources "tmp\$($appName)\" --msapp HelloWorld.msapp # Encoding file to b64
最终解决方案
经过多次尝试,通过**直接编辑zip格式的.msapp文件(无需解压)**的方式解决了问题,彻底避免了重新压缩过程导致的.msapp文件损坏问题。
内容的提问来源于stack exchange,提问作者carlosmhd27

