Stop-process

Hi,

I’m trying to kill a process during a deployment, but the code below doesn’t work. I thiunk it may be permission-related. Any ideas?

$TitleToKill = “Services”

get-process | where-object {$_.MainWindowTitle -eq $TitleToKill} | stop-process -force

Regards,
Paul

Hi @CTO!

Thanks for reaching out, and sorry to hear that you’re having issues.

I just had a couple of questions to help you get this resolved:

  • Does this work outside of Octopus, when being run as the user that your tentacle service is configured to run as?
  • Are you receiving any errors when you attempt to run this through Octopus?

Look forward to hearing from you soon!

Hi,

Octopus is running on the remote machine under the Local System account.

If I use the Sysinternals tool PSExec to run Powershell as NT Authority\System on the remote machine the script works correctly.

Regards,
Paul

Thanks for getting back to me @CTO - I can confirm that this doesn’t work as reported, but this looks like it’s a limitation of powershell in non-interactive sessions, than any bug with Octopus. I was able to get to this conclusion by the following tests:

Regular output in an interactive/active PowerShell session:

PS C:\Users\jwalsh> Get-Process | where-Object {$_.mainWindowTitle} | Measure-Object

Count    : 4
Average  :
Sum      :
Maximum  :
Minimum  :
Property : 

When calling powershell to run this command directly, it doesn’t allow access to the mainWindowTitle object element:

PS C:\Users\jwalsh> powershell.exe -Command Get-Process | where-Object {$_.mainWindowTitle} | Measure-Object

Count    : 0
Average  :
Sum      :
Maximum  :
Minimum  :
Property : 

So sadly, I’m afraid that you may need to investigate alternative means of finding your running process.

Please don’t hesitate to let me know if you have any further questions!

This is what i’m using to stop a process:

Write-Host "Searching for processes with name like [$ProcessName]"
$processes = Get-Process | Where ProcessName -like $ProcessName -ErrorAction SilentlyContinue

Write-Host "Found $($processes.Length) processes"

$processes | ForEach-Object {
    Write-Host "Stopping [$($_.Name)] @ [$($_.Path)]"

    Stop-Process $_ -Force
}
1 Like