简体   繁体   中英

Creating snapshot of VM in Azure

Having followed this article( http://www.coreazure.com/snapshot-vms-in-azure-2/ ) I am trying to create a snapshot of VM using powershell(PS) from Azure portal. This is the PS script which I have created to take a snapshot

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
      }    
  }    
}

The cmdlet

Start-AzureStorageBlobCopy -SrcContainer $vmOSContainerName -SrcBlob $vmOSBlobName -DestContainer $Using:storageContainer

throws an error:

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.

The container name 'a1smallvm-a1smallvm-2015-08-11.vhd' which I am getting follows the correct naming format but still why it's giving an error saying the name is invalid. The VM was created from the portal, it's an A1 type of VM, OS is CentOS "OpenLogic 6.5". Any clue what's wrong?

The following are the outputs from Write-output's

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

Correct script: The storage account name has to be same or we will have to add the context of destination storage account.

$storageAccountName = $vmOSDisk.MediaLink.Host.Split('.')[0] Set-AzureSubscription -SubscriptionName $subName -CurrentStorageAccount '$storagenameAccountName'

And index for Segments should be -2 not -1 $vmOSContainerName = $vmOSDisk.MediaLink.Segments[-2].Split('/')[0]

I'm no PowerShell expert (and I am sure there are better ways of doing it) but you could do the following:

$vmOSContainerName = $vmOSDisk.MediaLink.AbsolutePath.Split('/')[1]

This will output vhds which is the name of your blob container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM