diff --git a/communicator/winrm/communicator.go b/communicator/winrm/communicator.go index d1f7d2ab6..a725fe51f 100644 --- a/communicator/winrm/communicator.go +++ b/communicator/winrm/communicator.go @@ -127,6 +127,23 @@ func (c *Communicator) Upload(path string, input io.Reader, _ *os.FileInfo) erro if err != nil { return err } + + // Get information about destination path + endpoint := winrm.NewEndpoint(c.endpoint.Host, c.endpoint.Port, c.config.Https, c.config.Insecure, nil, nil, nil, c.config.Timeout) + client, err := winrm.NewClient(endpoint, c.config.Username, c.config.Password) + if err != nil { + return fmt.Errorf("Was unable to create winrm client: %s", err) + } + stdout, _, _, err := client.RunWithString(fmt.Sprintf("powershell -Command \"(Get-Item %s) -is [System.IO.DirectoryInfo]\"", path), "") + if err != nil { + return fmt.Errorf("Couldn't determine whether destination was a folder or file: %s", err) + } + if strings.Contains(stdout, "True") { + // The path exists and is a directory. + // Upload file into the directory instead of overwriting. + path = filepath.Join(path, filepath.Base((*fi).Name())) + } + log.Printf("Uploading file to '%s'", path) return wcp.Write(path, input) }