遵循本文(http://www.coreazure.com/snapshot-vms-in-azure-2/),我试图从Azure门户使用powershell(PS)创建虚拟机的快照。这是我创建的用于拍摄快照的PS脚本
workflow snapshot1
{
$subName = 'XYZ'
$cred = Get-AutomationPSCredential -Name "xyz@xyzgmail.onmicrosoft.com"
Add-AzureAccount -Credential $cred
Set-AzureSubscription -SubscriptionName $subName -CurrentStorageAccount 'storagename'
$serviceName = "a1smallvm"
$vm = Get-AzureVM –ServiceName $serviceName –Name "a1smallvm"
$storageContainer = "backups"
InlineScript {
# Create a storage for putting the backups of OSDisk & DataDisks
New-AzureStorageContainer -Name $Using:storageContainer -Permission off
# Stop VM if running
$Using:vm | Stop-AzureVM -StayProvisioned
$vm = Get-AzureVM –ServiceName $Using:serviceName –Name "a1smallvm"
$vmOSDisk = $vm | Get-AzureOSDisk
$vmDataDisks = $vm | Get-AzureDataDisk
Write-output "OSDisk: $vmOSDisk"
$storageAccountName = $vmOSDisk.MediaLink.Host.Split(‘.’)[0]
Write-output "Data Disk: $vmDataDisks"
Write-output "StorageAccountName: $storageAccountName"
$vmOSBlobName = $vmOSDisk.MediaLink.Segments[-1]
$vmOSContainerName = $vmOSDisk.MediaLink.Segments[-1].Split(‘/’)[0]
Write-output "vmOSBlobName: $vmOSBlobName"
Write-output "vmOSContainerName: $vmOSContainerName"
# Backup the osblob and oscontainer
Start-AzureStorageBlobCopy -SrcContainer $vmOSContainerName -SrcBlob $vmOSBlobName -DestContainer $Using:storageContainer
Get-AzureStorageBlobCopyState -Container $Using:storageContainer -Blob $vmOSBlobName -WaitForComplete
# Backup the dataBlob and dataContainer
ForEach ($vmDataDisk in $vmDataDisks) {
$vmDataBlobName = $vmDataDisk.MediaLink.Segments[-1]
$vmDataContainerName = $vmDataDisk.MediaLink.Segments[-2].Split(‘/’)[0]
Start-AzureStorageBlobCopy -SrcContainer $vmDataContainerName -SrcBlob $vmDataBlobName -DestContainer backups -Force
Get-AzureStorageBlobCopyState -Container backups -Blob $vmDataBlobName -WaitForComplete
}
}
}
cmdlet
Start-AzureStorageBlobCopy -SrcContainer $vmOSContainerName -SrcBlob $vmOSBlobName -DestContainer $Using:storageContainer
抛出错误:
Error: Start-AzureStorageBlobCopy : Container name 'a1smallvm-a1smallvm-2015-08-11.vhd' is invalid. Valid names start and end with a lower case letter or a number and has in between a lower case letter, number or dash with no consecutive dashes and is 3 through 63 characters long.
容器名称'a1smallvm-a1smallvm-2015-08-11。我得到的vhd'遵循正确的命名格式,但仍然为什么它给出一个错误,说名称无效。虚拟机是从门户创建的,它是A1类型的虚拟机,操作系统是CentOS"OpenLogic 6.5"。知道怎么了吗?
下面是Write-output
的输出OSDisk: Microsoft.WindowsAzure.Commands.ServiceManagement.Model.OSVirtualHardDisk
Data Disk:
StorageAccountName: portalvhds14510n2y65vnh
vmOSBlobName: a1smallvm-a1smallvm-2015-08-11.vhd
vmOSContainerName: a1smallvm-a1smallvm-2015-08-11.vhd
正确的脚本:存储帐户名称必须相同,否则我们将不得不添加目标存储帐户的上下文。
$storageAccountName = $vmOSDisk.MediaLink.Host.Split(‘.’)[0]
Set-AzureSubscription -SubscriptionName $subName -CurrentStorageAccount '$storagenameAccountName'
segment的索引应该是-2而不是-1$vmOSContainerName = $vmOSDisk.MediaLink.Segments[-2].Split(‘/’)[0]
我不是PowerShell专家(我相信有更好的方法),但你可以这样做:
$vmOSContainerName = $vmOSDisk.MediaLink.AbsolutePath.Split('/')[1]
这将输出vhds
,这是您的blob容器的名称。