Skip to content

Make Windows TA agnostic to Powershell ExecutionPolicy #5935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,11 @@ if "%discovery_properties_value%" == "" (
call :extract_bundle

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

call :splunk_TA_otel_log_msg "INFO" "Otel agent stopped"
endlocal
Expand All @@ -206,7 +208,7 @@ echo on
set "log_type=%~1"
set "log_msg=%~2"

for /f "delims=" %%a in ('powershell -noninteractive -noprofile -command "get-date -format 'MM-dd-yyyy HH:mm K'"') do (
for /f "delims=" %%a in ('powershell -noninteractive -noprofile -command "get-date -format 'yyyy-MM-dd HH:mm:ss.fff K'"') do (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3

set "log_date=%%a"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,33 @@ $splunkTAPSLogFile=Join-Path -Path $splunkTAOtelLogDir -ChildPath Splunk_TA_otel
function otelLogWrite
{
Param ([string]$logstring)

Add-content $splunkTAPSLogFile -value $logstring
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff K"
Add-content $splunkTAPSLogFile -value "$timestamp $logstring"
}

function isOtelProcessRunning($processName)
{
$parent = (Get-WmiObject win32_process | ? processid -eq $PID).parentprocessid
$Global:parentPid = $parent
otelLogWrite "INFO Parent process id: $parent"
$child = (Get-WmiObject win32_process | ? parentprocessid -eq $parent | ? processname -eq $processName).processid
$Global:otelPid = $child
otelLogWrite "INFO Collector process id: $child"
$otelProcess = Get-Process -Id $child
if($otelProcess)
{
otelLogWrite "INFO Otel agent running"
return 1
return $true
}
else
{
return $false
}
}

function waitForExit($parent)
{
Wait-Process -Id $parent
otelLogWrite "INFO Parent process exited"
}

function forceStopOtelProcess($processId)
Expand All @@ -45,9 +51,13 @@ function CheckOtelProcessStop($processId)
}
}

otelLogWrite "INFO Splunk_TA_otelutils.ps1 started"
start-sleep -s 3
isOtelProcessRunning($otelProcessName)

waitForExit($parentPid)
forceStopOtelProcess($otelPid)
CheckOtelProcessStop($otelPid)
if (isOtelProcessRunning($otelProcessName)) {
otelLogWrite "INFO Otel agent running"
waitForExit($parentPid)
forceStopOtelProcess($otelPid)
CheckOtelProcessStop($otelPid)
} else {
otelLogWrite "ERROR Otel agent not running"
}