From 22cf29b2ee45054a589fa5f7ce788b8f4acb4a50 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Wed, 23 Dec 2015 11:56:40 +0100 Subject: [PATCH] FIX #639 and #510 find powershell.exe Signed-off-by: David Gageot --- drivers/hyperv/hyperv.go | 5 +++++ drivers/hyperv/powershell.go | 14 ++++---------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/drivers/hyperv/hyperv.go b/drivers/hyperv/hyperv.go index a19858aeab..6f1a1c5c00 100644 --- a/drivers/hyperv/hyperv.go +++ b/drivers/hyperv/hyperv.go @@ -130,6 +130,11 @@ func (d *Driver) GetState() (state.State, error) { // PreCreateCheck checks that the machine creation process can be started safely. func (d *Driver) PreCreateCheck() error { + // Check that powershell was found + if powershell == "" { + return ErrPowerShellNotFound + } + // Check that hyperv is installed if err := hypervAvailable(); err != nil { return err diff --git a/drivers/hyperv/powershell.go b/drivers/hyperv/powershell.go index f7168f40c2..a47695938d 100644 --- a/drivers/hyperv/powershell.go +++ b/drivers/hyperv/powershell.go @@ -4,9 +4,7 @@ import ( "bufio" "bytes" "errors" - "os" "os/exec" - "path/filepath" "strings" "fmt" @@ -17,17 +15,13 @@ import ( var powershell string var ( - ErrNotAdministrator = errors.New("Hyper-v commands have to be run as an Administrator") - ErrNotInstalled = errors.New("Hyper-V PowerShell Module is not available") + ErrPowerShellNotFound = errors.New("Powershell was not found in the path") + ErrNotAdministrator = errors.New("Hyper-v commands have to be run as an Administrator") + ErrNotInstalled = errors.New("Hyper-V PowerShell Module is not available") ) func init() { - systemPath := strings.Split(os.Getenv("PATH"), ";") - for _, path := range systemPath { - if strings.Index(path, "WindowsPowerShell") != -1 { - powershell = filepath.Join(path, "powershell.exe") - } - } + powershell, _ = exec.LookPath("powershell.exe") } func cmdOut(args ...string) (string, error) {