是否有任何管理 API 来查找 Azure VM 创建日期?有获取云服务创建日期的托管服务属性 API,但我们无法找到 Azure VM 的创建日期。
6 回答
我们可以在 Blob 存储中检索操作系统磁盘文件的创建时间,它相当于虚拟机的创建时间。查看更多https://social.msdn.microsoft.com/forums/azure/en-US/3da7750a-1a7d-4c62-b58a-a4b427b2520d/get-azure-vm-creationprovision-date
2016 更新:仍然没有办法以可靠的方式快速获取 VM 创建日期(不,磁盘名称的日期部分不可靠)。
我最近为这种你可能会觉得有用的东西写了一个函数。
该函数只需要您的租户 ID 和具有正确权限的身份,并将返回您之后的有关 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
在上面的示例中,您还可以使用定义的变量调用其他版本的操作系统,例如对于所有 Win7 机器,您可以调用变量 $Win7 等等等...
使用以下 az 命令获取磁盘创建日期...从这个 OS 磁盘创建中,我们可以找到 VM 提供日期:
diskquery='[*].{Resourceid:id, name:name, timeCreated:timeCreated}'
az disk list --query "$diskquery"
没有办法通过我知道的 Windows Azure API 来获得这个。您是尝试远程获取此值,还是获取它以在实例上使用?
如果您在实例上,您可能会尝试基于 WMI 获取它。它在 PowerShell 中,但您可以适应 C#、VB.NET 或其他可以获取 WMI 信息的脚本语言。
[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 控制器将实例移动到 DC 中的另一个位置并重新启动它,那么安装日期可能就是它的时间感动。这可能正是您正在寻找的,或者可能不取决于您的要求。