Skip to content

Commit

Permalink
Use Select-Object instead of parenthesis to get Hyper-V VM status
Browse files Browse the repository at this point in the history
Previous implementation doesn't fail if the VM doesn't exist. With
Select-Object, the Hyper-V Get-VM error is correctly reported.

Before:
$ crc status
Error getting the machine state: unexpected Hyper-V state []

After:
$ crc status
Cannot get machine state: Failed to find the VM status: exit status 1 - Hyper-V\Get-VM : Hyper-V was unable to find a virtual machine with name "crc"
  • Loading branch information
guillaumerose authored and praveenkumar committed Sep 3, 2021
1 parent bd3de75 commit cf730cd
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pkg/drivers/hyperv/hyperv_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

log "github.com/code-ready/crc/pkg/crc/logging"
"github.com/code-ready/crc/pkg/os/windows/powershell"
"github.com/code-ready/machine/libmachine/drivers"
"github.com/code-ready/machine/libmachine/state"
)
Expand Down Expand Up @@ -86,14 +87,14 @@ func (d *Driver) DriverName() string {
}

func (d *Driver) GetState() (state.State, error) {
stdout, err := cmdOut("(", "Hyper-V\\Get-VM", d.MachineName, ").state")
stdout, stderr, err := powershell.Execute("Hyper-V\\Get-VM", d.MachineName, "|", "Select-Object", "-ExpandProperty", "State")
if err != nil {
return state.Error, fmt.Errorf("Failed to find the VM status")
return state.Error, fmt.Errorf("Failed to find the VM status: %v - %s", err, stderr)
}

resp := parseLines(stdout)
if len(resp) < 1 {
return state.Error, fmt.Errorf("unexpected Hyper-V state %s", resp)
return state.Error, fmt.Errorf("unexpected Hyper-V state %s", stdout)
}

switch resp[0] {
Expand Down

0 comments on commit cf730cd

Please sign in to comment.