Adapt windows 2003 server script to windows 2000 server

I have the next script for windows 2003 server, working very well, and i need to adapt it to work in windows 2000, when i try to execute it (as a .bat file) i get the next error

@echo off
setlocal enabledelayedexpansion
set Process=dllhost
set Limit=80
set LogFile=C:\Temp\dllhost.log
:loopPerf
set Usage=0
for /f "delims=" %%a in ('wmic.exe path Win32_PerfFormattedData_PerfProc_Process Where "Name='%Process%' or name like '%Process%#%%'" get PercentProcessorTime^,IDProcess /value ^| find.exe "="') do (
	for /f "tokens=1,2 delims==" %%w in ("%%a") do (
		set %%w=%%x
		if "%%w"=="PercentProcessorTime" (
			if !PercentProcessorTime! geq %Limit% (
				taskkill.exe /pid !IDProcess!
				>>"%LogFile%" echo [%Date%][%Time%] Killed %Process% with PID !IDProcess!.
			) else (
				echo usage for PID !IDProcess! at !PercentProcessorTime! at %time%
			)
		)
	)
)
ping.exe -n 61 localhost >NUL
goto loopPerf

Open in new window


Error returned:

WMIC not recognized as an internal or external command

I assume there must be some other interpreter instead of wmic??
LVL 1
celticianAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Neil RussellTechnical Development LeadCommented:
My only comment would be..

Why are you using TWO outdated, unsupported, retired OSes?
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
WMIC does not exist on W2000. Your best option is to run the script remotely, as you can access WMI from another PC (XP and above).
celticianAuthor Commented:
Theser servers are being used at my company, they will be replaced in a few weeks... so i just need this kind of patch working for this long.... but this is not a long term solution.
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

oBdACommented:
I've changed the script to support remoting; it might work if you run it on your W2k3 server against the W2k machine.
@echo off
setlocal enabledelayedexpansion
set Server=acme
set Process=dllhost
set Limit=80
set LogFile=C:\Temp\dllhost.log
:loopPerf
set Usage=0
for /f "delims=" %%a in ('wmic.exe /node:%Server% path Win32_PerfFormattedData_PerfProc_Process Where "Name='%Process%' or name like '%Process%#%%'" get PercentProcessorTime^,IDProcess /value ^| find.exe "="') do (
	for /f "tokens=1,2 delims==" %%w in ("%%a") do (
		set %%w=%%x
		if "%%w"=="PercentProcessorTime" (
			if !PercentProcessorTime! geq %Limit% (
				taskkill.exe /s %Server% /pid !IDProcess! /f
				>>"%LogFile%" echo [%Server%][%Date%][%Time%] Killed %Process% with PID !IDProcess!.
			) else (
				echo [%Server%] Usage for PID !IDProcess! at !PercentProcessorTime! at %time%
			)
		)
	)
)
ping.exe -n 61 localhost >NUL
goto loopPerf

Open in new window

celticianAuthor Commented:
Ok so i must connect to the remote server from the script in the W2k3?

Is there any way i can check the connetion between servers first?

I like the idea, because the 2003 server has more resources available.
oBdACommented:
Well, the main resources are used by WMI, which will still be running on the target server.
This will query while in the monitoring loop:
@echo off
setlocal enabledelayedexpansion
set Server=acme
set Process=dllhost
set Limit=80
set LogFile=C:\Temp\dllhost.log
:loopPerf
ping.exe -4 -n 2 %Server% | find.exe /i "TTL" >NUL
if errorlevel 1 (
	echo [%Server%] Offline at %time%
) else (
	set Usage=0
	for /f "delims=" %%a in ('wmic.exe /node:%Server% path Win32_PerfFormattedData_PerfProc_Process Where "Name='%Process%' or name like '%Process%#%%'" get PercentProcessorTime^,IDProcess /value ^| find.exe "="') do (
		for /f "tokens=1,2 delims==" %%w in ("%%a") do (
			set %%w=%%x
			if "%%w"=="PercentProcessorTime" (
				if !PercentProcessorTime! geq %Limit% (
					taskkill.exe /s %Server% /pid !IDProcess! /f
					>>"%LogFile%" echo [%Server%][%Date%][%Time%] Killed %Process% with PID !IDProcess!.
				) else (
					echo [%Server%] Usage for PID !IDProcess! at !PercentProcessorTime! at %time%
				)
			)
		)
	)
)
ping.exe -n 3 localhost >NUL
goto loopPerf

Open in new window

celticianAuthor Commented:
I think i should authenticate somehow into the remote host, shouldn't i?

im not asked for user/password anywhere.

I get the next error when running the script:

Error:
Código ( CODE) = 0x800706ba
Descripción (descriptioN) = El servidor RPC no está disponible. (RPC server not available)
Recurso (resource) = Win32

Open in new window

oBdACommented:
Assuming those machines are in the same domain, and you're logged on as Domain Admin, it should work.
Check if the Windows Remote Management service (or something similar; it's been too long since I had a W2k machine in front of me) is running on the W2k server. If it's set to Manual or Disabled, set it to Automatic and start it, then try again.
celticianAuthor Commented:
they are in the same domain, have different IPs but i really dont know the permissions of the user i use to log in (i assume its an admin), ill check it with the systems department.

isn't there any way of adapting the script to a w2k machine and run it locally instead?
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Converting the script to VBS should work, but needs some effort. Usually it is much easier to check for remote WMIC access.
oBdACommented:
Well, wmic.exce just isn't available on Windows 2000.
But you need to be a local administrator anyway to kill a process that wasn't started by you, so once remote management is enabled, this should work remotely as well.
celticianAuthor Commented:
Ok, today ill get an answer from the systems department, unfortunately this company is so large, that internal bureaucracy takes so long. :(
celticianAuthor Commented:
i got an answer, they dont give support to W2K anylonger as the machines will be shut down these months...

So back to the first question, isn't it possible to adapt it?
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You set up Performance Monitoring for dllhost, and define a trigger action if the CPU load is more than the limit for x minutes. The action is just the taskkill.

This is the proper way to do it. Should work on W2000 and above.
oBdACommented:
celtician,
did you check the Windows Remote Management service on the W2k machine?

Qlemo,
Relevant word being "should" - in my experience, performance monitoring tasks are hit and miss, leaning towards the miss (at least on W2k/W2k3). Especially after a reboot of the machine, the alert might or just might not fire.
And taskkill.exe only came with XP/W2k3 and later.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Stupid me, of course taskkill is not available on W2k.
Never had issues with perfmon tasks, but didn't use them that much to disagree.
celticianAuthor Commented:
Im checking if that service is enabled... i assume this has nothing to do with remote desktop (which i use to connect to the machine, as i dont have phisical access) right???
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
WinRM is not related to RDP, celtician.
celticianAuthor Commented:
Ok i have enabled it (my  systems department) and confirmed to be so, however i still get this error:

Error:
Código (code) = 0x800706ba
Descripción = El servidor RPC no está disponible. (RCP server is not available)
Recurso (resource) = Win32

Shoud i use in the script the other server's name or the IP??
celticianAuthor Commented:
So any hints?? the Windows Remote Management has been enabled yet... but it keeps failing to work.
oBdACommented:
So you're trying to connect from the W2k3 machine to the W2k machine?
Can you start wbemtest.exe locally on the W2k machine and connect to the root\cimv2 namespace?
Are both machines in the same subnet?
Any active (soft- or hardware) firewall between them?
celticianAuthor Commented:
I really don't have physical access to these machines, i know they are in the same building and internal corporate network but i do not know anything else (fierworks, subnets. etc...) besides their IP addresses.

is there any way i can make an easy check of the connection between them?

I really would prefer adapting the script to the local machine instead... i think it would be easier...
oBdACommented:
Start as cscript.exe /nologo "C:\Temp\Whatever.vbs":
strProcess = "dllhost"
intLimit = 80
strLogFile = "C:\Temp\dllhost.log"

Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
While True
	Set colPerf = objWMI.ExecQuery("Select * From Win32_PerfFormattedData_PerfProc_Process Where Name='" & strProcess & "' Or Name like '" & strProcess & "#%'")
	If colPerf.Count = 0 Then
		Wscript.StdErr.WriteLine "No Instance(s) Available."
	Else
		For Each objPerf in colPerf
			intPercentProcessorTime = CInt(objPerf.PercentProcessorTime)
			If intPercentProcessorTime > intLimit Then
				Set objLog = objFSO.OpenTextFile(strLogFile, ForAppending, True)
				objLog.WriteLine "[" & Date & "][" & Time & "] Killed " & strProcess & " with PID " & objPerf.IDProcess & "."
				objLog.Close
				Set colProcess = objWMI.ExecQuery("Select * From Win32_Process Where ProcessId='" & objPerf.IDProcess & "'")
				For Each objProcess in colProcess
					objProcess.Terminate
				Next
			Else
				Wscript.StdOut.WriteLine "Usage for PID " & objPerf.IDProcess & " at " & objPerf.PercentProcessorTime & " at " & Time
			End If
		Next
	End If
	Wscript.Sleep 60 * 1000
Wend

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
celticianAuthor Commented:
Thanks for the answer OBdA

When executing it (locally in the W2k machine) from the cmd window (i renamed the script as myscript.vbs and placed it in the root folder c:/), i get the next error:

C:\>cscript.exe /nologo "C:\myscript.vbs"
C:\myscript.vbs(10, 2) SWbemObjectSet: Non specified Error

Open in new window


What should i check?
celticianAuthor Commented:
I think i should open another question to solve this, as the adaptation has been made, but now the problems that may arise may have nothing to do!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows 2000

From novice to tech pro — start learning today.