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

如何过滤PowerShell中AWS RDS查询输出,获取指定前缀的最新实例地址

完整实现脚本(PowerShell,直接写入Script.ps1即可运行)

# 1. 拉取us-east-1区域全量RDS实例端点地址
$allRdsEndpoints = aws rds --region us-east-1 describe-db-instances --query "DBInstances[*].Endpoint.Address" | ConvertFrom-Json

# 2. 提取前缀为db-tenant的末尾数值最大的实例地址(Output1)
$output1 = $allRdsEndpoints | Where-Object { $_ -match '^db-tenant' } | ForEach-Object {
    # 匹配地址最后一段横杠后的数字作为排序依据
    if ($_ -match '-(\d+)\.[^.]+us-east-1\.rds\.amazonaws\.com$') {
        [PSCustomObject]@{
            Address = $_
            Num = [int]$matches[1]
        }
    }
} | Sort-Object Num -Descending | Select-Object -First 1 -ExpandProperty Address

# 3. 提取前缀为neb-dev的末尾数值最大的实例地址(Output2)
$output2 = $allRdsEndpoints | Where-Object { $_ -match '^neb-dev' } | ForEach-Object {
    if ($_ -match '-(\d+)\.[^.]+us-east-1\.rds\.amazonaws\.com$') {
        [PSCustomObject]@{
            Address = $_
            Num = [int]$matches[1]
        }
    }
} | Sort-Object Num -Descending | Select-Object -First 1 -ExpandProperty Address

# 4. 按要求格式输出结果
@"
"Output1":"$output1",
"Output2":"$output2"
"@

逻辑说明

  • 首先将AWS CLI返回的JSON结果转换为PowerShell可处理的数组对象
  • 用正则分别过滤两个前缀的地址,同时提取地址末尾的版本数字转换为整数
  • 对数字降序排序后取第一条,即为数值最大的最新实例地址
  • 输出格式完全匹配要求的预期样式,如果需要标准JSON结构,可以直接将两个结果存入自定义对象后调用ConvertTo-Json即可

如果你的RDS地址结构和示例有差异,调整正则匹配规则即可适配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 02:36:08