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

如何通过AZ CLI从SharePoint获取SiteID?脚本报错求助

SharePoint文档库文件获取脚本错误分析与修复

错误原因

  • 执行环境与语法不匹配:原脚本是Bash语法,但你在PowerShell环境中运行,导致curl的请求头参数解析失败。PowerShell对命令参数的处理逻辑和Bash不同,直接传入"Authorization: Bearer $ACCESS_TOKEN"会被当成单个字符串,而PowerShell的curl(实际是Invoke-WebRequest的别名)期望请求头是哈希表格式,因此抛出类型转换错误。
  • 站点URL空格未编码:请求URL里的Files / Test包含空格,Graph API无法直接识别,必须转义为%20。
  • 更新脚本逻辑混乱:错误引入PowerShell命令Invoke-Expression,混淆了Bash与PowerShell语法,进一步破坏脚本执行逻辑。

修复方案

根据你使用的终端环境,选择对应的修正脚本:

场景1:Bash环境执行(如WSL、Linux终端)

az login
ACCESS_TOKEN=$(az account get-access-token --resource https://graph.microsoft.com --query accessToken -o tsv)

# 编码站点路径中的空格为%20,确保Graph API能识别
SITE_ID=$(curl -X GET "https://graph.microsoft.com/v1.0/sites/testacme.sharepoint.com:/sites/Files%20/%20Test" -H "Authorization: Bearer $ACCESS_TOKEN" | jq -r '.id')

# 获取文件的后续代码

场景2:PowerShell环境执行

适配PowerShell语法,使用哈希表传递请求头:

az login
$ACCESS_TOKEN = az account get-access-token --resource https://graph.microsoft.com --query accessToken -o tsv

# 构造请求头哈希表,编码URL空格
$headers = @{
    "Authorization" = "Bearer $ACCESS_TOKEN"
}
$siteResponse = Invoke-WebRequest -Uri "https://graph.microsoft.com/v1.0/sites/testacme.sharepoint.com:/sites/Files%20/%20Test" -Headers $headers -Method Get
$SITE_ID = ($siteResponse.Content | ConvertFrom-Json).id

# 获取文件的后续代码

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 22:17:09