#Test if the azure powershell modules are present on the system $scmd="Login-AzureRmAccount" $cmdout=Get-Command $scmd -eA SilentlyContinue -EV $serr -OV $sout if(!$cmdout.CommandType) { echo "Required powershell modules are missing. Please install the azure modules and retry" exit } #Sign in for this session Login-AzureRmAccount #Select the subscription on which image will be deployed $SubSelect = 'n' Do { $subs=Get-AzureRmSubscription echo "Listing available subscriptions in your account" $subid=0 $ProvisionSub=99999 foreach ($sub in $subs) { echo "Subscription $subid :" echo $sub $subid++ } if($subid -ge 1) { $ProvisionSub=Read-Host -Prompt "Select one of the above for provisioning" } else { $ProvisionSub=0 } echo "Selected subscription for provisioning :" echo $subs[$ProvisionSub] echo "Enter `"y`" to continue with this subscription or `"n`" to choose again" $SubSelect=Read-Host } While($SubSelect -eq 'n' -or $SubSelect -eq 'N') if($SubSelect -ne 'y' -and $SubSelect -ne 'Y') { echo "You did not choose a subscription to deploy in, script will exit now" exit } $subscription=$subs[$ProvisionSub] echo "Script will continue to provision in the selected subscription $subscription " Set-AzureRmContext -SubscriptionId $subscription.Id echo "Azure Subscription for current session set to the following" Get-AzureRmContext echo "Do you wish to continue(y/n):" $select=Read-Host if($select -eq 'n' -or $select -eq 'N') { echo "Script terminating per user input" exit } echo "Provisioning will continue with the selected subscription" #Fetch the config file to be loaded if( $args[0] -ne $null ){ $filename=$args[0] } else { $filename="./copyconfig.txt" } if ( -not (Test-Path $filename)) { echo "Config file not found at $filename" exit } echo "Found the file, populating variables" #initialize the variables $osdisk='' $datadisk='' $sastok='' $storageac='' $dest='' $rgname='' $prefix='' $location='eastus' foreach ($line in Get-Content $filename) { $entries=$line.split("=",2,[StringSplitOptions]'RemoveEmptyEntries') #$entries=$line.split("=") $e1=$entries[0] $e2=$entries[1] $key=$e1.Trim() $value=$e2.Trim() echo "Got entries" $entries[0] "->" $entries[1] if($key -eq "osdisk") { $osdisk=$value continue } if($key -eq "location") { $location=$value continue } if($key -eq "datadisk") { $datadisk=$value continue } if($key -eq "store") { $storageac=$value continue } if($key -eq "rgname") { $rgname=$value continue } if($key -eq "dest") { $dest=$value continue } if($key -eq "sastok") { $sastok=$value continue } if($key -eq "name") { $prefix=$value continue } } #validate resource group $rg=Get-AzureRmResourceGroup -ResourceGroupName $rgname -ev notPresent -ea 0 $store=Get-AzureRmStorageAccount -ResourceGroupName $rgname -Name $storageac -ev stnotPresent -ea 0 if($rg.ProvisioningState -ne "Succeeded") { echo "Resource Group, $rgname is not provisioned. Script will exit now" exit } if($store.ProvisioningState -ne "Succeeded") { echo "Storage account $storageac is not provisioned" exit } $context=$store.Context $sasosurl=$osdisk+$sastok $sasddurl=$datadisk+$sastok $osname=$prefix+"_osdisk.vhd" $dataname=$prefix+"_datadisk.vhd" #validate target container $container=Get-AzureStorageContainer -Context $context -Name $dest -ev ctrStr -ea 0 if($ctrStr) { echo "The destination container configured is not available. Exiting" exit } #Start copy of the osdisk Start-AzureStorageBlobCopy -AbsoluteUri $sasosurl -DestContainer $dest -DestContext $context -DestBlob $osname Get-AzureStorageBlobCopyState -Context $context -Blob $osname -Container $dest #Start copy of the datadisk Start-AzureStorageBlobCopy -AbsoluteUri $sasddurl -DestContainer $dest -DestContext $context -DestBlob $dataname Get-AzureStorageBlobCopyState -Context $context -Blob $dataname -Container $dest clear #$status=Get-AzureStorageBlobCopyState -Context $context -Blob $osname -Container $dest #Start-Sleep -s 100 Get-AzureStorageBlobCopyState -Context $context -Blob $osname -Container $dest $osstatus=Get-AzureStorageBlobCopyState -Context $context -Blob $osname -Container $dest echo "Disk copy in progress" Start-Sleep -s 30 While($osstatus.Status -ne "Success") { if($osstatus.Status -ne "Pending") { echo "Error copying the osdisk" exit } $osstatus=Get-AzureStorageBlobCopyState -Context $context -Blob $osname -Container $dest echo "OS disk copy in progress" $osstatus.BytesCopied "bytes copied of" $osstatus.TotalBytes sleep 10 clear } echo "OS disk copy completed" $ddstatus=Get-AzureStorageBlobCopyState -Context $context -Blob $dataname -Container $dest While($ddstatus.Status -ne "Success") { if($ddstatus.Status -ne "Pending") { echo "Data disk copy failed" exit } $ddstatus=Get-AzureStorageBlobCopyState -Context $context -Blob $dataname -Container $dest echo "Data disk copy in progress" $ddstatus.BytesCopied "Bytes copied of" $ddstatus.TotalBytes sleep 10 clear } #Create the VM image in the destination account $osVhdUri=$context.BlobEndPoint+$dest+"/"+$osname $imageName=$prefix+"_image" $imageConfig = New-AzureRmImageConfig -Location $location $imageConfig = Set-AzureRmImageOsDisk -Image $imageConfig -OsType Linux -OsState Generalized -BlobUri $osVhdUri $image = New-AzureRmImage -ImageName $imageName -ResourceGroupName $rgname -Image $imageConfig