Public/TenantConfiguration/Baseline/Add-WUFBConfiguration.ps1

function Add-WUFBConfiguration {
  param(
    [Parameter(Mandatory)]
    [string]$TenantId
  )

  try {
    Connect-CustomerGraph -CustomerTenantId $TenantId

    $WUFBConfigurations = (Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations").value

    $WUFBConfigurationFiles = Get-ChildItem -Path "$PSScriptRoot\WUFBConfiguration" -Filter *.json
    foreach ($WUFBConfigurationFile in $WUFBConfigurationFiles) {
      $WUFBConfiguration = Get-Content -Path $WUFBConfigurationFile.FullName | ConvertFrom-Json -AsHashtable -Depth 100

      if ($WUFBConfigurations.displayName -contains $WUFBConfiguration.displayName) {
        Write-Host "Windows Update for Business configuration profile '$($WUFBConfiguration.displayName)' already exists, not creating.." -ForegroundColor Yellow
      }
      else {
        $WUFBConfiguration = Invoke-MgGraphRequest -Method POST -Uri "https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations" -Body $WUFBConfiguration
        Write-Host "Created Windows Update for Business configuration profile '$($WUFBConfiguration.DisplayName)'!" -ForegroundColor Green

        Invoke-MgGraphRequest -METHOD POST -Uri "https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations/$($WUFBConfiguration.id)/assign" -Body @{
          "assignments" = @(
            @{"target" = @{
              "@odata.type" = "#microsoft.graph.allDevicesAssignmentTarget" }
            }) 
          } | Out-Null

        Write-Host "Assigned Windows Update for Business configuration profile '$($WUFBConfiguration.displayName)' to all devices." -ForegroundColor Green
      }
    }
  }
  catch {
    throw "Failed to add Windows Update for Business configuration: $_"
  }
}