hyper-v: Don't error while checking for admin permissions.
This commit is contained in:
parent
abcc02dc64
commit
0be02ab217
|
@ -301,23 +301,37 @@ func (d *HypervPS4Driver) verifyPSHypervModule() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *HypervPS4Driver) isCurrentUserAHyperVAdministrator() (bool, error) {
|
||||||
|
//SID:S-1-5-32-578 = 'BUILTIN\Hyper-V Administrators'
|
||||||
|
//https://support.microsoft.com/en-us/help/243330/well-known-security-identifiers-in-windows-operating-systems
|
||||||
|
|
||||||
|
var script = `
|
||||||
|
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
|
||||||
|
$principal = new-object System.Security.Principal.WindowsPrincipal($identity)
|
||||||
|
$hypervrole = [System.Security.Principal.SecurityIdentifier]"S-1-5-32-544"
|
||||||
|
return $principal.IsInRole($hypervrole)
|
||||||
|
`
|
||||||
|
|
||||||
|
var ps powershell.PowerShellCmd
|
||||||
|
cmdOut, err := ps.Output(script)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
res := strings.TrimSpace(cmdOut)
|
||||||
|
return powershell.IsTrue(res), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (d *HypervPS4Driver) verifyHypervPermissions() error {
|
func (d *HypervPS4Driver) verifyHypervPermissions() error {
|
||||||
|
|
||||||
log.Printf("Enter method: %s", "verifyHypervPermissions")
|
log.Printf("Enter method: %s", "verifyHypervPermissions")
|
||||||
|
|
||||||
//SID:S-1-5-32-578 = 'BUILTIN\Hyper-V Administrators'
|
hyperVAdmin, err := d.isCurrentUserAHyperVAdministrator()
|
||||||
//https://support.microsoft.com/en-us/help/243330/well-known-security-identifiers-in-windows-operating-systems
|
|
||||||
hypervAdminCmd := "([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole('S-1-5-32-578')"
|
|
||||||
|
|
||||||
var ps powershell.PowerShellCmd
|
|
||||||
cmdOut, err := ps.Output(hypervAdminCmd)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
log.Printf("Error discovering if current is is a Hyper-V Admin: %s", err)
|
||||||
}
|
}
|
||||||
|
if !hyperVAdmin {
|
||||||
|
|
||||||
res := strings.TrimSpace(cmdOut)
|
|
||||||
|
|
||||||
if res == "False" {
|
|
||||||
isAdmin, _ := powershell.IsCurrentUserAnAdministrator()
|
isAdmin, _ := powershell.IsCurrentUserAnAdministrator()
|
||||||
|
|
||||||
if !isAdmin {
|
if !isAdmin {
|
||||||
|
|
|
@ -17,6 +17,14 @@ const (
|
||||||
powerShellTrue = "True"
|
powerShellTrue = "True"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func IsTrue(s string) bool {
|
||||||
|
return s == powerShellTrue
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsFalse(s string) bool {
|
||||||
|
return s == powerShellFalse
|
||||||
|
}
|
||||||
|
|
||||||
type PowerShellCmd struct {
|
type PowerShellCmd struct {
|
||||||
Stdout io.Writer
|
Stdout io.Writer
|
||||||
Stderr io.Writer
|
Stderr io.Writer
|
||||||
|
|
Loading…
Reference in New Issue