Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New-AzureRMVM Object reference not set to an instance of an object - previously working script #6847

Closed
jeanfrancoislarente opened this issue Aug 3, 2018 · 11 comments
Assignees
Labels
bug This issue requires a change to an existing behavior in the product in order to be resolved. Compute - Managed Disks Service Attention This issue is responsible by Azure service team.

Comments

@jeanfrancoislarente
Copy link

Currently on 6.6.0

Had a script to create a new VM from a stored VHD (been using for ~8 months now).

I'm all of a sudden getting New-AzureRmVM : Object reference not set to an instance of an object.

Omitting the creation of RG, networking, etc. Below is the script to create OS disk and VM.

Has anything changed in the latest releases?

Write-Host "Creating OS Disk" -ForegroundColor Green
# OS Disk
New-AzureRmDisk -DiskName $osDiskName -Disk `
(New-AzureRmDiskConfig -AccountType Premium_LRS  `
        -Location $location -CreateOption Import `
        -StorageAccountId $storageAccountId `
        -SourceUri $osVHDUri) `
    -ResourceGroupName $resourceGroupName
$osDisk = Get-AzureRMDisk -DiskName $osDiskName -ResourceGroupName $resourceGroupName

Write-Host "Setting VM Configuration" -ForegroundColor Green
#Initialize virtual machine configuration
$VirtualMachine = New-AzureRmVMConfig -VMName $virtualMachineName -VMSize $virtualMachineSize
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -ManagedDiskId $osDisk.Id -CreateOption Attach -Windows
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $nic.Id

#Create the virtual machine with Managed Disk
Write-Host "Creating Virtual Machine" -ForegroundColor Green
New-AzureRmVM -VM $VirtualMachine -ResourceGroupName $resourceGroupName -Location $location -Verbose

Write-Host "Enabling Auto-Shutdown" -ForegroundColor Green
Enable-AzureRMVmAutoShutdown -SubscriptionId $subscriptionId -ResourceGroupName $resourceGroupName -VirtualMachineName $virtualMachineName -TimeZone $timeZone


### Debug output

``` DEBUG: AzureQoSEvent: CommandName - New-AzureRmVM; IsSuccess - False; Duration - 00:01:15.8115447; Exception - System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Azure.Management.Compute.Models.StorageAccountTypes.ToString()
   at Microsoft.Azure.Management.Compute.Models.StorageAccountTypesConverter.WriteJson(JsonWriter writer, Object value, JsonSerializer serializer)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter writer, JsonConverter converter, Object value, JsonContract contract, JsonContainerContract collectionContract, JsonProperty
containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty
containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty
containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty
containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.Linq.JToken.FromObjectInternal(Object o, JsonSerializer jsonSerializer)
   at Microsoft.Rest.Serialization.TransformationJsonConverter.WriteJson(JsonWriter writer, Object value, JsonSerializer serializer)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter writer, JsonConverter converter, Object value, JsonContract contract, JsonContainerContract collectionContract, JsonProperty
containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
   at Microsoft.Rest.Serialization.SafeJsonConvert.SerializeObject(Object obj, JsonSerializerSettings settings)
   at Microsoft.Azure.Management.Compute.VirtualMachinesOperations.<BeginCreateOrUpdateWithHttpMessagesAsync>d__24.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Management.Compute.VirtualMachinesOperations.<CreateOrUpdateWithHttpMessagesAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Commands.Compute.NewAzureVMCommand.<DefaultExecuteCmdlet>b__114_0()
   at Microsoft.Azure.Commands.Compute.ComputeClientBaseCmdlet.ExecuteClientAction(Action action)
   at Microsoft.Azure.Commands.Compute.NewAzureVMCommand.DefaultExecuteCmdlet()
   at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord(); ```
@markcowl
Copy link
Member

markcowl commented Aug 3, 2018

@jeanfrancoislarente Thanks for the report, cna you also provide the output when executing Resolve-AzureRmError -Last immdeiately after the error?

@singhkays @hyonholee can you take a look?

@JVSemmelrock
Copy link

Issue with error "Object reference not set to an instance of an object." also applies to Get-AzureRmVm. I had a prior working script suddenly start failing after 6.6.0 update and after discovering that the example powershell at https://docs.microsoft.com/en-us/azure/virtual-machines/windows/attach-disk-ps also now fails with the same error i rolled back to an earlier module and it succeeded.

The only time Update-AzureRmVm would acccept the vm object on 6.6.0 was via a pipestream, but, at least in the adding-a-data-disk flow, something still wasn't actually functioning.

@jeanfrancoislarente
Copy link
Author

@singhkays @hyonholee @markcowl

Here is the error for New-AzureRMVm



Message        : Object reference not set to an instance of an object.
StackTrace     :    at Microsoft.Azure.Management.Compute.Models.StorageAccountTypes.ToString()
                    at Microsoft.Azure.Management.Compute.Models.StorageAccountTypesConverter.WriteJson(JsonWriter writer, Object value, JsonSerializer serializer)
                    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter writer, JsonConverter converter, Object value, JsonContract contract, JsonContainerContract
                 collectionContract, JsonProperty containerProperty)
                    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract,
                 JsonProperty containerProperty)
                    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract,
                 JsonProperty containerProperty)
                    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract,
                 JsonProperty containerProperty)
                    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
                    at Newtonsoft.Json.Linq.JToken.FromObjectInternal(Object o, JsonSerializer jsonSerializer)
                    at Microsoft.Rest.Serialization.TransformationJsonConverter.WriteJson(JsonWriter writer, Object value, JsonSerializer serializer)
                    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter writer, JsonConverter converter, Object value, JsonContract contract, JsonContainerContract
                 collectionContract, JsonProperty containerProperty)
                    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
                    at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
                    at Microsoft.Rest.Serialization.SafeJsonConvert.SerializeObject(Object obj, JsonSerializerSettings settings)
                    at Microsoft.Azure.Management.Compute.VirtualMachinesOperations.<BeginCreateOrUpdateWithHttpMessagesAsync>d__24.MoveNext()
                 --- End of stack trace from previous location where exception was thrown ---
                    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                    at Microsoft.Azure.Management.Compute.VirtualMachinesOperations.<CreateOrUpdateWithHttpMessagesAsync>d__6.MoveNext()
                 --- End of stack trace from previous location where exception was thrown ---
                    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                    at Microsoft.Azure.Commands.Compute.NewAzureVMCommand.<DefaultExecuteCmdlet>b__114_0()
                    at Microsoft.Azure.Commands.Compute.ComputeClientBaseCmdlet.ExecuteClientAction(Action action)
                    at Microsoft.Azure.Commands.Compute.NewAzureVMCommand.DefaultExecuteCmdlet()
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
Exception      : System.NullReferenceException
InvocationInfo : {New-AzureRmVM}
Line           : New-AzureRmVM -VM $VirtualMachine -ResourceGroupName $resourceGroupName -Location $location

Position       : At C:\Projects\gh.s.hh.utils\Azure\CreateVMFromVhd.ps1:176 char:1
                 + New-AzureRmVM -VM $VirtualMachine -ResourceGroupName $resourceGroupNa ...
                 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 200```

@jeanfrancoislarente
Copy link
Author

Read another thread about storage account type names changing with/without the underscore. Since the disk is being created as Premium, is it possible that the New-AzureRMVm command, which is trying to create boot diagnostics in basic storage is using the wrong name for the "standard" storage account type?

@panchagnula panchagnula added App Services aka WebSites and removed App Services aka WebSites labels Aug 7, 2018
@jeanfrancoislarente
Copy link
Author

Works correctly in 6.3.0. Didn't try any newer versions except for the reproducible 6.6.0 version.

@OmegaMadLab
Copy link

OmegaMadLab commented Aug 14, 2018

Faced same issue yesterday on 5.4.0. Solved by specifying StorageAccountType in the Set-AzureRmVMOSDisk phase, before creating the VM with New-AzureRmVm:

$TestVm = Set-AzureRmVMOSDisk -VM $TestVm -ManagedDiskId $Disk.Id -CreateOption Attach -Windows -StorageAccountType Premium_LRS

Previously I was specifying storage sku only in New-AzureRmDiskConfig, as per official documentation, and New-AzureRMVm stopped working after last update.

@jeanfrancoislarente
Copy link
Author

Fantastic @OmegaMadLab - re-installed 6.6.0 and tried by adding the StorageAccountType to the Set-AzureRmVMOSDisk CmdLet and it worked like a charm.

Interesting that it works in 6.3.0 without setting that value but not in 5.4.0 and 6.6.0

@hyonholee
Copy link
Contributor

The root cause is the generated client library has a converter for storage account type, and even the value is null, it tries to write the value by calling value.ToString() (below line).
https://github.com/Azure/azure-sdk-for-net/blob/8852cf540d6b68acf2ce7f0d3505749c8dd9aa91/src/SDKs/Compute/Management.Compute/Generated/Models/StorageAccountTypesConverter.cs#L49

@Karishma-Tiwari-MSFT
Copy link
Contributor

@hyonholee @singhkays Is there an update on this issue? Many customers are still facing the same issue. Is there a timeline on when this be fixed? Thanks.

@BrandonBoone
Copy link
Contributor

FYI: @OmegaMadLab 's fix is also working when calling Add-AzureRmVMDataDisk

$vmConfig = Add-AzureRmVMDataDisk -VM $vmConfig -VM $vmConfig -StorageAccountType Premium_LRS #...

@mjconnection mjconnection added bug This issue requires a change to an existing behavior in the product in order to be resolved. and removed Compute labels Jun 17, 2019
@Drewm3
Copy link
Member

Drewm3 commented Sep 19, 2019

Cleaning up old issues from 2016 - 2018. Please reopen this issue if it is still a concern.

@Drewm3 Drewm3 closed this as completed Sep 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue requires a change to an existing behavior in the product in order to be resolved. Compute - Managed Disks Service Attention This issue is responsible by Azure service team.
Projects
None yet
Development

No branches or pull requests