2

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]

devd
  • 370
  • 10
  • 28
  • I think there's a problem with parsing `MediaLink` to get container name. Your container name is indeed invalid. It can't contain a period (.) - https://msdn.microsoft.com/en-us/library/azure/dd135715.aspx – Gaurav Mantri Aug 11 '15 at 10:22
  • @Gaurav Yes, you're right, but how to get the container name? I've edited the post and provided the output at the end. – devd Aug 11 '15 at 10:45
  • Can you share the value of `MediaLink`? – Gaurav Mantri Aug 11 '15 at 10:56
  • 1
    plz try with given url http://blogs.technet.com/b/canitpro/archive/2014/12/11/step-by-step-create-a-vm-snapshot-in-azure.aspx – subhash singh Aug 11 '15 at 10:56
  • @GauravMantri I am not sure if this is the value you're talking about Microsoft.WindowsAzure.Commands.ServiceManagement.Model.OSVirtualHardDisk.Medialink Got it using `$vmOSDisk = $vm | Get-AzureOSDisk` `$medialink = $vmOSDisk.Medialink` if not then I don't know how to get the value of `Medialink` – devd Aug 11 '15 at 11:09
  • 1
    I think MediaLink is an object of type Uri. What you could do is try to output that ... something like `Write-Output $vmOSDisk.MediaLink.AbsoluteUri`. I want to try your parsing logic essentially on the URL. – Gaurav Mantri Aug 11 '15 at 11:15
  • Value of Medialink.AbsoluteUri: `https://portalvhds14510n2y65vnh.blob.core.windows.net/vhds/a1smallvm-a1smallvm-2015-08-11.vhd` – devd Aug 11 '15 at 11:22
  • @subhashsingh That link I'd already seen. That doesn't work for me at all as I have to execute all this from a Linux machine. This script only supports Windows OS I suppose. – devd Aug 11 '15 at 12:03

1 Answers1

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.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • I was also thinking about that 'vhds' should be the container name...but when I execute the above cmdlet I am getting the error: `Error: Start-AzureStorageBlobCopy : Can not find blob 'a1smallvm-a1smallvm-2015-08-11.vhd' in container 'vhds'` – devd Aug 11 '15 at 11:46
  • 1
    Where are you setting storage account context for source and destination storage accounts? See this link for documentation - https://msdn.microsoft.com/en-us/library/dn806394.aspx. – Gaurav Mantri Aug 11 '15 at 14:20
  • What I've found is if I use the same storage account `$storageAccountName = $vmOSDisk.MediaLink.Host.Split(‘.’)[0]` to the create the container then everything is working. But what I want is to copy it into container in a different storage account. This link will be helpful. – devd Aug 11 '15 at 14:31