| 1 | |
| 2 | |
| 3 | |
| 4 | package windows |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "encoding/json" |
| 9 | "fmt" |
| 10 | "os/exec" |
| 11 | ) |
| 12 | |
| 13 | type CommandLineJSON []struct { |
| 14 | CommandLine string |
| 15 | } |
| 16 | |
| 17 | |
| 18 | func GetProcessCommandLine(ctx context.Context, name string) ([]string, error) { |
| 19 | out, err := exec.CommandContext(ctx, |
| 20 | "powershell.exe", |
| 21 | "-nologo", |
| 22 | "-noprofile", |
| 23 | fmt.Sprintf( |
| 24 | `Get-CimInstance Win32_Process -Filter "name = '%s'" | Select CommandLine | ConvertTo-Json`, |
| 25 | name, |
| 26 | ), |
| 27 | ).CombinedOutput() |
| 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | |
| 32 | var outJSON CommandLineJSON |
| 33 | if err = json.Unmarshal(out, &outJSON); err != nil { |
| 34 | return nil, fmt.Errorf("failed to unmarshal %#q as %T: %w", out, outJSON, err) |
| 35 | } |
| 36 | |
| 37 | var ret []string |
| 38 | for _, s := range outJSON { |
| 39 | ret = append(ret, s.CommandLine) |
| 40 | } |
| 41 | |
| 42 | return ret, nil |
| 43 | } |