forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
powershell.go
62 lines (54 loc) · 1.29 KB
/
powershell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package hyperv
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/docker/machine/libmachine/log"
)
var powershell string
func init() {
systemPath := strings.Split(os.Getenv("PATH"), ";")
for _, path := range systemPath {
if strings.Index(path, "WindowsPowerShell") != -1 {
powershell = filepath.Join(path, "powershell.exe")
}
}
}
func execute(args []string) (string, error) {
args = append([]string{"-NoProfile"}, args...)
cmd := exec.Command(powershell, args...)
log.Debugf("[executing ==>] : %v %v", powershell, strings.Join(args, " "))
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
log.Debugf("[stdout =====>] : %s", stdout.String())
log.Debugf("[stderr =====>] : %s", stderr.String())
return stdout.String(), err
}
func parseStdout(stdout string) []string {
s := bufio.NewScanner(strings.NewReader(stdout))
resp := []string{}
for s.Scan() {
resp = append(resp, s.Text())
}
return resp
}
func hypervAvailable() error {
command := []string{
"@(Get-Command Get-VM).ModuleName"}
stdout, err := execute(command)
if err != nil {
return err
}
resp := parseStdout(stdout)
if resp[0] == "Hyper-V" {
return nil
}
return fmt.Errorf("Hyper-V PowerShell Module is not available")
}