2
votes

I am trying to restore a VM (http://blogs.technet.com/b/keithmayer/archive/2014/02/04/step-by-step-perform-cloud-restores-of-windows-azure-virtual-machines-using-powershell-part-2.aspx) which requires to export the VM config before deleting the VM. Now, I am trying to achieve all this through a runbook. The Export-AzureVM saves the config details in a file in the local machine using Windows PowerShell from that machine. Now, since I am running this in Azure portal is there a way to save the config file in Azure Powershell?

EDIT: This is working as expected in Azure portal, but from where it's getting C: drive I am not sure.

$exportedFile = "C:\file.xml"
New-Item -Path $exportFolder -ItemType Directory
$exportPath = $exportFolder + "\" + $vm.Name + ".xml"
$vm | Export-AzureVM -Path $exportPath

Output:

Directory: C:\
Mode            LastWriteTime        Length      Name             PSComputerName                       
----            ---------            ------      ----             ----------                       
d----           8/20/2015   2:15 PM             ExportVMs         localhost                            
1

1 Answers

3
votes

You have to copy your exported files to some place that you can access from any place. Of course you can chose from all file storage services that exist in the internet. My first approach would it be to copy those exported files to an Azure Storage account.

Here is some sample PowerShell code which shows how to copy files to an Azure Storage account. That sample even creates a new Storage account. If you already have an Azure Storage account to use, just remove the lines that create a new one.

# Used settings
$subscriptionName = "Your Subscription Name" # Get-AzureSubscription
$location = "West Europe" # Get-AzureLocation
$storageAccountName = "mystorageaccount123"

# Create storage account and set is as current.
New-AzureStorageAccount -Location $location -StorageAccountName $storageAccountName -Type Standard_LRS
Set-AzureSubscription -SubscriptionName $subscriptionName -CurrentStorageAccountName $storageAccountName

$container = "exportedfiles"

# Create destination container in storage if it does not exist.
$containerList = Get-AzureStorageContainer -Name $container -ErrorAction Ignore # Ignore error if container not found.
if ($containerList.Length -eq 0) {
    New-AzureStorageContainer -Name $container -Permission Off
}

$exportedFile = "C:\file.xml" # That is something you should know where you have exported your file.

# Upload PowerShell file
Set-AzureStorageBlobContent -Container $container -File $exportedFile -Force

I took the sample code from my own sample code on GitHub Gist and adapted it to your question. You can find the whole sample here, if you like.

If you need a tool to access an Azure Storage account, see this list. There are some good tools. I personally use ClumsyLeaf CloudXplorer.