【Azure 环境】在Azure虚拟机(经典) 的资源中,使用SDK导出VM列表的办法
Azure, 在2008年10月的Microsoft专业开发人员大会(PDC)上宣布,当时使用内部项目代号“Project Red Dog”,并于2010年2月正式发布为Windows Azure,然后在 2014年3月25日更名为Microsoft Azure
Azure, announced at Microsoft's Professional Developers Conference (PDC) in October 2008, went by the internal project codename "Project Red Dog", and formally released in February 2010, as Windows Azure before being renamed to Microsoft Azure on March 25, 2014
到2021年,经历了11年的发展,其中的架构也经历了大的变动,最明显的就是从最早的经典模式到目前的ARM(Azure Resource Manager)模式,这两中模式简单区别的模式图如:
更多ARM模型与经典模型的区别,可以参考官网介绍:https://docs.microsoft.com/zh-cn/azure/azure-resource-manager/management/deployment-models
问题描述
因为两种模型的不同,如果是较早使用经典部署创建的虚拟机,则必须继续通过经典操作对其进行操作。所以如何才能获取到VM列表呢?
对于虚拟机、存储帐户和虚拟网络,如果资源是通过经典部署创建的,则必须继续通过经典操作对其进行操作。 如果虚拟机、存储帐户或虚拟网络是通过 Resource Manager 部署创建的,则必须继续使用 Resource Manager 操作。
解决方法
如果是想获取ARM部署下的虚拟机资源,可以通过SDK获取,操作示例代码可见(【Azure Developer】使用Java SDK代码创建Azure VM (包含设置NSG,及添加数据磁盘SSD)),用 azure.virtualMachines().list() 这句代码,即可列出当前订阅下的所有VM列表。
但是,获取经典模式下的虚拟机则完全不同。经过研究,代码方式可以通过REST API的方式来解决,只是操作步骤也最复杂:
REST API 接口:https://management.core.chinacloudapi.cn/{订阅号ID}/services/hostedservices/{云服务名称}/deployments/{部署名}
1) 在调用接口前,需要保证您的当前订阅有经典管理员的权限,请参考:https://docs.azure.cn/zh-cn/role-based-access-control/classic-administrators
2) 获取一个证书文件
- 点击链接:https://portal.azure.cn/#blade/Microsoft_Azure_ClassicResources/PublishingProfileBlade
- 选择第一步中设置的订阅账号,点击Validate按钮,然后Download Publish Settings。下载的即是在第三步API调用中所需要的证书文件
3) 在代码中通过REST API的方式获取经典虚拟机信息
在Azure门户中先查看到经典云服务和部署的名称,然后通过 Get Deployment 方法获取到部署信息,返回的角色信息节点中包含经典虚拟机的信息。在下面的代码中替换掉以下四个参数
- credentialsPath = @"your.publishsettings" ## 第二步中所下载的文件
- subscriptionId = "subscriptionId" ##第一步中的订阅号
- cloudServiceName = "cloudServiceName" ##在云服务中查看
- deploymentName = "deploymentName" ##在云服务中查看
/****************************** Module Header ******************************\
* Module Name: Program.cs
* Project: GetClassicVMInfo
* Copyright (c) Microsoft Corporation.
*
* Managing Azure in Role instance may be difficult, because it requires a client
* certificate. This sample will show how to use the base64 string certificate
* instead of getting the certificate from Certificates store
*
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/en-us/openness/licenses.aspx#MPL
* All other rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/ using System;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Xml.Linq;
using System.Net;
using System.IO; namespace GetClassicVMInfo
{
class Program
{
public static string credentialsPath = @"your.publishsettings";
public static string subscriptionId = "subscriptionId";
public static string cloudServiceName = "cloudServiceName";
public static string deploymentName = "deploymentName"; static void Main(string[] args)
{
XElement x=XElement.Load(credentialsPath); var Base64cer = (from c in x.Descendants("Subscription")
where c.Attribute("Id").Value == subscriptionId
select c.Attribute("ManagementCertificate").Value).FirstOrDefault(); X509Certificate2 cer = null;
if (Base64cer != null)
{
cer = new X509Certificate2(Convert.FromBase64String(Base64cer));
}
if (cer != null)
{
GetHostedServicesByRESTAPI(subscriptionId, cer);
}
Console.ReadLine();
} static void GetHostedServicesByRESTAPI(string subscriptionId, X509Certificate2 certificate)
{
string uri = string.Format("https://management.core.chinacloudapi.cn/{0}/services/hostedservices/{1}/deployments/{2}", subscriptionId, cloudServiceName, deploymentName);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(uri));
request.Method = "GET";
request.ClientCertificates.Add(certificate);
request.ContentType = "application/xml";
request.Headers.Add("x-ms-version", "2016-06-01"); try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Parse the web response.
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream); string xml = reader.ReadToEnd();
XDocument doc = XDocument.Parse(xml);
XNamespace ns = doc.Root.Name.Namespace;
XElement roleInstanceList = doc.Root.Element(ns + "RoleInstanceList");
if (roleInstanceList != null)
{
foreach (XElement roleInstance in roleInstanceList.Elements())
{
Console.WriteLine("InstanceName:"+roleInstance.Element(ns + "InstanceName").Value);
Console.WriteLine("InstanceStatus:" + roleInstance.Element(ns + "InstanceStatus").Value);
}
}
// Close the no longer needed resources.
response.Close();
responseStream.Close();
reader.Close();
Console.ReadKey();
}
catch (WebException ex)
{
Console.Write(ex.Response.Headers.ToString());
Console.Read();
} }
}
}
PS: 也可以通过List Cloud Services方法获取到所有云服务,然后遍历其中的部署信息获取经典虚拟机的信息
Get Deployment : https://docs.microsoft.com/en-us/previous-versions/azure/reference/ee460804(v=azure.100)
List Cloud Services : https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/rest-list-cloud-services
以上文档中的URL均为Global Azure,如果在中国区使用,则需要转换成:management.core.chinacloudapi.cn, 这点非常重要。
附录一:在Azure 门户中使用Azure Resource Graph快速查看经典虚拟机列表(无代码),然后另存为本地文件。
点击“https://portal.azure.cn/#blade/HubsExtension/ArgQueryBlade”进入 Azure Resource Graph Explorer 页面,输入以下查询语句:
Resources
| project name, location, type
| where type =~ 'Microsoft.ClassicCompute/virtualMachines'
| order by name desc
查询结果:
参考资料
Azure 经典订阅管理员: https://docs.azure.cn/zh-cn/role-based-access-control/classic-administrators
Azure 资源管理器和经典部署:了解部署模型和资源状态 : https://docs.microsoft.com/zh-cn/azure/azure-resource-manager/management/deployment-models
Get Deployment : https://docs.microsoft.com/en-us/previous-versions/azure/reference/ee460804(v=azure.100)
List Cloud Services : https://docs.microsoft.com/en-us/rest/api/compute/cloudservices/rest-list-cloud-services
中国区Azure 终结点 Endpoint:https://docs.azure.cn/zh-cn/articles/guidance/developerdifferences#check-endpoints-in-azure
【Azure 环境】在Azure虚拟机(经典) 的资源中,使用SDK导出VM列表的办法的更多相关文章
- 【Azure 环境】Azure Key Vault (密钥保管库)中所保管的Keys, Secrets,Certificates是否可以实现数据粒度的权限控制呢?
问题描述 Key Vault (密钥保管库) 能不能针对用户授权实现指定用户只能访问某个或某些特定的key? 如当前有两个用户(User1, User2),在Key Vault中有10个Key,Use ...
- 【Azure 环境】Azure Resource Graph Explorer 中实现动态数组数据转换成多行记录模式 - mv-expand
问题描述 想对Azure中全部VM的NSG资源进行收集,如果只是查看一个VM的NSG设定,可以在门户页面中查看表格模式,但是如果想把导出成表格,可以在Azure Resource Graph Expl ...
- 【Azure 环境】Azure 云环境对于OpenSSL 3.x 的严重漏洞(CVE-2022-3602 和 CVE-2022-3786)的处理公告
问题描述 引用报告:(OpenSSL3.x曝出严重漏洞 : https://www.ctocio.com/ccnews/37529.html ) 最近OpenSSL 3.x 爆出了严重安全漏洞,分别是 ...
- 【Azure 环境】Azure通知中心(Notification Hub)使用百度推送平台解说
问题描述 在通知中心的页面中显示支持BaiDu,介绍一下支持的是百度(Baidu)的什么吗?Azure的这个功能在国内使用的时候是否可以保证国内安卓手机的信息送达率? 问题解答 通知中心的页面中的Ba ...
- 【Azure 环境】【Azure Developer】使用Python代码获取Azure 中的资源的Metrics定义及数据
问题描述 使用Python SDK来获取Azure上的各种资源的Metrics的名称以及Metrics Data的示例 问题解答 通过 azure-monitor-query ,可以创建一个 metr ...
- 使用 Azure CLI 创建 Windows 虚拟机
Azure CLI 用于从命令行或脚本创建和管理 Azure 资源. 本指南详细介绍如何使用 Azure CLI 部署运行 Windows Server 2016 的虚拟机. 部署完成后,我们连接到服 ...
- 使用 Azure CLI 管理 Azure 虚拟网络和 Linux 虚拟机
Azure 虚拟机使用 Azure 网络进行内部和外部网络通信. 本教程将指导读者部署两个虚拟机,并为这些 VM 配置 Azure 网络. 本教程中的示例假设 VM 将要托管包含数据库后端的 Web ...
- 使用 Azure CLI 创建 Linux 虚拟机
Azure CLI 用于从命令行或脚本创建和管理 Azure 资源. 本指南详细介绍了如何使用 Azure CLI 部署运行 Ubuntu 服务器的虚拟机. 服务器部署以后,将创建 SSH 连接,并且 ...
- ASP.NET 在 Windows Azure 环境中使用基于 SQLServer 的 Session
Session 嘛,占一点儿服务器资源,但是总归比 ViewState 和 Cookie 安全点儿,所以还是要用的. Windows Azure 环境中的 Web 服务器经由负载均衡调度,根本无法保证 ...
随机推荐
- 使用Webpack构建多页面程序
使用webpack搭建单页面程序十分常见,但在实际开发中我们可能还会有开发多页面程序的需求,因此我研究了一下如何使用webpack搭建多页面程序. 原理 将每个页面所在的文件夹都看作是一个单独的单页面 ...
- HDU(1420)Prepared for New Acmer(JAVA语言)【快速幂模板】
思路:快速幂裸题. //注意用long,否则会超范围 Problem Description 集训进行了将近2个礼拜,这段时间以恢复性训练为主,我一直在密切关注大家的训练情况,目前为止,对大家的表现相 ...
- 攻防世界 maze NJUPT CTF 2017
迷宫题 1 __int64 __fastcall main(__int64 a1, char **a2, char **a3) 2 { 3 signed __int64 mid_i; // rbx 4 ...
- Ubuntu18.04安装MySQL(未设置密码或忘记密码)
一 安装MySQL sudo apt-get update sudo apt-get install mysql-server 二 密码问题 1 安装时提示设置密码 这种情况没什么问题,通过已下命令登 ...
- 学习笔记-angular 使用uuid
import { UUID } from 'angular2-uuid'; let uuid = UUID.UUID().replace(/-/g, '').toLocaleUpperCase(); ...
- 后续来啦:Winform/WPF中快速搭建日志面板
后续来啦:Winform/WPF中快速搭建日志面板 继昨天发文ASP.NET Core 可视化日志组件使用(阅读文章,查看视频)后,视频下有朋友留言 "Winform客户端的程序能用它不?& ...
- Java(25-40)【数据类型转换、运算符、方法入门】
1.ASCII编码表 0--48 A--65 a--97 2. Unicode万国码 字符'中'为20013 3.算数运算符 double类型的加上int类型结果为double byte short ...
- 【CTF】CTFHub 技能树 文件头检查 writeup
PHP一句话木马 <?php @eval($_POST["pass"]);?> <?php eval($_REQUEST["pass"]);? ...
- VirtualBox CentOS8 调整分辨率
1 概述 VirtualBox安装完CentOS8后无法调节分辨率,需要安装额外的工具. 2 安装依赖包 首先确保虚拟机能正常连接网络,然后安装:kernel.kernel-core.kernel-m ...
- redhat7.6 更换 centos7 YUM
使用yum 遇到如下错误. This system is not registered to Red Hat Subscription Management. You can use subscrip ...