2016-02-22 14:44:12 -05:00
|
|
|
package winrm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2021-01-07 18:44:58 -05:00
|
|
|
|
|
|
|
"golang.org/x/text/encoding/unicode"
|
2016-02-22 14:44:12 -05:00
|
|
|
)
|
|
|
|
|
2016-10-01 16:34:42 -04:00
|
|
|
// Powershell wraps a PowerShell script
|
|
|
|
// and prepares it for execution by the winrm client
|
2016-02-22 14:44:12 -05:00
|
|
|
func Powershell(psCmd string) string {
|
2021-01-07 18:44:58 -05:00
|
|
|
// Disable unnecessary progress bars which considered as stderr.
|
|
|
|
psCmd = "$ProgressPreference = 'SilentlyContinue';" + psCmd
|
|
|
|
|
|
|
|
// Encode string to UTF16-LE
|
|
|
|
encoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewEncoder()
|
|
|
|
encoded, err := encoder.String(psCmd)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
2021-01-07 18:44:58 -05:00
|
|
|
// Finally make it base64 encoded which is required for powershell.
|
|
|
|
psCmd = base64.StdEncoding.EncodeToString([]byte(encoded))
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2021-01-07 18:44:58 -05:00
|
|
|
// Specify powershell.exe to run encoded command
|
|
|
|
return "powershell.exe -EncodedCommand " + psCmd
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|