From 2e7636ad20eac51a38a58c25b07b3dbd9376b621 Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Wed, 17 Jul 2024 14:35:50 +0200 Subject: [PATCH] unify all powershell commands --- packaging/snclient.ini | 2 +- pkg/snclient/config.go | 4 +++- pkg/snclient/snclient_windows.go | 13 +++---------- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/packaging/snclient.ini b/packaging/snclient.ini index 2c1b0f7f..22d75586 100644 --- a/packaging/snclient.ini +++ b/packaging/snclient.ini @@ -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. diff --git a/pkg/snclient/config.go b/pkg/snclient/config.go index bc0984f0..508d114c 100644 --- a/pkg/snclient/config.go +++ b/pkg/snclient/config.go @@ -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", @@ -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%`, }, } diff --git a/pkg/snclient/snclient_windows.go b/pkg/snclient/snclient_windows.go index 777e72a6..0b5593df 100644 --- a/pkg/snclient/snclient_windows.go +++ b/pkg/snclient/snclient_windows.go @@ -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 @@ -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 @@ -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