无法使用JavaScript sdk列出Azure VPN网关

bnl4lu3b  于 2023-04-07  发布在  Java
关注(0)|答案(1)|浏览(125)

这是我的代码。这里我用“subid”替换了我的订阅ID。

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();

输出只是一个像这样的空列表[].但我有2 VPN网关在我的子.进一步,我可以使用Azure CLI列出它们.但我不能使用JavaScript获得列表.

dgsult0t

dgsult0t1#

我在我的环境中尝试了一下,得到了以下结果:

您可以使用下面的JavaScript代码来获取VPN网关列表。

验证码:

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

const subscriptionId = '';
const credential = new DefaultAzureCredential();
const client = new NetworkManagementClient(credential, subscriptionId);

async function vpnGatewayListBySubscription() {
  for await (let item of client.vpnGateways.list()) {
        console.log(item.name)
        console.log(item.location)
        console.log(item.id)
  }
}

vpnGatewayListBySubscription();

输出:

上面的代码执行并成功列出了VPN网关的名称、ID和位置。

908d062c7c964e78815e889ac0815c5c-westindia-gw
westindia
/subscriptions/subid/resourceGroups/Networking-Resources/providers/Microsoft.Network/vpnGateways/908d062c7c964e78815e889ac0815c5c-westindia-gw

参考号:虚拟网络网关-列表- REST API(Azure Network Gateway)|微软学习

相关问题