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

PowerShell null reference error when importing Hyper-V VM with VMMS not running #10973

Closed
mrshanahan opened this issue Jul 17, 2019 · 3 comments

Comments

@mrshanahan
Copy link
Contributor

mrshanahan commented Jul 17, 2019

Vagrant version

2.2.5

Host operating system

Windows 10 (1709)

Guest operating system

Windows Server 2012 R2

Vagrantfile

Nothing specific:

Vagrant.configure("2") do |config|
  config.vm.box = "<custom-hyperv-box>"
end

Debug output

https://gist.github.com/mrshanahan/58edae984050bcb94dafae8ddd8545d1

Expected behavior

The command fails with a helpful error message, possibly indicating that VMMS is down.

Actual behavior

The import_vm.ps1 script fails with the following error:

You cannot call a method on a null-valued expression.

Steps to reproduce

  1. Disable the VMMS service ("Hyper-V Virtual Machine Management"). (PowerShell: Get-Service vmms | Stop-Service)
  2. Initialize a new Vagrant directory with a bare-bones Vagrantfile pointing at a Hyper-V box:
Vagrant.configure("2") do |config|
  config.vm.box = "<some-hyperv-box>"
end
  1. Run vagrant up (with --provider hyperv as necessary with the box).

Root cause

I've found the root cause and will open a PR ASAP. Just wanted to create an issue to track it.

Vagrant fails to create a VM due to VMMS not running, which is expected behavior. It then attempts to create a more helpful error message for the failed import, and starts by querying WMI for the VMMS service:

$ManagementService = Get-WmiObject -Namespace 'root\virtualization\v2' -Class 'Msvm_VirtualSystemManagementService'

Since this service is not running, the return value is null, and the attempt to import the system definition from the null service fails:

$Result = $ManagementService.ImportSystemDefinition($FullPathFile, $null, $true)

Checking for null after querying the service and throwing a more specific message should do the trick.

@mrshanahan mrshanahan changed the title PowerShell null reference error when initially calling vagrant up with VMMS not running PowerShell null reference error when importing Hyper-V VM with VMMS not running Jul 17, 2019
mrshanahan added a commit to mrshanahan/vagrant that referenced this issue Jul 19, 2019
@briancain briancain added this to the 2.2.6 milestone Jul 19, 2019
@chrisroberts chrisroberts self-assigned this Jul 22, 2019
briancain added a commit that referenced this issue Oct 1, 2019
Fixes issue #10973: checks that VMMS WMI reference is null & throws appropriately
gitebra pushed a commit to gitebra/vagrant that referenced this issue Oct 4, 2019
* commit '07a51906769ad1d717e9246a67d3dc4fc57e84a9':
  Update CHANGELOG
  Update CHANGELOG
  Update CHANGELOG
  Fixes hashicorp#10950: Ensure pip_install_cmd is finalized
  Fixes hashicorp#11027: Ensure VM id is passed to list snapshots
  Update CHANGELOG
  Move around example mention in docs
  Docs: Add note about running bash with the `run` option for triggers
  Fixes hashicorp#10966: Ensure all subdirectory files are watched
  Fixes issue hashicorp#10973: checks that VMMS WMI reference is null & throws appropriately
  ansible_ssh_host in the example is deprecated
@briancain
Copy link
Member

Closing this since the pull request was merged.

@mrshanahan
Copy link
Contributor Author

Thank you!

@ghost
Copy link

ghost commented Jan 28, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@hashicorp hashicorp locked and limited conversation to collaborators Jan 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants