[VBS] 파라미터로 실행되는 프로그램을 파라미터 단위로 CPU 사용률 출력 Etc.2011. 4. 1. 22:26
파라미터로 실행되는 프로그램을 파라미터 단위로 CPU 사용률 출력
VBS VB Script
' 프로세스 및 파라미터 정보 - 동적 입력 로직 추가
HostProcess = "notepad" ' 프로세스 명: .exe 제외
sApplication = "ABC.txt|123.txt|DEF.txt" ' 테스트 파라미터
Dim Param(), PID(), N1(), D1(), N2(), D2(), CPUPerf()
Set WMI_Service = GetObject("winmgmts:{impersonationlevel=impersonate}!\root\cimv2")
NumCore = GetNumProcessors
Set colItems = WMI_Service.ExecQuery ("Select * from Win32_Process where name = '"&HostProcess&".exe'")
ProcessCount = colItems.Count
If ProcessCount = 0 Then
Set colItems = Nothing
Set WMI_Service = Nothing
WScript.Quit
End If
Redim Param(ProcessCount), PID(ProcessCount)
arrApplication = Split(sApplication,"|")
intCount = 0
For Each App in arrApplication
For Each Item in colItems
arrCommandLine = Split(Item.CommandLine," ")
If Instr(UCASE(App), UCASE(arrCommandLine(UBound(arrCommandLine)))) Then
Param(intCount) = App
PID(intCount) = Item.ProcessID
intCount = intCount + 1
End If
Next
Next
Redim N1(intCount), D1(intCount), N2(intCount), D2(intCount), CPUPerf(intCount)
Set colItems = Nothing
Set colItems = WMI_Service.ExecQuery ("Select * from Win32_PerfRawData_PerfProc_Process where name = '"&HostProcess&"' or name like '"&HostProcess&"#%'")
For Each CPUPerf1 in colItems
For i = 0 To intCount
If CPUPerf1.IDProcess = PID(i) Then
N1(i) = CPUPerf1.PercentProcessorTime
D1(i) = CPUPerf1.TimeStamp_Sys100NS
End If
Next
Next
WScript.Sleep(500)
Set colItems = WMI_Service.ExecQuery ("Select * from Win32_PerfRawData_PerfProc_Process where name = '"&HostProcess&"' or name like '"&HostProcess&"#%'")
For Each CPUPerf2 in colItems
For i = 0 To intCount
If CPUPerf2.IDProcess = PID(i) Then
N1(i) = CPUPerf2.PercentProcessorTime
D1(i) = CPUPerf2.TimeStamp_Sys100NS
End If
Next
Next
Set colItems = Nothing
For i = 0 to intCount
If ( 0 = (D2(i)-D1(i)) ) then
CPUPerf(i) = 0
Else
CPUPerf(i) = Round(((((N2(i) - N1(i)) / (D2(i) - D1(i))) * 100) / NumCore), 2)
End If
Next
' 출력 부분 - 가공 로직 추가
For i = 0 to intCount-1
WScript.Echo Param(i), PID(i), CPUPerf(i)
Next
' 코어 수 쿼리 함수
Function GetNumProcessors
Set colItems = WMI_Service.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objItem In colItems
On Error Resume Next
GetNumProcessors = objItem.NumberOfLogicalProcessors
If Err.number <> 0 Then
GetNumProcessors = objItem.NumberOfProcessors
End If
On Error GoTo 0
Next
Set colItems = Nothing
End Function