使用 Microsoft.Azure.Management.Fluent 获取 Azure VM 相关详细信息
Get Azure VM related details using Microsoft.Azure.Management.Fluent
我正在尝试使用 Microsoft.Azure.Management.Fluent 和相关的软件包集,
我需要以下有关订阅中所有 Azure VM 的详细信息:
谁创建了 VM、VM 的区域、VmSize、VM 的当前状态(如已停止/运行/已解除分配等),
我也需要
VM 的历史记录,根据此 VM 启动的持续时间和最近 x 个月/周 运行。
这可以使用 Microsoft.Azure.Management.Fluent 包吗?
如果你想知道虚拟机的启动和停止时间,我们可以从Azure activity 日志中获取。关于如何获取activity日志,我们可以使用Microsoft.Azure.Management.Monitor.Fluent包。
例如
- 创建服务主体并将 Azure RABC 角色分配给 sp(我使用 Azure CLI)
az login
#it will create a service principal and assign contributor role to the sp
az ad sp create-for-rbac -n "jonsp2"
- 安装包
// for more details about the package, please refer to https://www.nuget.org/packages/Microsoft.Azure.Management.Fluent/
Install-Package Microsoft.Azure.Management.Fluent -Version 1.34.0
- 代码
AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
clientId, // the sp appId
clientSecret, // the sp password
tenantId, // the sp tenant
AzureEnvironment.AzureGlobalCloud);
var azure = Microsoft.Azure.Management.Fluent.Azure.Configure()
.Authenticate(credentials)
.WithSubscription(subscriptionId);
var vms = await azure.VirtualMachines.ListAsync();
foreach (var vm in vms)
{
var staus = vm.PowerState.Value; // vm power state
var region = vm.RegionName; // vm region
var size = vm.Size.Value; // vm size
var logs = await azure.ActivityLogs.DefineQuery()
.StartingFrom(DateTime.Now.AddDays(-1))
.EndsBefore(DateTime.Now)
.WithAllPropertiesInResponse()
.FilterByResource("/subscriptions/e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68/resourceGroups/jimtest/providers/Microsoft.Compute/virtualMachines/testvm")
.ExecuteAsync();
List<DateTime?> stopTime = new List<DateTime?>();
List<DateTime?> startTime = new List<DateTime?>();
foreach (var log in logs)
{
// get stop time
if ((log.OperationName.LocalizedValue == "Deallocate Virtual Machine") & (log.Status.LocalizedValue == "Succeeded"))
{
stopTime.Add(log.EventTimestamp);
}
// get start tim
if ((log.OperationName.LocalizedValue == "Strat Virtual Machine") & (log.Status.LocalizedValue == "Succeeded"))
{
startTime.Add(log.EventTimestamp);
}
}
}
我正在尝试使用 Microsoft.Azure.Management.Fluent 和相关的软件包集,
我需要以下有关订阅中所有 Azure VM 的详细信息:
谁创建了 VM、VM 的区域、VmSize、VM 的当前状态(如已停止/运行/已解除分配等),
我也需要
VM 的历史记录,根据此 VM 启动的持续时间和最近 x 个月/周 运行。
这可以使用 Microsoft.Azure.Management.Fluent 包吗?
如果你想知道虚拟机的启动和停止时间,我们可以从Azure activity 日志中获取。关于如何获取activity日志,我们可以使用Microsoft.Azure.Management.Monitor.Fluent包。
例如
- 创建服务主体并将 Azure RABC 角色分配给 sp(我使用 Azure CLI)
az login
#it will create a service principal and assign contributor role to the sp
az ad sp create-for-rbac -n "jonsp2"
- 安装包
// for more details about the package, please refer to https://www.nuget.org/packages/Microsoft.Azure.Management.Fluent/
Install-Package Microsoft.Azure.Management.Fluent -Version 1.34.0
- 代码
AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
clientId, // the sp appId
clientSecret, // the sp password
tenantId, // the sp tenant
AzureEnvironment.AzureGlobalCloud);
var azure = Microsoft.Azure.Management.Fluent.Azure.Configure()
.Authenticate(credentials)
.WithSubscription(subscriptionId);
var vms = await azure.VirtualMachines.ListAsync();
foreach (var vm in vms)
{
var staus = vm.PowerState.Value; // vm power state
var region = vm.RegionName; // vm region
var size = vm.Size.Value; // vm size
var logs = await azure.ActivityLogs.DefineQuery()
.StartingFrom(DateTime.Now.AddDays(-1))
.EndsBefore(DateTime.Now)
.WithAllPropertiesInResponse()
.FilterByResource("/subscriptions/e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68/resourceGroups/jimtest/providers/Microsoft.Compute/virtualMachines/testvm")
.ExecuteAsync();
List<DateTime?> stopTime = new List<DateTime?>();
List<DateTime?> startTime = new List<DateTime?>();
foreach (var log in logs)
{
// get stop time
if ((log.OperationName.LocalizedValue == "Deallocate Virtual Machine") & (log.Status.LocalizedValue == "Succeeded"))
{
stopTime.Add(log.EventTimestamp);
}
// get start tim
if ((log.OperationName.LocalizedValue == "Strat Virtual Machine") & (log.Status.LocalizedValue == "Succeeded"))
{
startTime.Add(log.EventTimestamp);
}
}
}