i = wscript.arguments.count
object = ""
counter = ""
instance = ""
warn = ""
critical = ""
outformat = ""
for x=0 to i-1
	test = wscript.arguments(x)
	if (test = "-C") then
		object = wscript.arguments(x+1)
	end if
	if (test = "-P") then
		counter = wscript.arguments(x+1)
	end if
	if (test = "-I") then
		instance = wscript.arguments(x+1)
	end if
	if (test = "-w") then
		warn = ccur(wscript.arguments(x+1))
	end if
	if (test = "-c") then
		critical = ccur(wscript.arguments(x+1))
	end if
	if (test = "-f") then
		outformat = wscript.arguments(x+1)
	end if
next

if (object = "" or counter = "" or warn = "" or critical = "" or outformat = "") then
	wscript.echo "Error processing arguments"
	wscript.quit(3)
end if

Set objShell = CreateObject("WScript.Shell")
Set objEnv = objShell.Environment("Process")
Set fso = CreateObject("Scripting.FileSystemObject")

Randomize

filename = objEnv("TEMP") & "\perfcounter" & Int((1000)*Rnd+min) & ".log" 

if fso.FileExists(filename) then
	fso.DeleteFile(filename) 
end if

objShell.Run "%comspec% /c ""typeperf ""\" & object & "(" & instance & ")\" & counter & """ -sc 1 > " & filename & """", 0, True

set input = fso.OpenTextFile(filename, 1) 
output = input.readline
output = input.readline
output = input.readline
input.close
set input = nothing 
if fso.FileExists(filename) then
	fso.DeleteFile(filename) 
end if

arrOut = Split(output, ",", -1, 1)

value = ccur(replace(arrOut(1), """", ""))

output = replace(outformat, "%f", value)
output = replace(output, "%%", "%")

wscript.echo output

if (warn > critical) and (value < critical) then
	wscript.quit(2)
end if
if (warn > critical) and (value < warn) then
	wscript.quit(1)
end if
if (warn < critical) and (value > critical) then
	wscript.quit(2)
end if
if (warn < critical) and (value > critical) then
	wscript.quit(1)
end if

wscript.quit(0)