1

Before deployment, I'm trying to kill processes that lock files using PowerShell Invoke-Command

This is my code:

$password = ConvertTo-SecureString "password" -AsPlainText -Force

$credentials = New-Object System.Management.Automation.PsCredential("Admin",$password)

$scriptBlock = {Get-Process | Where-Object { $_.MainWindowTitle -like 'MyApp*'} | Stop-Process}

Invoke-Command -computername Agent1 -Credential $credentials -scriptblock $scriptBlock

Unfortunately it does not do anything and no errors are thrown.

On the machine, this works fine:

Get-Process | Where-Object { $_.MainWindowTitle -like 'MyApp*'} | Stop-Process
697
  • 321
  • 4
  • 16
  • 1
    if there's a process running in the background then it's not going to have a Window Title. Try `Enter-PSSession -computername Agent1` and then do a `Get-Process -IncludeUsername | Where-Object { $_.MainWindowTitle }` You should get an empty result, even if you login to Agent1 via RDP and launch notepad or something you'll still get no result if you try to list that through a remote session. – Repeat Daily May 16 '19 at 17:09
  • The process i'm trying to kill is running as a console app and has title matching the one in my stop-process script. With the exact same command, on the Agent1 machine, I'm able to kill it, running: Get-Process | Where-Object { $_.MainWindowTitle -like 'MyApp*'} | Stop-Process I've tried Enter-PSSession -computername Agent1 -credential $credentials It worked fine, but then, same. Running command has no result. – 697 May 16 '19 at 17:22
  • So if I understand correctly, it works using `Enter-PSSession`, yes? Try creating a session object using `New-PSSession`, and then passing that object to `Enter-PSSession` and testing; if that works then try passing the session object to `Invoke-Command`. – Repeat Daily May 16 '19 at 17:40
  • I'm able to connect to the machine, but when I try to actually stop any process there, it doesn't.. – 697 May 16 '19 at 17:48

1 Answers1

1

As described above create a PS session object:

$ErrorActionPreference = "Stop"

$password = ConvertTo-SecureString "password" -AsPlainText -Force

$credentials = New-Object System.Management.Automation.PsCredential("Admin",$password)

$scriptBlock = {
    $process = Get-Process
    $process | Where-Object { $_.MainWindowTitle -like 'MyApp*'} | Stop-Process}
    $process
}
$session = New-PsSession -ComputerName "Agent1" -Credentials $credentials

$remoteProcess = Invoke-Command -Session $session -Credential $credentials -scriptblock $scriptBlock

$remoteProcess | format-table

Above code will also return you a list processes running on the remote host. Based on $remoteProcess you'll see if the process to kill was running or not. I also set the ErrorActionPreference to stop which forces above code to stop on the first error (in case of the session could not be created).

Hope that helps

Moerwald
  • 10,448
  • 9
  • 43
  • 83
  • $session = New-PsSession -ComputerName "Agent1" -Credentials $credentials -should be -Credential $credentials (little typo there, extra 's') However, after this: $remoteProcess = Invoke-Command -Session $session -scriptblock $scriptBlock $remoteProcess | format-table - this executes but no result, process is still running – 697 May 16 '19 at 18:45
  • 1
    I think this here might be what you need to do. https://stackoverflow.com/questions/958123/powershell-script-to-check-an-application-thats-locking-a-file – Repeat Daily May 16 '19 at 20:41
  • Using ```Get-Process | Where-Object { $_.MainWindowTitle -like 'MyApp*'``` does not work, apparently because of luck of the access to UI. However ```Stop-Process -Name "cmd*" -Force``` does the work in my case, even it is not what I was looking for. Thanks for help guys! – 697 May 20 '19 at 19:52