Windows 2008R2 :: Taskkill in a scheduled task - taskkill

I'm using
TASKKILL /FI "IMAGENAME eq xyz.exe" /FI "STATUS eq UNKNOWN" /F
in a batch which is run as scheduled task.
When I run the batch manually, the filters work fine. When I run it from ScheduledTasks with RunNow, all processes of xyz.exe are killed.
Any ideas ?
Regards,
Thomas

Related

Shutdown Remote Server at 3AM

I'm trying to shutdown/restart a remote server at 3AM using this script
shutdown /r /f /t $([int]([datetime]"03:00AM"-(Get-Date)).TotalSeconds)
But PS doesn't like it because it's going into the next day, if I run this instead it works:
shutdown /r /f /t $([int]([datetime]"11:59AM"-(Get-Date)).TotalSeconds)
I've looked around but can't figure out how to get this command to run at 3AM, anyone any ideas?
Check if "3:00AM" is in the past, then add 1 day:
$threeAM = [datetime]"03:00AM"
if($threeAM -lt (Get-Date)){
$threeAM = $threeAM.AddDays(1)
}
shutdown /r /f /t $([int]($threeAM - (Get-Date)).TotalSeconds)

cmd.exe throws error "& was unexpected at this time."

so my problem is that when i just ran cmd.exe in terminal, i get "& was unexpected at this time." Error at the end - looks like this
So the problem is that i'm getting erros in Unity 3D when it wants to run the unity_csc.bat file and compile solution. These errors are exactly the same as the one when i just run cmd.exe - therefore i suspect its not an Unity3D based problem (if you want you can check the Unity3D specific thread here https://forum.unity.com/threads/2-empty-errors-in-console-was-unexpected-at-this-time.799110/ )
Does anyone know why this might be happening ? This also happens when i try to run a .bat file - which I suspect is why i cant compile Unity project
I'm running Windows 10 with all of the latest updates
EDIT:
Since cmd /d does not throw the error, might there be some problem with this registry record ?
In User Folder i do have this Autorun record
#mode 20,5 & tasklist /FI "IMAGENAME eq SoundMixer.exe" 2>NUL | find /I /N "SoundMixer.exe">NUL && exit & if exist " ( start /MIN "" " & tasklist /FI "IMAGENAME eq explorer.exe" 2>NUL | find /I /N "explorer.exe">NUL && exit & explorer.exe & exit ) else ( tasklist /FI "IMAGENAME eq explorer.exe" 2>NUL | find /I /N "explorer.exe">NUL && exit & explorer.exe & exit )
Stephan has provided the crucial pointer:
It sounds like you have a broken autorun command defined for cmd.exe; that is, your registry defines a command that is automatically executed whenever you call cmd.exe, and that command causes the syntax error you're seeing.
Note that such commands are executed irrespective of whether you open an interactive cmd session or invoke via a batch file or pass a command to cmd with /C.
Passing /D to cmd bypasses any autorun commands.
There are two locations in the registry where such a command can be defined, one at the local-machine level (which applies to all users), HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor, and another for the current user only, HKEY_CURRENT_USER\Software\Microsoft\Command Processor, in a value named AutoRun.
If commands are defined in both locations, the HKEY_LOCAL_MACHINE's commands run first.
To list any defined autorun commands:
Get-ItemProperty -ea Ignore ('HKCU:', 'HKLM:' -replace '$', '\Software\Microsoft\Command Processor') AutoRun
You can use the following PowerShell snippet to remove autorun commands from both locations, but note that you'll have to run it with elevation (as administrator), if a local-machine value is present:
Get-ItemProperty -ea Ignore ('HKCU:', 'HKLM:' -replace '$', '\Software\Microsoft\Command Processor') AutoRun |
Remove-ItemProperty -Name AutoRun -WhatIf
Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf, once you're sure the operation will do what you want.

Windows Service not stopping in Windows Server 2012 but easily on Windows 7

I have a below script which is attached to a windows service on stop command:-
wmic service where name='%NS_SERVICE_NAME%' get ProcessId | more +1 > tmp.txt
set /p NS_PID=<tmp.txt
del tmp.txt
for /f %%i in ('wmic process where "(ParentProcessId=%NS_PID%)" get ProcessId ^| more +1') do (
for /f %%a in ('wmic process where "(ParentProcessId=%%i)" get ProcessId ^| more +1') do (
echo %%a|findstr "^[-][1-9][0-9]*$ ^[1-9][0-9]*$ ^0$">nul && TASKKILL /F /PID %%a || rem
IF %ERRORLEVEL% NEQ 0 do (
exit
)
)
)
which works fine on Windows 7 and service stops quickly in 1-2 seconds, but in case of Windows Server 2012 the service does not stop and keeps in stopping state unless the process related to it is ended.
The error which I get in logs is as below:- "No Instance(s) Available."

How do Loop this command?

I'm new to this community and new to coding. I'm trying to figure out if there is a way to repeat a command like this
CMD /k Taskkill /IM A.exe /F
CMD /k Taskkill /IM B.exe /F
CMD /k Taskkill /IM C.exe /F
CMD /k Taskkill /IM D.exe /F
CMD /k Taskkill /IM E.exe /F
I'm currently using this code to do the same thing
:A
Taskkill /IM A.exe /F
Taskkill /IM B.exe /F
Taskkill /IM C.exe /F
Taskkill /IM D.exe /F
Taskkill /IM E.exe /F
Goto A
Although the code I'm using can be faulty because the batch program does not execute simultaneously. I need help looking for a way to execute the commands simultaneously in batch or any other kind of language. Your help will be much appreciated.
Use a loop, be it a for or a while. Using goto for looping is not used anymore in any modern language that I can think of! That kind of iteration is used in assembler, or in very primitive scripting/programming languages. Haven't you coded anything in the last, say, 20 years or so? :P
- while or for loop will do the work.
Eg:
while(true){ // Its a infinite loop
dosomething...
dosomething....
}
Or
for(;;){ // Its a infinite loop
dosomething...
dosomething....
}

RunOnceEx - Reboot System after each Installation

Is there any way I can reboot my computer after each component installation specified in RunOnceEx.CMD file?
I am creating a unattended setup disk for windows XP which would install some default software after installing windows on the system. I am using RunOnceEx.cmd file to define the software that needs to be installed, what I want is to reboot my the system after installation of each software.
Thanks and regards,
Yep there is. Although it's not a supported feature. I do something similar.
This might not be the the fanciest solution but it works reliably. The key is to stop the RunOnceEx process (rundll32.exe) before commencing the reboot procedure. If it's not stopped, Windows will stop all processes before shutting down in an unknown order. And if that order means killing our "Reboot" process before killing the RunOnceEx process it will continue on the next item on the RunOnceEx list before being killed (and thus aborted, which is not what we want).
The simple answer, add a reboot key that kills the RunOnceEx process and then reboots:
set %KEY%=HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnceEx
REG ADD %KEY%\009 /ve /D "Reboot.." /f
REG ADD %KEY%\009 /v 1 /D "cmd.exe /c taskkill.exe /f /im rundll32.exe & shutdown /r /t 0 /f" /f
This might leave remnant keys during next startup. So to make it look cleaner, add an instruction to remove the key manually before killing and rebooting:
set %KEY%=HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnceEx
REG ADD %KEY%\009 /ve /D "Reboot.." /f
REG ADD %KEY%\009 /v 1 /D "cmd.exe /c REG DELETE %key%\009 /va /f & taskkill.exe /f /im rundll32.exe & shutdown /r /t 0 /f" /f
Hope it helps.
Edit:
In XP you might have to use tskill instead of taskkill, but the principle is the same.