Skip to content

Commit 4481d72

Browse files
authored
Make Windows TA agnostic to Powershell ExecutionPolicy (#5935)
1 parent 3d36883 commit 4481d72

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

packaging/technical-addon/Splunk_TA_otel/windows_x86_64/bin/Splunk_TA_otel.cmd

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,11 @@ if "%discovery_properties_value%" == "" (
190190
call :extract_bundle
191191

192192
set "command_line=%splunk_TA_otel_app_directory%%splunk_otel_process_name%"
193+
call :splunk_TA_otel_log_msg "INFO" "Starting otel agent with: %command_line% %SPLUNK_OTEL_FLAGS%"
193194
start /B "" "%command_line%" %SPLUNK_OTEL_FLAGS% > "%splunk_otel_log_file_value%" 2>&1
194195
set "splunk_otel_common_ps=%splunk_TA_otel_app_directory%%splunk_otel_common_ps_name%"
195-
start /B "" /I /WAIT powershell "& '%splunk_otel_common_ps%' '%splunk_otel_process_name%' '%splunk_TA_otel_log_file%'"
196+
call :splunk_TA_otel_log_msg "INFO" "Monitoring collector process with: %splunk_otel_common_ps% '%splunk_otel_process_name%' '%splunk_TA_otel_log_file%'"
197+
start /B "" /I /WAIT powershell -ExecutionPolicy ByPass "& '%splunk_otel_common_ps%' '%splunk_otel_process_name%' '%splunk_TA_otel_log_file%'"
196198

197199
call :splunk_TA_otel_log_msg "INFO" "Otel agent stopped"
198200
endlocal
@@ -206,7 +208,7 @@ echo on
206208
set "log_type=%~1"
207209
set "log_msg=%~2"
208210

209-
for /f "delims=" %%a in ('powershell -noninteractive -noprofile -command "get-date -format 'MM-dd-yyyy HH:mm K'"') do (
211+
for /f "delims=" %%a in ('powershell -noninteractive -noprofile -command "get-date -format 'yyyy-MM-dd HH:mm:ss.fff K'"') do (
210212
set "log_date=%%a"
211213
)
212214

packaging/technical-addon/Splunk_TA_otel/windows_x86_64/bin/Splunk_TA_otel_utils.ps1

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,33 @@ $splunkTAPSLogFile=Join-Path -Path $splunkTAOtelLogDir -ChildPath Splunk_TA_otel
66
function otelLogWrite
77
{
88
Param ([string]$logstring)
9-
10-
Add-content $splunkTAPSLogFile -value $logstring
9+
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff K"
10+
Add-content $splunkTAPSLogFile -value "$timestamp $logstring"
1111
}
1212

1313
function isOtelProcessRunning($processName)
1414
{
1515
$parent = (Get-WmiObject win32_process | ? processid -eq $PID).parentprocessid
1616
$Global:parentPid = $parent
17+
otelLogWrite "INFO Parent process id: $parent"
1718
$child = (Get-WmiObject win32_process | ? parentprocessid -eq $parent | ? processname -eq $processName).processid
1819
$Global:otelPid = $child
20+
otelLogWrite "INFO Collector process id: $child"
1921
$otelProcess = Get-Process -Id $child
2022
if($otelProcess)
2123
{
22-
otelLogWrite "INFO Otel agent running"
23-
return 1
24+
return $true
25+
}
26+
else
27+
{
28+
return $false
2429
}
2530
}
2631

2732
function waitForExit($parent)
2833
{
2934
Wait-Process -Id $parent
35+
otelLogWrite "INFO Parent process exited"
3036
}
3137

3238
function forceStopOtelProcess($processId)
@@ -45,9 +51,13 @@ function CheckOtelProcessStop($processId)
4551
}
4652
}
4753

54+
otelLogWrite "INFO Splunk_TA_otelutils.ps1 started"
4855
start-sleep -s 3
49-
isOtelProcessRunning($otelProcessName)
50-
51-
waitForExit($parentPid)
52-
forceStopOtelProcess($otelPid)
53-
CheckOtelProcessStop($otelPid)
56+
if (isOtelProcessRunning($otelProcessName)) {
57+
otelLogWrite "INFO Otel agent running"
58+
waitForExit($parentPid)
59+
forceStopOtelProcess($otelPid)
60+
CheckOtelProcessStop($otelPid)
61+
} else {
62+
otelLogWrite "ERROR Otel agent not running"
63+
}

0 commit comments

Comments
 (0)