Stop the Running process of taskmgr from PowerShell - powershell

I write this command for powershell. I want to stop the running process of taskmgr from PowerShell.
Get-Service | Where-Object {$_.Status -eq "Running"}
And how to stop process those running processes?

Task Manager is a process, not a service.
So, in order to stop it from PowerShell you need to run:
Stop-Process -Name Taskmgr -Force
PS: You will need to run the powershell as administrator in order to stop the process.
Best regards,

Related

My powershell script is not stopping a process that I am opening

I am creating a basic powershell script that will open the calculator app, get its process id, and then eventually stop the process itself with confirmation. But when I run the following script on my computer (through Powershell ISE, but it also doesn't seem to work on regular powershell in admin mode), it does not kill the calculator process. I am running Windows 10 Version 21H1.
Start-Process calc.exe; Get-Process calc; Stop-Process -Name calc -whatif; Stop-Process -Name calc -Confirm -PassThru
I had to use the name Calculator instead of calc for Get-Process. I also had to add a delay between the launching of calc.exe and Stop-Process. Without the delay, it could not find the process to stop it.
Start-Process -FilePath calc.exe
Start-Sleep -Seconds 3
Get-Process -Name Calculator | Stop-Process

PowerShell - How to create a script for automatic restart of windows services?

I have PowerShell commands to restart my windows service:
$Svc = Get-Service -DisplayName 'Test' -ErrorAction Stop
$Svc | Restart-Service -ErrorAction Stop
Now I do not know how I can automatically create powershell script which will execute those PS commands and also how to schedule that new powershell script to be executed once per day (to perform that restart).
Thank you!!!

Task Scheduler can't kill a process properly using PowerShell script

I want to kill a process using Task Scheduler and PowerShell script.
When a specific process starts, task scheduler triggers the PS script. The script gets the process Id and tries to stop it.
My issue is, the script can't kill the process until the process finishes its job. However, I want to kill the process as soon as the script triggers, without waitingfor anything. As a note, the process I want to kill also runs with Admin privileges, and runs in window mode(not in background)
Scheduled Task settings: running as SYSTEM with highest privileges. I also used executionPloicyBypass parameter as below:
powershell -ExecutionPolicy Bypass -File C:\Scripts\KillProcess.ps1.
In the script, I have the following code basically
$process = Get-Process -Id $pid
$process.PriorityClass = 'High'
$events=Get-WinEvent -FilterHashtable #{LogName="Security"; Id = <eventId>; StartTime = [datetime]::Now.AddMinutes(-5)} |Where-Object -Property Message -Match '<string to search for>' | Where-Object -Property Message -Match ('<string to search for>') -ErrorAction Stop
if (!$events[0].message) {
Exit
}
else {
$processes = #()
#for each event, get process Id and kill it.
#this is because the process can spawn multiple process.
foreach ($event in $events) {
#parse the process Id.-*
$processId=[int][regex]::Match($event.message,'Process\sID\:\s+(0x.+)\s').captures.groups[1].Value
$processes += $processId
}
$processes = $processes | Select -Unique
foreach ($proc in $processes) {
Stop-Process -Id $proc -Force -ErrorAction SilentlyContinue
}
}
When I run PowerShell ISE as Admin and run the script there manually, it immediately kills the process. However, it waits for the process to finish its job when task scheduler triggers the script. Am i doing something wrong with the Task scheduler?
I don't know what was the issue with Stop-Process but I changed it to process.Kill() method by getting the process object using Get-Process -Id $proc first. Now it works without issue.

Set-Service control actions

I am using below command to start the windows service. I need stop the execution of this command if it doesn't start before 5 or 10 and continue with next commands in script. Can you please help me how do it?
Get-Service -ComputerName “$server_name” -Name “XHedge.Execution-NYTRADER” |
Set-Service -Status Running

Remote Machine not executing program

Here is what I have so far:
$source1="C:\Folder\Files\IPList.txt"
Get-Content $source1 |
Where-Object {-not(gwmi win32_process -ComputerName $_ -filter "name='Program.exe'")} |
Foreach-Object {Invoke-Command -ComputerName $_ -ScriptBlock {"C:\Program Files\Folder\Folder\Program.exe"}}
When I run this in ISE everything comes back as normal and says it has run correctly. However, when I look at my remote machine nothing has been executed. The Process Program.exe is not running and there for the exe should be launched. I am running this from a server to hit about 50 remote machines. Once it goes through all 50, I will loop it and have it do it again, then continue the process in an infinite loop.
What am I missing for the program to start remotely? By the way I am running this script on Server 2008 R2 and it is hitting Windows 7 machines.
Edit
I am wondering since I can see the process firing off, is this an issue with Windows 7? I know Microsoft changed things and a service cannot fire off an application in the User space. Do you think this would be part of the same problem?
Try adding the call (&) operator to the ScriptBlock:
$source1="C:\Folder\Files\IPList.txt"
Get-Content $source1 |
Where-Object {-not(gwmi win32_process -ComputerName $_ -filter "name='Program.exe'")} |
Foreach-Object {Invoke-Command -ComputerName $_ -ScriptBlock {& "C:\Program Files\Folder\Folder\Program.exe"}}
Here's a good article on the various methods available to you: http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx
In your current syntax, the command your passing is just a string! This is what is happening on the remote end:
PS C:\> "C:\Program Files\Console2\Console.exe"
C:\Program Files\Console2\Console.exe
Powershell is echoing your string!
I am going to quote Oliver Lipkau as mentioned here: Source
If you need to start a process on a remote computer that keeps running after the script finished, use this function:
Function New-Process ([string]$computername, [string]$name) {
([WMICLASS]"\\$computername\ROOT\CIMV2:win32_process").Create($name)
}