基于Azure DevOps实现Dynamics 365 F&O部署自动化方案咨询
自动化Dynamics 365 F&O生产环境部署前/后批处理作业与队列扫描器管理
一、OData API端点说明
1. 批处理作业管理
- 读取/更新批处理作业:
/data/BatchJobs - 批处理作业状态枚举:可通过
/data/BatchJobStatuses获取完整值,核心状态对应:Waiting=2,Withhold=5
2. 队列扫描器管理
- 读取/更新队列扫描器:
/data/QueueScanners - 状态控制:通过更新
IsActive字段实现(true=启用,false=停用)
二、筛选并更新符合条件的批处理作业
筛选逻辑OData查询参数
针对非管理员创建的当日Waiting状态作业,构造如下过滤条件:
$filter= CreatedBy ne 'Admin' and day(CreatedDateTime) eq day(now()) and month(CreatedDateTime) eq month(now()) and year(CreatedDateTime) eq year(now()) and Status eq 2
更新请求示例(PATCH)
先通过GET请求获取符合条件的作业ID列表,再逐个发送PATCH请求(D365 OData暂不支持批量更新):
PATCH /data/BatchJobs(BatchJobId=guid'[目标作业ID]') HTTP/1.1 Content-Type: application/json {"Status": 5}
三、Azure DevOps集成的PowerShell自动化方案
核心脚本片段(含认证、前置处理、后置恢复)
# 基础配置 $foBaseUrl = "https://[你的F&O环境域名]/data" $clientId = "[Azure AD应用注册Client ID]" $clientSecret = "[Azure AD应用注册Client Secret]" $tenantId = "[租户ID]" # 获取OAuth2访问令牌 $tokenEndpoint = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" $tokenBody = @{ grant_type = "client_credentials" client_id = $clientId client_secret = $clientSecret scope = "$foBaseUrl/.default" } $tokenResponse = Invoke-RestMethod -Uri $tokenEndpoint -Method Post -Body $tokenBody $authHeader = @{ Authorization = "Bearer $($tokenResponse.access_token)" } # ------------------- 部署前置操作 ------------------- # 1. 获取符合条件的批处理作业 $filterQuery = "CreatedBy ne 'Admin' and day(CreatedDateTime) eq day(now()) and month(CreatedDateTime) eq month(now()) and year(CreatedDateTime) eq year(now()) and Status eq 2" $batchJobs = Invoke-RestMethod -Uri "$foBaseUrl/BatchJobs`?$filter=$filterQuery" -Headers $authHeader -Method Get $targetJobIds = $batchJobs.value | Select-Object -ExpandProperty BatchJobId # 2. 将作业状态改为Withhold foreach ($jobId in $targetJobIds) { $updateBody = @{ Status = 5 } | ConvertTo-Json Invoke-RestMethod -Uri "$foBaseUrl/BatchJobs(BatchJobId=guid'$jobId')" -Headers $authHeader -Method Patch -Body $updateBody -ContentType "application/json" } # 3. 停止所有队列扫描器 $allScanners = Invoke-RestMethod -Uri "$foBaseUrl/QueueScanners" -Headers $authHeader -Method Get foreach ($scanner in $allScanners.value) { if ($scanner.IsActive) { $updateBody = @{ IsActive = $false } | ConvertTo-Json Invoke-RestMethod -Uri "$foBaseUrl/QueueScanners(QueueScannerId=guid'$($scanner.QueueScannerId)')" -Headers $authHeader -Method Patch -Body $updateBody -ContentType "application/json" } } # ------------------- 插入LCS部署任务(可使用Azure DevOps LCS扩展任务) ------------------- # ------------------- 部署后置恢复操作 ------------------- # 1. 将批处理作业状态改回Waiting foreach ($jobId in $targetJobIds) { $updateBody = @{ Status = 2 } | ConvertTo-Json Invoke-RestMethod -Uri "$foBaseUrl/BatchJobs(BatchJobId=guid'$jobId')" -Headers $authHeader -Method Patch -Body $updateBody -ContentType "application/json" } # 2. 重启所有队列扫描器 foreach ($scanner in $allScanners.value) { $updateBody = @{ IsActive = $true } | ConvertTo-Json Invoke-RestMethod -Uri "$foBaseUrl/QueueScanners(QueueScannerId=guid'$($scanner.QueueScannerId)')" -Headers $authHeader -Method Patch -Body $updateBody -ContentType "application/json" }
Azure DevOps集成步骤
- 添加PowerShell任务,选择内联脚本或引用仓库内的脚本文件
- 将
clientId、clientSecret等敏感参数存入Azure DevOps变量组并标记为保密 - 在LCS部署任务前后,分别调用前置处理、后置恢复脚本片段
- 增加错误处理:脚本执行失败时触发告警,并尝试回滚已执行的操作
四、最佳实践
- 日志记录:在脚本中添加
Write-Host输出关键操作节点,便于流水线问题排查 - 状态备份:前置操作前将目标作业的ID和原始状态写入临时文件,避免恢复时遗漏
- 环境验证:先在UAT环境验证脚本逻辑,确认无业务影响后再推到生产
- 权限控制:确保Azure AD应用注册拥有D365 F&O中
BatchJob、QueueScanner实体的读写权限
内容的提问来源于stack exchange,提问作者Siddarth
相关产品推荐
相关产品推荐

