diff --git a/builder/hyperv/common/driver_ps_4.go b/builder/hyperv/common/driver_ps_4.go index 030ee8f55..ae125259a 100644 --- a/builder/hyperv/common/driver_ps_4.go +++ b/builder/hyperv/common/driver_ps_4.go @@ -64,7 +64,7 @@ func (d *HypervPS4Driver) Verify() error { return err } - if err := d.verifyElevatedMode(); err != nil { + if err := d.verifyHypervPermissions(); err != nil { return err } @@ -293,16 +293,28 @@ func (d *HypervPS4Driver) verifyPSHypervModule() error { return nil } -func (d *HypervPS4Driver) verifyElevatedMode() error { +func (d *HypervPS4Driver) verifyHypervPermissions() error { - log.Printf("Enter method: %s", "verifyElevatedMode") + log.Printf("Enter method: %s", "verifyHypervPermissions") - isAdmin, _ := powershell.IsCurrentUserAnAdministrator() + hypervAdminCmd := "([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole('Hyper-V Administrators')" - if !isAdmin { - err := fmt.Errorf("%s", "Please restart your shell in elevated mode") + var ps powershell.PowerShellCmd + cmdOut, err := ps.Output(hypervAdminCmd) + if err != nil { return err } + res := strings.TrimSpace(string(cmdOut)) + + if res == "False" { + isAdmin, _ := powershell.IsCurrentUserAnAdministrator() + + if !isAdmin { + err := fmt.Errorf("%s", "Current user is not a member of 'Hyper-V Administrators' or 'Administrators' group") + return err + } + } + return nil }