Skip to content

Commit

Permalink
unify all powershell commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Jul 17, 2024
1 parent d1f2896 commit 2e7636a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packaging/snclient.ini
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ bat = ${script root}\\%SCRIPT% %ARGS%
vbs = cscript.exe //T:30 //NoLogo ${script root}\\lib\\wrapper.vbs %SCRIPT% %ARGS%

; Powershell Wrapping - Command line used for executing wrapped ps1 (powershell) scripts
ps1 = cmd /c echo If (-Not (Test-Path "${script root}\%SCRIPT%") ) { Write-Host "UNKNOWN: Script `"%SCRIPT%`" not found."; exit(3) }; ${script root}\%SCRIPT% $ARGS$; exit($lastexitcode) | powershell.exe -nologo -noprofile -command -
ps1 = cmd /c echo If (-Not (Test-Path "${script root}\%SCRIPT%") ) { Write-Host "UNKNOWN: Script `"%SCRIPT%`" not found."; exit(3) }; ${script root}\%SCRIPT% $ARGS$; exit($lastexitcode) | powershell.exe -nologo -noprofile -WindowStyle hidden -NonInteractive -ExecutionPolicy ByPass -command -


; log - Configure log properties.
Expand Down
4 changes: 3 additions & 1 deletion pkg/snclient/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"golang.org/x/exp/slices"
)

const POWERSHELL = "powershell.exe -nologo -noprofile -WindowStyle hidden -NonInteractive -ExecutionPolicy ByPass"

var DefaultConfig = map[string]ConfigData{
"/modules": {
"Logrotate": "enabled",
Expand Down Expand Up @@ -51,7 +53,7 @@ var DefaultConfig = map[string]ConfigData{
`If (-Not (Test-Path "${script root}\%SCRIPT%") ) ` +
`{ Write-Host "UNKNOWN: Script ` + "`\"%SCRIPT%`\" not found.\"; exit(3) }; " +
`${script root}\%SCRIPT% $ARGS$; ` +
`exit($lastexitcode) | powershell.exe -nologo -noprofile -command -`,
`exit($lastexitcode) | ` + POWERSHELL + ` -command -`,
"vbs": `cscript.exe //T:30 //NoLogo ${script root}\\lib\\wrapper.vbs %SCRIPT% %ARGS%`,
},
}
Expand Down
13 changes: 3 additions & 10 deletions pkg/snclient/snclient_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,7 @@ func (snc *Agent) makeCmd(ctx context.Context, command string) (*exec.Cmd, error
// powershell command
case strings.HasPrefix(command, "& "):
cmd := execCommandContext(ctx, "powershell", env)
cmd.SysProcAttr.CmdLine = fmt.Sprintf(
`powershell -WindowStyle hidden -NoLogo -NonInteractive -Command %s; exit($LASTEXITCODE)`,
command,
)
cmd.SysProcAttr.CmdLine = fmt.Sprintf(`%s -command %s; exit($LASTEXITCODE)`, POWERSHELL, command)

return cmd, nil

Expand Down Expand Up @@ -275,11 +272,7 @@ func (snc *Agent) makeCmd(ctx context.Context, command string) (*exec.Cmd, error
}
}
cmd := execCommandContext(ctx, "powershell", env)
cmd.SysProcAttr.CmdLine = fmt.Sprintf(
`powershell -WindowStyle hidden -NoLogo -NonInteractive -Command ". '%s' %s; exit($LASTEXITCODE)"`,
cmdName,
strings.Join(cmdArgs, " "),
)
cmd.SysProcAttr.CmdLine = fmt.Sprintf(`%s -Command ". '%s' %s; exit($LASTEXITCODE)"`, POWERSHELL, cmdName, strings.Join(cmdArgs, " "))

return cmd, nil

Expand Down Expand Up @@ -347,7 +340,7 @@ func powerShellCmd(ctx context.Context, command string) (cmd *exec.Cmd) {
cmd.Args = nil
cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: true,
CmdLine: fmt.Sprintf(`powershell -WindowStyle hidden -NoLogo -NonInteractive -Command "%s"`, command), //nolint:gocritic // using %q just breaks the command from escaping newlines
CmdLine: fmt.Sprintf(`%s -Command "%s"`, POWERSHELL, command), //nolint:gocritic // using %q just breaks the command from escaping newlines
}

return cmd
Expand Down

0 comments on commit 2e7636a

Please sign in to comment.