Find a Hyper-V switch. Either check that the supplied switch exists or find the first external switch.
(name string)
| 231 | // Find a Hyper-V switch. Either check that the supplied switch exists |
| 232 | // or find the first external switch. |
| 233 | func hypervGetSwitch(name string) (string, error) { |
| 234 | if name != "" { |
| 235 | if _, _, err := poshCmd("Get-VMSwitch", name); err != nil { |
| 236 | return "", fmt.Errorf("could not find switch %s: %v", name, err) |
| 237 | } |
| 238 | return name, nil |
| 239 | } |
| 240 | |
| 241 | // The Windows 10 Fall Creators Update adds a new 'Default |
| 242 | // Switch'. Check if it is present and if so, use it. |
| 243 | name = "Default Switch" |
| 244 | if _, _, err := poshCmd("Get-VMSwitch", name); err == nil { |
| 245 | return name, nil |
| 246 | } |
| 247 | |
| 248 | out, _, err := poshCmd("Get-VMSwitch | Format-Table -Property Name, SwitchType -HideTableHeaders") |
| 249 | if err != nil { |
| 250 | return "", fmt.Errorf("could not get list of switches: %v", err) |
| 251 | } |
| 252 | switches := splitLines(out) |
| 253 | for _, s := range switches { |
| 254 | if len(s) == 0 { |
| 255 | continue |
| 256 | } |
| 257 | t := strings.Split(s, " ") |
| 258 | if len(t) < 2 { |
| 259 | continue |
| 260 | } |
| 261 | if strings.Contains(t[len(t)-1:][0], "External") { |
| 262 | return strings.Join(t[:len(t)-1], " "), nil |
| 263 | } |
| 264 | } |
| 265 | return "", fmt.Errorf("could not find an external switch") |
| 266 | } |
no test coverage detected