Perform some sanity checks, and error if failing
()
| 189 | |
| 190 | // Perform some sanity checks, and error if failing |
| 191 | func hypervChecks() { |
| 192 | powershell, _ = exec.LookPath("powershell.exe") |
| 193 | if powershell == "" { |
| 194 | log.Fatalf("Could not find powershell executable") |
| 195 | } |
| 196 | |
| 197 | hvAdmin := false |
| 198 | admin := false |
| 199 | |
| 200 | out, _, err := poshCmd(`@([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole("Hyper-V Administrators")`) |
| 201 | if err != nil { |
| 202 | log.Debugf("Check for Hyper-V Admin failed: %v", err) |
| 203 | } |
| 204 | res := splitLines(out) |
| 205 | if res[0] == "True" { |
| 206 | hvAdmin = true |
| 207 | } |
| 208 | |
| 209 | out, _, err = poshCmd(`@([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")`) |
| 210 | if err != nil { |
| 211 | log.Debugf("Check for Admin failed: %v", err) |
| 212 | } |
| 213 | res = splitLines(out) |
| 214 | if res[0] == "True" { |
| 215 | admin = true |
| 216 | } |
| 217 | if !hvAdmin && !admin { |
| 218 | log.Fatal("Must be run from an elevated prompt or user must be in the Hyper-V Administrator role") |
| 219 | } |
| 220 | |
| 221 | out, _, err = poshCmd("@(Get-Command Get-VM).ModuleName") |
| 222 | if err != nil { |
| 223 | log.Fatalf("Check for Hyper-V powershell modules failed: %v", err) |
| 224 | } |
| 225 | res = splitLines(out) |
| 226 | if res[0] != "Hyper-V" { |
| 227 | log.Fatal("The Hyper-V powershell module does not seem to be installed") |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // Find a Hyper-V switch. Either check that the supplied switch exists |
| 232 | // or find the first external switch. |
no test coverage detected