How to kill Windows service process stuck at starting? - windows

I have a problem with one service which stuck at starting stage when I try to start such service, and I am not able to terminate such process. I know that restart is a solution, but server can't be restart couple times everyday, also any additional software like Process Explorer is not allowed to be installed and used.
So far I tried to use below solution (all commands were executed as admin):
taskkill /f /pid 7788
ERROR: The process with PID 7788 could not be terminated.
Reason: Access is denied.
C:\Windows\system32>wmic
wmic:root\cli>process where name="ProcesName.exe" delete
Delete '\\xxxxx\ROOT\CIMV2:Win32_Process.Handle="7788"' (Y/N/?)? y
Deleting instance \\xxxxx\ROOT\CIMV2:Win32_Process.Handle="7788"
ERROR:
Description = Access denied
wmic:root\cli>
C:\Windows\system32>wmic
wmic:root\cli>process where name="ProcesName.exe" call terminate
Execute (\\xxxxx\ROOT\CIMV2:Win32_Process.Handle="7788")->terminate() (Y/N/?)? y
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ReturnValue = 2;
};
wmic:root\cli>
C:\Windows\system32>taskkill /IM "ProcessName.exe" /T /F
ERROR: The process with PID 7788 (child process of PID 680) could not be terminated.
Reason: Access is denied.
C:\Windows\system32>tasklist | findstr 680
services.exe 680 Services 0 9 084 K
java.exe 5832 Services 0 443 680 K

1- Run PowerShell as an administrator
2- Enter: Get-Service
like this pic
After enter Get-Service command
3- Enter: Stop-Service -Name "SERVICE-NAME"
Like this Picture
Example about stop service
the result after stop service
4- If service doesn't stop try this command
Set-Service -Name "SERVICE-NAME" -Status stopped -force
Another solution:
1- Open powershell or CMD as administrator.
2- {{Path of service}} -k netsvcs

Related

Name Services under started process in cmd

In windows 10 cmd, I started a process C:\Windows\system32\svchost.exe -k RPCSS -p successfully. How do I list the services it is currently managing?
I've looked at tasklist, however, that requires the image name, and I am unsure how to identify which svchost it is, in process explorer.
You can try with wmic in cmd to get the commandline of the process :
wmic process where "name like 'svchost.exe' And CommandLine!=Null" get commandline /Value

Resizing a PowerShell window on a remote computer using a batch file

Wondering if anyone knows how to/if it is even possible to resize a PowerShell windows that I open on a remote computer using the batch file I used to run it.
1 #ECHO OFF
2 title Remote Start ProgramName Server
3 cd "C:\IT\FOLDER"
4
5 #ECHO OFF
6 SET /p _computer=Computer Name:
7
8 #ECHO "Launching Program Server On Remote PC %_computer%"
9
10 start PowerShell.exe .\psexec -i \\%_computer% '"C:\Program Files\ProgramFolder\programserver.exe"'
11
12 #ECHO "Launching ClientSideProgram on Local Machine"
13 start "" "C:\Program Files\ProgramFolder\programclient.exe"
14
15 PAUSE
16
17 start "" PowerShell.exe .\pskill \\%_computer% programserver.exe
18 ECHO "Program Server Terminated"
19
20 PAUSE
So as far as I have tested this, it works, but when I do start PowerShell.exe on line 10, it, as expected opens a powershell. THIS is the powershell I would like to resize to the smallest possible size, so that it's not as big of a nuisance when it opens.
Thanks in advance for whatever help anyone has
Edit: If resizing is not possible, can I just 'hide' it? or move it to the background at all?
This is a batch file that is just using PowerShell to call psexec.
Either use PowerShell Remoting ---
Running Remote Commands
Start an Interactive Session To start an interactive session with a
single remote computer, use the Enter-PSSession cmdlet. For example,
to start an interactive session with the Server01 remote computer,
type:
Enter-PSSession Server01
Run a Remote Command To run a command on one or more computers, use
the Invoke-Command cmdlet. For example, to run a Get-UICulture command
on the Server01 and Server02 remote computers, type:
Invoke-Command -ComputerName Server01, Server02 -ScriptBlock {Get-UICulture}
--- or use PSexec and target that remote host.
PsExec - Windows Sysinternals | Microsoft Docs
Using psExec to Open a Remote Command Window
https://blogs.technet.microsoft.com/systemcenteressentials/2009/09/01/using-psexec-to-open-a-remote-command-window
psExec \computer cmd
PsExec (SysInternals)
Execute a command-line process on a remote machine.
Much of PSRemoting requires that you use an account that is in the local admin group on the target. However, there are some that do not.
The only reason to use PSExec over PSRemoting is if PSRemoting is not enabled and if you need to run Code in the context of the logged on user.
# PowerShell only via PSRemoting
$TargetComputerName = Read-Host -Prompt 'Enter a computer name.'
Invoke-Command -ComputerName $TargetComputerName -ScriptBlock {
'C:\Program Files\ProgramFolder\programserver.exe'
} -Credential 'contoso\administrator'
# PSExec only
psexec \SomeTargetComputerName -c C:\Program Files\ProgramFolder\programserver.exe
With PSRemoting nothing is ever displayed on the targethost.
With that PSExec command, nothing is ever displayed on the targethost.
If you use PSExec to run in the context of the logged on user on that target host, then stuff will display when you call PowerShell.exe unless you use the minimize or hide switches.
This indicates that you are new to PowerShell and PowerShell remoting commands. Please hit up YouTube and view some of the talks on PSRemoting, running remote command, running remote programs, well, PowerShell in general.
Found the answer from a fellow colleague of mine. Apparently, very easy to just star minimized.
start /MIN
will do just that

TASKKILL command works properly outside Script, but not inside of it

Few days ago I started developing a Powershell script which monitors a service. Command:
taskkill /f /fi "USERNAME eq admin" /im tm1top.exe
When I ran taskkill to stop one process inside my script it didn’t work: that process remained in Running, hence the script was not able to end properly.
On the other hand, running exactly the same command (taskkill) from CMD directly was successful.
NOTE: the user which is running this script has ADMIN RIGHTS on the computer and I am running Windows Seever 2008. Also tried to create a task into Windows Scheduler and to run it with highest privileges with this user, but the same result..
Could you please advise what should I modify in order to make this function work directly from my ps script?
I would recommend using WMI for this:
Get-WmiObject Win32_Process | Where-Object {
$_.GetOwner().User -eq 'admin' -and
$_.Name -eq 'tm1top.exe'
} | ForEach-Object {
$_.Terminate()
}

Batch file or Powershell: How to Kill all Process from a particular user

I wonder how you can write a batch file or powershell script that, given an input of a user name, that it goes and kill every processes that is owned by that user.
For example, if I input user name: testuser. I want the script to go and kill every system processes that its owner is testuser.
On a Windows Server 2008, 2012.
Thanks.
use the command taskkill on windows server, you can kill any processes owned by specific user.
TASKKILL /F /FI "USERNAME eq "
Using powershell you can list all processes started by a specified user:
Get-Process -IncludeUserName | where {$_.UserName -eq "<Username here>"}
To kill/stop a process, either call .kill() on each result or feed the result into Stop-Process.

How to set the Background Process priority at Windows IOT startup

I found how to create and start a background process, how to add it to startup and so on. Quite simple.
The only step I need is how to increase the priority of the process I created at startup.
I saw that, from PowerShell, I can type:
$prog = Get-Process -Name backgroundTaskHost
$prog.PriorityClass = [System.Diagnostics.ProcessPriorityClass]::High
and this works nice, I can check it running the command:
Get-Process | Format-Table -View priority
How to start the process with an higher priority? Is there any setting, command, or another method that allow to create at startup an higher priority background process?
There are 11 levels of the task. RealTime has the highest priority, its value is 0, is higher than High. You can start the process with an higher priority by running a PowerShell Script on Startup. You can follow these steps:
First, create a new file, name it StartupScript.ps1 and add the following lines of PowerShell code:
$prog = Get-Process -Name backgroundTaskHost
$prog.PriorityClass = [System.Diagnostics.ProcessPriorityClass]::RealTime
Second, create a simple batch (*.bat) file that will execute the PowerShell script. Create a new file, name it “Startup.bat” and populate it with the following code:
powershell -command "C:\StartupScript.ps1"
Third, move the script and batch file to the IoT device. You can access IoT disk by File Explorer, enter following line in address bar(use your Raspberry Pi's name or IP address instead of here "minwinpc"):
\\minwinpc\C$
After that, you will see it like this picture shows:
Fourth, establish a PowerShell session with your IoT Core device and add the “C:\Windows\System32” folder permanently to your path by executing the following command:
setx PATH "%PATH%;C:\Windows\System32"
Add a startup scheduled task by executing the following command:
schtasks /create /tn "Startup PowerShell" /tr c:\Startup.bat /sc onstart /ru SYSTEM
Finally, reboot your device. When your device comes back online you can check the result by running the following command:
Get-Process | Format-Table -View priority

Resources