Skip to content

Commit 8c658ec

Browse files
committed
Refactor Windows collector to handle PowerShell execution.
Moved PowerShell script handling from Install() to SendSystemLogs() for improved clarity and task separation. Added execution policy configuration and error logging to handle script execution failures more gracefully.
1 parent 69daeeb commit 8c658ec

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

agent/collectors/windows_arm64.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,26 @@ foreach ($rawEvent in $recentLogs) {
153153
`
154154

155155
func (w Windows) Install() error {
156-
path := utils.GetMyPath()
157-
collectorPath := filepath.Join(path, "collector.ps1")
158-
err := os.WriteFile(collectorPath, []byte(PowerShellScript), 0644)
159-
return err
156+
return nil
160157
}
161158

162159
func (w Windows) SendSystemLogs() {
163160
path := utils.GetMyPath()
164161
collectorPath := filepath.Join(path, "collector.ps1")
165162

163+
err := os.WriteFile(collectorPath, []byte(PowerShellScript), 0644)
164+
if err != nil {
165+
_ = utils.Logger.ErrorF("error writing powershell script: %v", err)
166+
return
167+
}
168+
169+
cmd := exec.Command("Powershell.exe", "-Command", `"Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser -Force"`)
170+
err = cmd.Run()
171+
if err != nil {
172+
_ = utils.Logger.ErrorF("error setting powershell execution policy: %v", err)
173+
return
174+
}
175+
166176
for {
167177
select {
168178
case <-time.After(30 * time.Second):
@@ -182,7 +192,7 @@ func (w Windows) SendSystemLogs() {
182192
for _, logLine := range logLines {
183193
validatedLog, _, err := validations.ValidateString(logLine, false)
184194
if err != nil {
185-
utils.Logger.LogF(100, "error validating log: %s: %v", logLine, err)
195+
_ = utils.Logger.LogF(100, "error validating log: %s: %v", logLine, err)
186196
continue
187197
}
188198

0 commit comments

Comments
 (0)