准备更新以导入 IoT 中心的设备更新

了解如何获取新的更新,并准备该更新,以便导入到 IoT Hub 的设备更新中。

先决条件

获取设备的更新

在设置设备更新并预配设备后,您需要准备好要部署到这些设备上的更新文件。

  • 如果已从原始设备制造商(OEM)或解决方案集成商购买设备,则组织很可能为你提供更新文件,而无需创建更新。 请联系 OEM 或解决方案集成商,了解他们如何提供更新。

  • 如果组织已为所使用的设备创建软件,则同一组将是为该软件创建更新的组。

在使用适用于 IoT 中心的设备更新创建要部署的更新时,请根据你的情境从基于映像的方法 或基于包的方法 开始。

创建基本设备更新导入清单

获取更新文件后,创建一个导入清单来描述更新。 如果尚未这样做,请务必熟悉基本 导入概念。 虽然可以使用文本编辑器手动创作导入清单 JSON,但本指南将使用 PowerShell 作为示例。

小提示

尝试 基于映像的基于包的代理更新 教程,如果尚未这样做。 还可以从这些教程中查看示例导入清单文件以供参考。

  1. 克隆Azure/iot-hub-device-updateGit 存储库

  2. 在 PowerShell 中导航至您的本地克隆文件夹中的 Tools/AduCmdlets

  3. 将以下示例参数值替换为你自己的参数值后运行以下命令:提供程序、名称、版本、属性、处理程序、已安装的条件、文件。 有关可以使用哪些值的详细信息,请参阅 导入架构和 API 信息。 特别是 请注意,不能将同一组完全相同的兼容性属性与多个提供程序和名称组合一起使用。

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
    
    Import-Module ./AduUpdate.psm1
    
    $updateId = New-AduUpdateId -Provider Contoso -Name Toaster -Version 1.0
    
    $compat = New-AduUpdateCompatibility -Properties @{ deviceManufacturer = 'Contoso'; deviceModel = 'Toaster' }
    
    $installStep = New-AduInstallationStep -Handler 'microsoft/swupdate:1'-HandlerProperties @{ installedCriteria = '1.0' } -Files 'path to your update file'
    
    $update = New-AduImportManifest -UpdateId $updateId -Compatibility $compat -InstallationSteps $installStep
    
    # Write the import manifest to a file, ideally next to the update file(s).
    $update | Out-File "./$($updateId.provider).$($updateId.name).$($updateId.version).importmanifest.json" -Encoding utf8
    

创建导入清单后,如果已准备好导入更新,可以滚动到此页面底部的“后续步骤”链接。

为代理更新创建进阶设备更新导入清单

如果更新更为复杂,例如 代理更新,则可能需要创建多个导入清单。 可以使用上一部分中的同一个 PowerShell 脚本为复杂更新创建父级和子导入清单。 将示例参数值替换为你自己的参数值后,运行以下命令。 有关可以使用哪些值的详细信息,请参阅 导入架构和 API 信息

  Import-Module $PSScriptRoot/AduUpdate.psm1 -ErrorAction Stop
  
  # We will use arbitrary files as update payload files.
  $childFile = "$env:TEMP/childFile.bin.txt"
  $parentFile = "$env:TEMP/parentFile.bin.txt"
  "This is a child update payload file." | Out-File $childFile -Force -Encoding utf8
  "This is a parent update payload file." | Out-File $parentFile -Force -Encoding utf8
  
  # ------------------------------
  # Create a child update
  # ------------------------------
  Write-Host 'Preparing child update ...'
  
  $microphoneUpdateId = New-AduUpdateId -Provider Contoso -Name Microphone -Version $UpdateVersion
  $microphoneCompat = New-AduUpdateCompatibility -DeviceManufacturer Contoso -DeviceModel Microphone
  $microphoneInstallStep = New-AduInstallationStep -Handler 'microsoft/swupdate:1' -Files $childFile
  $microphoneUpdate = New-AduImportManifest -UpdateId $microphoneUpdateId `
                                               -IsDeployable $false `
                                               -Compatibility $microphoneCompat `
                                               -InstallationSteps $microphoneInstallStep `
                                               -ErrorAction Stop -Verbose:$VerbosePreference
  
  # ------------------------------
  # Create another child update
  # ------------------------------
  Write-Host 'Preparing another child update ...'
  
  $speakerUpdateId = New-AduUpdateId -Provider Contoso -Name Speaker -Version $UpdateVersion
  $speakerCompat = New-AduUpdateCompatibility -DeviceManufacturer Contoso -DeviceModel Speaker
  $speakerInstallStep = New-AduInstallationStep -Handler 'microsoft/swupdate:1' -Files $childFile
  $speakerUpdate = New-AduImportManifest -UpdateId $speakerUpdateId `
                                            -IsDeployable $false `
                                            -Compatibility $speakerCompat `
                                            -InstallationSteps $speakerInstallStep `
                                            -ErrorAction Stop -Verbose:$VerbosePreference
  
  # ------------------------------------------------------------
  # Create the parent update which parents the child update above
  # ------------------------------------------------------------
  Write-Host 'Preparing parent update ...'
  
  $parentUpdateId = New-AduUpdateId -Provider Contoso -Name Toaster -Version $UpdateVersion
  $parentCompat = New-AduUpdateCompatibility -DeviceManufacturer Contoso -DeviceModel Toaster
  $parentSteps = @()
  $parentSteps += New-AduInstallationStep -Handler 'microsoft/script:1' -Files $parentFile -HandlerProperties @{ 'arguments'='--pre'} -Description 'Pre-install script'
  $parentSteps += New-AduInstallationStep -UpdateId $microphoneUpdateId -Description 'Microphone Firmware'
  $parentSteps += New-AduInstallationStep -UpdateId $speakerUpdateId -Description 'Speaker Firmware'
  $parentSteps += New-AduInstallationStep -Handler 'microsoft/script:1' -Files $parentFile -HandlerProperties @{ 'arguments'='--post'} -Description 'Post-install script'
  
  $parentUpdate = New-AduImportManifest -UpdateId $parentUpdateId `
                                        -Compatibility $parentCompat `
                                        -InstallationSteps $parentSteps `
                                        -ErrorAction Stop -Verbose:$VerbosePreference
  
  # ------------------------------------------------------------
  # Write all to files
  # ------------------------------------------------------------
  Write-Host 'Saving manifest and update files ...'
  
  New-Item $Path -ItemType Directory -Force | Out-Null
  
  $microphoneUpdate | Out-File "$Path/$($microphoneUpdateId.Provider).$($microphoneUpdateId.Name).$($microphoneUpdateId.Version).importmanifest.json" -Encoding utf8
  $speakerUpdate | Out-File "$Path/$($speakerUpdateId.Provider).$($speakerUpdateId.Name).$($speakerUpdateId.Version).importmanifest.json" -Encoding utf8
  $parentUpdate | Out-File "$Path/$($parentUpdateId.Provider).$($parentUpdateId.Name).$($parentUpdateId.Version).importmanifest.json" -Encoding utf8
  
  Copy-Item $parentFile -Destination $Path -Force
  Copy-Item $childFile -Destination $Path -Force
  
  Write-Host "Import manifest JSON files saved to $Path" -ForegroundColor Green
  
  Remove-Item $childFile -Force -ErrorAction SilentlyContinue | Out-Null
  Remove-Item $parentFile -Force -ErrorAction SilentlyContinue | Out-Null

后续步骤