You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell Runbook刷新Azure AS单表遇415错误,请求调试

错误原因及修复方案

核心问题:415 Unsupported Media Type 成因

这个错误源于请求的媒体类型不符合服务端要求,你的代码存在3处关键错误:

1. ContentType 拼写错误

Invoke-WebRequest参数中写的是"aapplication/json",多了一个字母a,导致服务端无法识别正确的媒体类型。

2. 请求头的Content-Type键名错误

HTTP标准请求头中,内容类型的键名是Content-Type(带连字符),你写的ContentType不符合规范,会导致服务端无法正确解析请求格式。

3. URL 拼接错误

AAS刷新API的标准URL结构为:https://<region>.asazure.windows.net/servers/<server-name>/models/<model-name>/refreshes,你将/refreshes包含在$DatabaseName中,会导致拼接后的URL路径错误。

修正后的完整代码

<#param
(
    [Parameter (Mandatory = $false)]
    [object] $WebhookData,

    [Parameter (Mandatory = $false)]
    [String] $DatabaseName,
    [Parameter (Mandatory = $false)]
    [String] $AnalysisServer,
    [Parameter (Mandatory = $false)]
    [String] $RefreshType
)#>

# 修正:服务器地址仅保留到models层级
$AnalysisServer = "centralus.asazure.windows.net/servers/shipbobanalysisserver01/models"
# 修正:单独设置模型名称,不包含/refreshes路径
$DatabaseName = "POC Shipbob TabularModel"

$userName = $Credential.UserName
$securePassword = $Credential.Password
$password = $Credential.GetNetworkCredential().Password

$tenantId = ".........................................."
$requestAccessTokenUri  = "https://login.microsoftonline.com/$tenantId/oauth2/token"
$resource = 'https://centralus.asazure.windows.net/'
$pass = "..................................."

$requestBody = "grant_type=client_credentials&client_id=$userName&client_secret=$pass&resource=$resource"

$token = Invoke-RestMethod -Method Post -Uri $requestAccessTokenUri -Body $requestBody -ContentType 'application/x-www-form-urlencoded'

# 修正:请求头使用标准的Content-Type键名
$header=@{
    "Content-Type"="application/json"
    "Authorization"="Bearer $($token.access_token)"
    "Accept"="application/json"
}

# 修正:按照标准结构拼接URL
$baseUrl = "https://$AnalysisServer/$DatabaseName/refreshes"

$body =
'{
    "Type": "Full",
    "CommitMode": "transactional",
    "MaxParallelism": 2,
    "RetryCount": 2,
    "Objects": [
        {
            "table": "Hist_ShipbobLive_dbo_FulfillmentCenterType",
            "partition": "Partition"
        }
    ]
}'

# 修正:ContentType参数拼写错误,移除多余的a
Invoke-WebRequest -Uri $baseUrl -Body $body -Headers $header -Method Post -ContentType "application/json"

额外优化建议

  • 避免手动编写JSON,使用ConvertTo-Json生成请求体,降低语法错误概率:
$bodyObject = @{
    Type = "Full"
    CommitMode = "transactional"
    MaxParallelism = 2
    RetryCount = 2
    Objects = @(
        @{
            table = "Hist_ShipbobLive_dbo_FulfillmentCenterType"
            partition = "Partition"
        }
    )
}
$body = $bodyObject | ConvertTo-Json -Depth 3
  • 调试时添加-Verbose参数,查看请求的详细日志,辅助定位问题。

内容的提问来源于stack exchange,提问作者user2851806

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 17:17:06