github.com/safing/portbase@v0.19.5/utils/osdetail/shell_windows.go (about) 1 package osdetail 2 3 import ( 4 "bytes" 5 "errors" 6 ) 7 8 // RunPowershellCmd runs a powershell command and returns its output. 9 func RunPowershellCmd(script string) (output []byte, err error) { 10 // Create command to execute. 11 return RunCmd( 12 "powershell.exe", 13 "-ExecutionPolicy", "Bypass", 14 "-NoProfile", 15 "-NonInteractive", 16 "[System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8\n"+script, 17 ) 18 } 19 20 const outputSeparator = "pwzzhtuvpwdgozhzbnjj" 21 22 // RunTerminalCmd runs a Windows cmd command and returns its output. 23 // It sets the output of the cmd to UTF-8 in order to avoid encoding errors. 24 func RunTerminalCmd(command ...string) (output []byte, err error) { 25 output, err = RunCmd(append([]string{ 26 "cmd.exe", 27 "/c", 28 "chcp", // Set output encoding... 29 "65001", // ...to UTF-8. 30 "&", 31 "echo", 32 outputSeparator, 33 "&", 34 }, 35 command..., 36 )...) 37 if err != nil { 38 return nil, err 39 } 40 41 // Find correct start of output and shift start. 42 index := bytes.IndexAny(output, outputSeparator+"\r\n") 43 if index < 0 { 44 return nil, errors.New("failed to post-process output: could not find output separator") 45 } 46 output = output[index+len(outputSeparator)+2:] 47 48 return output, nil 49 }