-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathpowershell.go
More file actions
27 lines (21 loc) · 759 Bytes
/
powershell.go
File metadata and controls
27 lines (21 loc) · 759 Bytes
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
package winrm
import (
"encoding/base64"
"golang.org/x/text/encoding/unicode"
)
// Powershell wraps a PowerShell script
// and prepares it for execution by the winrm client
func Powershell(psCmd string) string {
// 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 ""
}
// Finally make it base64 encoded which is required for powershell.
psCmd = base64.StdEncoding.EncodeToString([]byte(encoded))
// Specify powershell.exe to run encoded command
return "powershell.exe -EncodedCommand " + psCmd
}