是否有任何管理API可以查找Azure VM创建日期?为云服务创建日期提供了托管服务属性API,但我们无法找到Azure VM创建日期。
答案 0 :(得分:2)
答案 1 :(得分:1)
我们可以在Blob存储中检索操作系统磁盘文件创建时间,该时间相当于虚拟机创建时间。查看更多https://social.msdn.microsoft.com/forums/azure/en-US/3da7750a-1a7d-4c62-b58a-a4b427b2520d/get-azure-vm-creationprovision-date
答案 2 :(得分:1)
2016更新:仍然无法以可靠的方式快速获取VM创建日期(不,磁盘名称的日期部分不可靠)。
答案 3 :(得分:1)
我最近为您可能会发现有用的这种东西编写了一个函数。
该功能仅需要您的tenantID和具有正确权限的身份,并将返回有关Azure VM的创建日期,它们所在的资源组,操作系统和SKU之后的数据。
功能如下:
Function Get-AZVMCreated {
<#
.SYNOPSIS
Function "Get-AZVMCreated" will connect to a given tenant and parse through the subscriptions and output Azure VM details based on creation date.
.DESCRIPTION
Author: Pwd9000 (Pwd9000@hotmail.co.uk)
PSVersion: 5.1
The user must specify the TenantId when using this function.
The function will request access credentials and connect to the given Tenant.
Granted the identity used has the required access permisson the function will parse through all subscriptions
and gather data on Azure Vms based on the creation date.
.EXAMPLE
Get-AZVMCreated -TenantId "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
.PARAMETER TenantId
A valid Tenant Id object. e.g: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" <String>
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $True,
ValueFromPipeline = $True,
HelpMessage = 'Please specify the tenant id? e.g: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"')]
[string]$TenantId
)
#------------------------------------------------Obtain Credentials for Session------------------------------------------------------------
$Credential = Get-Credential
#---------------------------------------------Get all Subscription Ids for given Tenant----------------------------------------------------
$SubscriptionIds = (Get-AzureRmSubscription -TenantId $TenantId).Id
#-------------------------------------------------Create Empty Table to capture data-------------------------------------------------------
$Table = @()
Foreach ($Subscription in $SubscriptionIds) {
Write-Host "Checking Subscription: $Subscription. for any Azure VMs and their creation date. This process may take a while. Please wait..." -ForegroundColor Green
$RMAccount = Add-AzureRmAccount -Credential $Credential -TenantId $TenantId -Subscription $subscription
Get-AzureRmDisk | Where-Object {$_.TimeCreated -le (Get-Date)} |
Select-Object Name, ManagedBy, Resourcegroupname, TimeCreated |
ForEach-Object {
Try {
$ErrName = $_.Name
$AzDiskManagedBy = $_.managedby | Split-path -leaf
$AzDiskManagedByRG = $_.ResourceGroupName
$CreationDate = $_.TimeCreated
$OS = (Get-AzurermVM -name $AzDiskManagedBy -ResourceGroup $AzDiskManagedByRG).StorageProfile.ImageReference.Offer
$SKU = (Get-AzurermVM -name $AzDiskManagedBy -ResourceGroup $AzDiskManagedByRG).StorageProfile.ImageReference.SKU
$Table += [pscustomobject]@{VMName = $AzDiskManagedBy; Created = $CreationDate; ResourceGroup = $AzDiskManagedByRG; OperatingSystem = $OS; SKU = $SKU}
}
Catch {
Write-Host "Cannot determine machine name associated with disk: [$ErrName]. Skipping drive-check for this item..." -ForegroundColor Yellow
Write-Host "Continue Checking Subscription: $Subscription. for any Azure VMs and their creation date. This process may take a while. Please wait..." -ForegroundColor Green
}
}
}
$UniqueVMs = $Table | Sort -Unique -Property VMName
$UniqueVMs
Write-Host "" -ForegroundColor Green
Write-Host "Number of disks associated with VMs: $($Table.Count)" -ForegroundColor Green
Write-Host "Number of disks unable to associate with VMs: $($ErrName.Count)" -ForegroundColor Yellow
Write-Host "Number of unique Azure VMs associated with disks: $($UniqueVMs.Count)" -ForegroundColor Green
Write-Host "Script finished.." -ForegroundColor Green
}
然后您可以调用并使用如下功能,例如:
$TenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$AZVMsAll = Get-AZVMCreated -TenantId $TenantId | sort-object -property Created
$Win10 = $AZVMsAll | Where-Object {$_.SKU -like "*Windows-10*"} | sort-object -property Created
$Win8 = $AZVMsAll | Where-Object {$_.SKU -like "*Win81*"} | sort-object -property Created
$Win7 = $AZVMsAll | Where-Object {$_.SKU -like "*Win7*"} | sort-object -property Created
$Server2008R2 = $AZVMsAll | Where-Object {$_.SKU -like "*2008-R2*"} | sort-object -property Created
$Server2012R2 = $AZVMsAll | Where-Object {$_.SKU -like "*2012-R2*"} | sort-object -property Created
$Server2016 = $AZVMsAll | Where-Object {$_.SKU -like "*2016*"} | sort-object -property Created
$RHEL = $AZVMsAll | Where-Object {$_.OperatingSystem -like "*RHEL*"} | sort-object -property Created
$Ubuntu = $AZVMsAll | Where-Object {$_.OperatingSystem -like "*Ubuntu*"} | sort-object -property Created
$Centos = $AZVMsAll | Where-Object {$_.OperatingSystem -like "*Centos*"} | sort-object -property Created
$AZVMsAll
在上面的示例中,您还可以使用定义的变量调用其他版本的OS,例如对于所有Win7机器,您可以调用变量$ Win7等,等等……
答案 4 :(得分:0)
使用以下az命令获取磁盘创建日期...从此OS磁盘创建中,我们可以找到VM的提供日期:
diskquery='[*].{Resourceid:id, name:name, timeCreated:timeCreated}'
az disk list --query "$diskquery"
答案 5 :(得分:-1)
通过我所知道的Windows Azure API无法获得此功能。您是否尝试远程获取此值,或者将其用于实例?
如果您在实例上,可能会尝试基于WMI获取它。这是在PowerShell中,但您可以适应C#,VB.NET或其他可以获取WMI信息的Scripting语言。
[reflection.Assembly]::LoadWithPartialName("system.management")
$wmiSearch = new-object -type System.Management.ManagementObjectSearcher -Arg "Select * from Win32_OperatingSystem"
$data = $wmiSearch.Get()
[System.Management.ManagementDateTimeConverter]::ToDateTime($data.InstallDate)
我在Azure VM上尝试了这个,它基本上就是为我创建了VM。您可能希望随着时间的推移测试此结果。此外,这只会让您获得虚拟机实际启动的时间。因此,如果您有一个运行良好的实例A,那么硬盘驱动器就会出现故障,并且Fabric Controller将实例移动到DC中的另一个位置并将其旋转回来,然后安装日期可能就是它的时间移动。这可能正是您正在寻找的,或者可能不依赖于您的要求。