2

I have this script to kill VNC precesses and restart VNC service:

$ip = Read-Host 'Enter hostname or IP'

& tasklist /s $ip /FI "IMAGENAME eq winvnc*"

$procid_1 = Read-Host 'pid 1'
$procid_2 = Read-Host 'pid 2'

& taskkill /s $ip /pid $procid_1
& taskkill /s $ip /pid $procid_2

Stop-Service -InputObject $(Get-Service -Computer $ip -Name "uvnc_service")
Start-Service -InputObject $(Get-Service -Computer $ip -Name "uvnc_service")

This command

& tasklist /s $ip /FI "IMAGENAME eq winvnc*"

gives me this output:

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
winvnc.exe                    3576                            0      2,968 K
winvnc.exe                    4444                            0      5,556 K

And I have to enter PIDs (in this case 3576 and 4444) manually to variables $procid_1 and $procid_2

Is there any way how to pass tasklist output directly to variables?

Thanks in advance for any hints! Josef

1 Answer 1

2

Use the Get-Process cmdlet instead of tasklist - Get-Process will output real live .NET objects, so you can grab the Id property directly using dot notation:

$RemoteComputer = Read-Host 'Enter hostname or IP'

Get-Process -Name winvnc -ComputerName $RemoteComputer |ForEach-Object {
    & taskkill /s $ip /pid $_.Id
}
Sign up to request clarification or add additional context in comments.

2 Comments

unfortunately it gives me this error Stop-Process : Feature is not supported for remote machines. At C:\Users\barcuchj1-a\Downloads\Scripty_testing\VNC\VNC_restart_v0.1.ps1:3 char:46 + Get-Process -Name winvnc -ComputerName $ip | Stop-Process -Force + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Stop-Process], NotSupportedException + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.StopProcessCommand
Excelent! Your solution works like a charm! SUCCESS: The process with PID 7564 has been terminated. SUCCESS: The process with PID 7708 has been terminated. Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.