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

无法使用JavaScript SDK列出Azure VPN网关,请求技术排查

Azure JavaScript SDK无法列出订阅下的VPN网关(返回空数组)

我的订阅下存在2个VPN网关,通过Azure CLI可以正常列出,但使用以下JavaScript代码调用Azure SDK时,返回结果为空数组[],请帮忙解决:

const { DefaultAzureCredential } = require("@azure/identity");
const { NetworkManagementClient } = require("@azure/arm-network");

const subscriptionId="subid"
const resourceGroupName = "azure_dep";
const gatewayName = "VNet2GW";

// Set up the credentials and network management client
const credential = new DefaultAzureCredential();
const client = new NetworkManagementClient(credential, subscriptionId);

async function vpnGatewayListBySubscription() {
    const resArray = new Array();
    for await (let item of client.vpnGateways.list()) {
        resArray.push(item);
    }
    console.log(resArray);
}

vpnGatewayListBySubscription();

核心问题:使用了错误的SDK方法

你当前调用的client.vpnGateways.list()方法需要传入资源组名称参数,用于列出指定资源组下的VPN网关;如果要列出整个订阅下的所有VPN网关,应该使用client.vpnGateways.listAll()方法。

修改后的代码

const { DefaultAzureCredential } = require("@azure/identity");
const { NetworkManagementClient } = require("@azure/arm-network");

const subscriptionId="subid"

// Set up the credentials and network management client
const credential = new DefaultAzureCredential();
const client = new NetworkManagementClient(credential, subscriptionId);

async function vpnGatewayListBySubscription() {
    const resArray = new Array();
    // 使用listAll()方法替代list()
    for await (let item of client.vpnGateways.listAll()) {
        resArray.push(item);
    }
    console.log(resArray);
}

vpnGatewayListBySubscription();

额外排查点

如果修改后仍无法获取网关,检查以下内容:

  • 权限验证:确保DefaultAzureCredential使用的身份(本地开发时通常是Azure CLI登录账户)拥有订阅的Network Contributor或Reader权限,可通过Azure门户IAM页面或az role assignment list --assignee <用户/SP ID>命令确认。
  • SDK版本:确认@azure/arm-network包为最新版本,旧版本可能存在方法差异,执行npm update @azure/arm-network更新。
  • 资源类型匹配:确认CLI列出的是VPN网关(对应命令az network vpn-gateway list),如果是虚拟网络网关(对应命令az network vnet-gateway list),需调用SDK中virtualNetworkGateways.listAll()方法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 23:33:19