使用PowerShell调用PowerBI REST API添加网关数据源报错求助
我正在使用PowerShell部署.pbix文件到工作区,需要先在网关上创建SQL数据源。通过服务主体调用Power BI REST API创建数据源时,遇到以下错误:
Encountered errors when invoking the command: { "code": "DM_GWPipeline_UnknownError", "pbi.error": { "code": "DM_GWPipeline_UnknownError",
"parameters": {}, "details": [ { "code": "DM_ErrorDetailNameCode_UnderlyingErrorMessage", "detail": { "type": 1,
"value": "The parameter is incorrect.\r\n" } }, { "code": "DM_ErrorDetailNameCode_UnderlyingHResult", "detail": {
"type": 1, "value": "-2146893785" } } ], "exceptionCulprit": 1 } }
出现DM_GWPipeline_UnknownError且提示参数错误,核心是请求参数不符合API要求,按以下步骤排查修复:
校验SQL数据源必填参数:
确保请求体包含以下核心内容,格式严格匹配要求:datasourceType:必须设为"Sql",大小写不能错误connectionDetails:正确填写SQL服务器和数据库名,格式如{"server": "你的服务器地址", "database": "目标数据库名"},避免多余转义字符credentialDetails:服务主体认证时,credentialType设为"ServicePrincipal",同时准确填写servicePrincipalId和servicePrincipalKey,注意密钥无多余空格或未转义的特殊字符
确认网关权限:
调用API的服务主体必须被添加为目标网关的管理员,无权限会触发参数校验失败,返回模糊的参数错误提示检查PowerShell请求构造:
使用Invoke-RestMethod时,确保请求体通过ConvertTo-Json正确序列化,且指定-ContentType "application/json",避免格式解析问题导致参数识别错误。示例代码如下:$body = @{ datasourceName = "SQL-Datasource" datasourceType = "Sql" connectionDetails = @{ server = "sql-server-name" database = "target-db" } credentialDetails = @{ credentialType = "ServicePrincipal" credentials = @{ servicePrincipalId = "你的服务主体ID" servicePrincipalKey = "你的服务主体密钥" } privacyLevel = "Organizational" } } | ConvertTo-Json -Depth 10 Invoke-RestMethod -Uri "https://api.powerbi.com/v1.0/myorg/gateways/{gatewayId}/datasources" -Method Post -Headers $headers -Body $body -ContentType "application/json"解析HResult错误码:
HResult-2146893785对应ERROR_INVALID_PARAMETER,说明存在完全不符合要求的参数,重点检查网关ID是否为有效GUID、连接字符串格式、服务主体密钥是否正确
内容的提问来源于stack exchange,提问作者Riyaj Shaikh

