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

修改PowerShell脚本API密钥参数后Invoke-RestMethod报500错误求助

解决PowerShell脚本中API密钥参数传递导致的500内部服务器错误

问题根源

你修改后的脚本里,API密钥的赋值用了单引号包裹变量:

$headers = @{'api-key' = '$ApiKey'}

PowerShell中,单引号字符串不会解析变量,所以实际发送给API的api-key头内容是字符串$ApiKey,而非你从SSIS传入的真实密钥值,这直接导致API鉴权失败,返回500错误。

解决方案

把单引号改成双引号,或者直接引用变量即可:

方案1:直接引用变量(推荐)

$headers = @{'api-key' = $ApiKey}

方案2:用双引号解析变量

$headers = @{'api-key' = "$ApiKey"}

修改后的完整脚本:

param( [string]$resultFile, [string]$from, [string]$to, [string]$ApiKey)

try {
    $headers = @{'api-key' = $ApiKey}

    $url = 'https://******?query={ calendar(where: {
        and: { from: { gte: "' + $from + '" }, to: { lte: "' + $to + '" } } } )
        {
            concerned
            {
                username
                regime
            }
            id
            status
            from
            to  
            primaryType
            secondaryType    
        }
    }'

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Invoke-RestMethod -Uri $url -Method Get -Headers $headers -OutFile $resultFile
}
catch {
    write-output $PSItem.ToString()
    exit 1
}

验证建议

可以在脚本中添加调试输出,确认参数是否正确传递:

# 打印传入的API密钥
Write-Host "传入的API密钥:$ApiKey"
# 打印最终的请求头
Write-Host "请求头信息:$($headers | ConvertTo-Json)"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 06:36:07