I need to know which service has executed a script from the same script.
For example The service service_Test executed the test script. From the test script I want to know which service has executed it.
I have not found a way to do this, I can only see which services are running.
It looks like what you're asking is how to determine the path of the executable that launched the current PowerShell process, so you can compare it to your service executable path.
You can do this with a combination of
Get-ProcessandGet-CimInstancewith theWin32_ProcessWMI class, using the automatic$PIDvariable to get the current process' ID:In PowerShell (Core) 7+, the solution can be simplified, given that PowerShell now decorates the
System.Diagnostics.Processinstances output byGet-Processwith a.Parentproperty:Not only is this simpler and shorter, it also works on Unix-like platforms.