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

PowerShell调用API获取Azure文件共享存储详情(含使用容量)遇问题

问题:通过PowerShell调用Azure API获取文件共享已使用容量失败

我正尝试通过PowerShell脚本调用API获取Azure文件共享的详细信息(尤其是已使用容量),但因为要集成到现有PowerShell工作流,Azure CLI或手动门户操作的方案不适用。

我的脚本如下:

$subscriptionId = ""
$resourceGroupName = ""
$storageAccountName = ""
$accessToken = "" 

# 设置请求头,包含Bearer令牌
$headers = @{
    'Authorization' = "Bearer $accessToken"
    'Content-Type' = 'application/json'
}

# 设置API版本
$apiVersion = "2023-04-01"

# 构造FileShares API请求地址
$url = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Storage/storageAccounts/$storageAccountName/fileServices/default/shares?api-version=$apiVersion"

# 发起API调用
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method Get

# 输出响应结果
$response.value

脚本执行无错误,但仅返回基础元数据,未包含已使用容量:

id : /subscriptions/subID/resourceGroups/ResourceName/providers/Microsoft.Storage/storageAccounts/StorageAcctName/fileServices/default/shares/ServerName-g
name : ServerName-g
type : Microsoft.Storage/storageAccounts/fileServices/shares
etag : “0x8D”
properties : @{leaseStatus=unlocked; leaseState=available; accessTier=Cool; accessTierChangeTime=2022-12-30T14:18:32.0000000Z; lastModifiedTime=2024-03-09T20:46:29.0000000Z; shareQuota=102400;
enabledProtocols=SMB}

解决方法

问题根源

你当前调用的是Azure Resource Manager(ARM)管理API,该接口默认仅返回文件共享的基础配置元数据,已使用容量属于存储使用统计数据,需要通过特定参数扩展或直接调用存储服务API获取。

方案一:修改ARM API请求,添加扩展参数

从API版本2021-04-01开始,ARM的文件共享列表接口支持通过$expand=stats参数返回使用统计数据,直接修改原请求URL即可:

# 关键修改:添加`$expand=stats`参数,PowerShell中`$`需转义为`` `$ ``避免被当作变量解析
$url = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Storage/storageAccounts/$storageAccountName/fileServices/default/shares?api-version=$apiVersion&`$expand=stats"

重新执行脚本后,properties字段会新增shareUsageBytes属性(已使用容量,单位为字节),可转换为更直观的GB单位:

# 遍历结果并输出已使用容量
$response.value | ForEach-Object {
    $usedGB = [math]::Round($_.properties.shareUsageBytes / 1GB, 2)
    Write-Host "共享名称:$($_.name),已使用容量:$usedGB GB"
}

方案二:直接调用存储服务REST API

如果需要更详细的存储统计数据,可直接调用Azure存储服务的API:

  1. 通过ARM API获取存储账户密钥
$keyUrl = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Storage/storageAccounts/$storageAccountName/listKeys?api-version=2023-04-01"
$keysResponse = Invoke-RestMethod -Uri $keyUrl -Headers $headers -Method Post
$accountKey = $keysResponse.keys[0].value
  1. 生成共享访问签名(SAS)令牌
$startTime = (Get-Date).AddHours(-1)
$expiryTime = (Get-Date).AddHours(1)
$sasToken = New-AzStorageAccountSASToken -Service File -ResourceType Service,Container,Object -Permission "r" -StartTime $startTime -ExpiryTime $expiryTime -Context (New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $accountKey)
  1. 调用存储API获取共享统计
$shareName = "ServerName-g"
$storageApiUrl = "https://$storageAccountName.file.core.windows.net/$shareName?restype=share&comp=stats$sasToken"
$shareStats = Invoke-RestMethod -Uri $storageApiUrl -Method Get -Headers @{ "x-ms-version" = "2023-01-03" }

$usedGB = [math]::Round($shareStats.ShareStats.Usage / 1GB, 2)
Write-Host "文件共享 $shareName 已使用容量:$usedGB GB"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 13:45:29