Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.21k stars 3.81k forks source link

New-AzVmss from -SharedGalleryImageId param is completely broken #22559

Open jerryc3 opened 1 year ago

jerryc3 commented 1 year ago

Description

Powershell command:

New-AzVmss -Credential <vm-cred> -VMScaleSetName <vmss-name> -SharedGalleryImageId "/SharedGalleries/WindowsServer.1P.Canary/images/2022-datacenter-azure-edition/versions/latest" -SecurityType "TrustedLaunch" -EnableSecureBoot $true -EnableVtpm $true -Location "eastus2euap"

results in VMSS request entity of:

    "imageReference": {
      "publisher": "MicrosoftWindowsServer",
      "offer": "WindowsServer",
      "sku": "2016-Datacenter",
      "version": "latest"
    },

The expected entity is

    "imageReference": {
      "sharedGalleryImageId": "/SharedGalleries/WindowsServer.1P.Canary/images/2022-datacenter-azure-edition/versions/latest"
    },

Bug code location: https://github.com/Azure/azure-powershell/blob/main/src/Compute/Compute/Strategies/ComputeRp/VirtualMachineScaleSetStrategy.cs#L117C52-L117C52

Since SharedGalleryImageId is provided, ImageReferenceId is null. This results ImageReference being set to imageAndOsType?.Image and sharedGalleryImageId is not set.

Issue script & Debug output

`New-AzVmss -Credential <vm-cred> -VMScaleSetName <vmss-name> -ImageReferenceId "/SharedGalleries/WindowsServer.1P.Canary/images/2022-datacenter-azure-edition/versions/latest" -SecurityType "TrustedLaunch" -EnableSecureBoot $true -EnableVtpm $true  -Location "eastus2euap"   
`

Environment data

PS C:\windows\system32> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.22621.1778
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22621.1778
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Module versions

Az.Compute 6.1.0

Error output

No response

TravisCragg-MSFT commented 1 year ago

@jerryc3 Thank you for reporting this issue. We are triaging and will get back to you as soon as we are able.

haagha commented 1 year ago

I see a SharedGalleryImageId parameter was added in add sharedGalleryImageId to VM/VMSS by grizzlytheodore · Pull Request #21139 · Azure/azure-powershell (github.com)

But in your issue you are using -ImageReferenceId "/SharedGalleries/WindowsServer.1P.Canary/images/2022-datacenter-azure-edition/versions/latest"

Is this what you intended? And if yes, then why did we add the sharedgalleryimageId parameter?

jerryc3 commented 10 months ago

Hey @haagha ,

I think the example was wrong in the original issue. the issue is pertaining to the "SharedGalleryImageId" parameter

if -sharedGalleryImageId is provided, it should map as follows:

"storageProfile": { "imageReference": { "sharedGalleryImageId": "/SharedGalleries/...." } }

jerryc3 commented 10 months ago

the current code has a bug and imageReferenceId == null when sharedGalleryImageId is passed. It ends up never getting used : }, StorageProfile = new VirtualMachineScaleSetStorageProfile { ImageReference = (imageReferenceId == null) ? imageAndOsType?.Image : (imageReferenceId.ToLower().StartsWith("/communitygalleries/") ? new ImageReference { CommunityGalleryImageId = imageReferenceId, SharedGalleryImageId = sharedImageGalleryId } : new ImageReference { Id = imageReferenceId, SharedGalleryImageId = sharedImageGalleryId }), DataDisks = DataDiskStrategy.CreateVmssDataDisks( imageAndOsType?.DataDiskLuns, dataDisks), DiskControllerType = diskControllerType },

mfortin commented 6 months ago

Also having this issue

$VmConfig = New-AzVMConfig -VMName testvm -VMSize Standard_B2s -SharedGalleryImageId /SharedGalleries/<sub>-<gallery>/Images/<definition/Versions/<version> -Location WestUS2
Write-Output $VmConfig.StorageProfile.ImageReference

Publisher               :
Offer                   :
Sku                     :
Version                 :
ExactVersion            :
SharedGalleryImageId    : /SharedGalleries/<sub>-<gallery>/Images/<definition/Versions/<version>
CommunityGalleryImageId :
Id                      : 

And when I launch the VM

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Azure.Commands.Compute.NewAzureVMCommand.retrieveImageVersion(String publisher, String offer, String sku, String version)
   at Microsoft.Azure.Commands.Compute.NewAzureVMCommand.retrieveSpecificImageFromNotId()
   at Microsoft.Azure.Commands.Compute.NewAzureVMCommand.DefaultExecuteCmdlet()
   at Microsoft.Azure.Commands.Compute.NewAzureVMCommand.ExecuteCmdlet()
   at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
System.Management.Automation.InvocationInfo

Seems to happen because no ID and no Publisher/Offer/Sku

mfortin commented 5 months ago

@TravisCragg-MSFT @haagha @isra-fel Any update ? I believe it is related to this: https://github.com/Azure/azure-powershell/blob/main/src/Compute/Compute/VirtualMachine/Operation/NewAzureVMCommand.cs#L966-L967

if (this.VM.StorageProfile?.ImageReference?.Id != null)
{
mfortin commented 5 months ago

https://github.com/Azure/azure-powershell/pull/24413 This should fix it. I have build the Az.Compute module with this PR and tested it, it works as expected.

mfortin commented 5 months ago

up

mfortin commented 4 months ago

@TravisCragg-MSFT @haagha @isra-fel ?

hilaryw29 commented 1 month ago

Hi @mfortin, sorry for the delayed response. Was #24413 able to fix? Or are you still running into this issue?