如何通过PowerShell调用API或模块获取Nutanix集群报告
通过PowerShell获取Nutanix Prism集群容量信息的两种方案
两种方案均可直接拿到集群总容量、已使用容量、空闲容量等核心数据,可根据实际场景选择。
方案1:使用Nutanix官方PowerShell模块(Nutanix.Cli)
适合需要频繁调用Nutanix接口的场景,封装好的命令调用更简单。
- 第一步安装官方模块:
# 管理员权限执行,仅当前用户使用可加 -Scope CurrentUser 参数 Install-Module -Name Nutanix.Cli
- 第二步连接Prism服务器:
# 推荐用安全凭证方式传入账号密码 $cred = Get-Credential Connect-NutanixCluster -Server <你的Prism服务器地址> -Credential $cred
- 第三步获取并输出容量信息:
Get-NutanixCluster | Select-Object ` Name, @{Name='总容量(TB)'; Expression={[math]::Round($_.TotalCapacityBytes / 1TB, 2)}}, @{Name='已使用容量(TB)'; Expression={[math]::Round($_.UsedCapacityBytes / 1TB, 2)}}, @{Name='空闲容量(TB)'; Expression={[math]::Round($_.FreeCapacityBytes / 1TB, 2)}}
方案2:直接调用Prism REST API
无需额外安装模块,适合临时查询、无法安装第三方模块的环境。
- 先构造请求头和身份验证信息:
$prismServer = "替换为你的Prism服务器IP/域名" $username = "替换为管理员账号" $password = "替换为账号密码" # 忽略自签名证书错误(Prism默认用自签名证书) Add-Type @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } "@ [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 # 构造Basic Auth头 $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password))) $headers = @{ "Authorization" = "Basic $base64AuthInfo" "Content-Type" = "application/json" }
- 调用接口获取并格式化输出容量信息:
# 请求集群数据 $clusterRes = Invoke-RestMethod -Uri "https://$prismServer/api/nutanix/v3/clusters/list" -Method Post -Headers $headers -Body '{"kind":"cluster"}' # 提取容量字段 $storageInfo = $clusterRes.entities.status.resources.storage $totalBytes = $storageInfo.capacity_bytes $usedBytes = $storageInfo.usage_bytes $freeBytes = $totalBytes - $usedBytes # 格式化输出 [PSCustomObject]@{ 集群名称 = $clusterRes.entities.spec.name 总容量(TB) = [math]::Round($totalBytes / 1TB, 2) 已使用容量(TB) = [math]::Round($usedBytes / 1TB, 2) 空闲容量(TB) = [math]::Round($freeBytes / 1TB, 2) }
注意:上述示例默认适配单集群环境,多集群环境下遍历
$clusterRes.entities数组即可获取所有集群的容量数据。
内容的提问来源于stack exchange,提问作者Shweta Agarwal
相关产品推荐
相关产品推荐

