Public/Build-FMTImageFromVHD.ps1

function Build-FMTImageFromVHD {
    param(
        [parameter(Mandatory)]
        [string] $targetResourceGroup,
        [parameter(Mandatory)]
        [string] $storageAccount,
        [parameter(Mandatory)]
        [string] $storageContainer,
        [parameter()]
        [string] $cnodeVersion,
        [parameter()]
        [string] $dnodeVersion
    )
    # Check AZ context

    $session = New-FMTSession
    if (!$session) {
        $message = '-- stopping --'
        return $message
    }

    if (!$cnodeVersion -and !$dnodeVersion) {
        Write-Host -ForegroundColor yellow "No image versions specified, please set -cnodeVersion or -dnodeVersion (or both)"
        pause
    }

    $rg = Get-AzResourceGroup -Name $targetResourceGroup -ErrorAction SilentlyContinue
    if (!$rg) {
        Write-Host -ForegroundColor yellow "Resource group -- $targetResourceGroup -- Not found. Please check and try again."
        Pause
    }
    $Location = $rg.Location

    # Parse stated version
  
    if ($cnodeVersion) {
        $cImageName = "k2c-cnode-" + $cnodeVersion.Replace('.','-')
        $cImageFileName = $cImageName + ".vhd"
        $currentCImage = Get-AzImage -Name $cimageName -ResourceGroupName $targetResourceGroup -ErrorAction SilentlyContinue
        if ($currentCImage) {
            Write-Host -ForegroundColor yellow "Image -- $cImageFileName -- Already exists in resource group -- $targetResourceGroup."
        }
    }

    if ($dnodeVersion) {
        $dImageName = "azure-dnode-" + $dnodeVersion.Replace('.','-')
        $dImageFileName = $dImageName + ".vhd"
        $currentDImage = Get-AzImage -Name $dimageName -ResourceGroupName $targetResourceGroup -ErrorAction SilentlyContinue
        if ($currentDImage) {
            Write-Host -ForegroundColor yellow "Image -- $dImageFileName -- Already exists in resource group -- $targetResourceGroup."
        }
    }

   
    $sa = Get-AzStorageAccount -ResourceGroupName $rg.ResourceGroupName -Name $storageAccount -ErrorAction SilentlyContinue

    $sc = $sa | Get-AzStorageContainer -Name $StorageContainer -ErrorAction SilentlyContinue

    if ($cnodeVersion) {
        # Create cnode Image
        $cimageURI = $sc.CloudBlobContainer.Uri.AbsoluteUri + '/' + $cimageFileName
        $cimageConfig = New-AzImageConfig -Location $Location
        $cimageConfig = Set-AzImageOsDisk -Image $cimageConfig -OsType Linux -OsState Generalized -BlobUri $cimageURI
        New-AzImage -ImageName $cimageName -ResourceGroupName $rg.ResourceGroupName -Image $cimageConfig
    }

    if ($dnodeVersion) {
        # Create dnode Image
        $dimageURI = $sc.CloudBlobContainer.Uri.AbsoluteUri + '/' + $dimageFileName
        $dimageConfig = New-AzImageConfig -Location $Location
        $dimageConfig = Set-AzImageOsDisk -Image $dimageConfig -OsType Linux -OsState Generalized -BlobUri $dimageURI
        New-AzImage -ImageName $dimageName -ResourceGroupName $rg.ResourceGroupName -Image $dimageConfig
    }
}