Skip to content

Instantly share code, notes, and snippets.

@hirman74
Created October 23, 2022 00:40
Show Gist options
  • Save hirman74/0ed30449703daebe24618aa7079e8ca4 to your computer and use it in GitHub Desktop.
Save hirman74/0ed30449703daebe24618aa7079e8ca4 to your computer and use it in GitHub Desktop.
strComputer = "."
Dim objShell, objWMIService
Set objShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Dim colItems, NumS, MemorySize, CPU1, CPU2, ProcessId, strProcess, strProcessKill, MemorySize1, MemorySize2, AverageCPU
Wscript.Sleep 10000
strProcessKill = "'TASKMGR.EXE'"
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process Where Name = " & strProcessKill)
For Each objProcess in colItems
objProcess.Terminate()
Next
strProcessKill = "'DRWTSN32'"
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process Where Name = " & strProcessKill)
For Each objProcess in colItems
objProcess.Terminate()
Next
Function CPUUSage (ProcID)
For Each objInstance1 in objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfProc_Process Where IDProcess = " & ProcID )
N1 = objInstance1.PercentProcessorTime
D1 = objInstance1.Timestamp_Sys100NS
Next
Wscript.Sleep (1000)
For Each objInstance1 in objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfProc_Process Where IDProcess = " & ProcID )
N2 = objInstance1.PercentProcessorTime
D2 = objInstance1.Timestamp_Sys100NS
Next
PercentProcessorTime = ((N2 - N1) / (D2 - D1)) * 100
CPUUSage = Round ( PercentProcessorTime , 0)
End Function
NumS = 0
Do Until NumS > 3
Do Until NumS > 0
strProcess = "'OpcBridgeServic'"
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = " & strProcess,,48)
For Each objItem in colItems
MemorySize = Round(objItem.WorkingSetSize / 1024)
Next
If MemorySize > 540000 Then
NumS = 1
End If
Set colItems = Nothing
Loop
MemorySize = ""
If NumS > 0 Then
Do Until NumS > 1
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = " & strProcess,,48)
For Each objItem in colItems
CPU1 = CPUUSage(objItem.Handle)
Next
Set colItems = Nothing
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = " & strProcess,,48)
For Each objItem in colItems
CPU2 = CPUUSage(objItem.Handle)
Next
Set colItems = Nothing
AverageCPU = (CPU1 + CPU2) / 2
If AverageCPU < 15 Then
NumS = 2
End If
Loop
End If
If NumS > 1 Then
NumS = 4
End If
Loop
MemorySize = ""
strProcess = "'Client Builder.'"
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = " & strProcess,,48)
For Each objItem in colItems
MemorySize = Round(objItem.WorkingSetSize / 1024)
Next
Set colItems = Nothing
If MemorySize = "" Then
StringCMD = "cmd /c c:\ATS_ODS_Mainline\CCL_MMI_Mainline.fvp"
objShell.Run StringCMD, 0 , False
Wscript.Sleep 20000
NumS = 0
Do Until NumS > 1
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = " & strProcess,,48)
For Each objItem in colItems
CPU1 = CPUUSage(objItem.Handle)
Next
Set colItems = Nothing
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = " & strProcess,,48)
For Each objItem in colItems
CPU2 = CPUUSage(objItem.Handle)
Next
Set colItems = Nothing
AverageCPU = (CPU1 + CPU2) / 2
If AverageCPU < 15 Then
NumS = 2
End If
Loop
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = " & strProcess,,48)
For Each objItem in colItems
ProcessId = objItem.ProcessId
Next
If objShell.AppActivate(ProcessId) Then
objShell.SendKeys "+{F8}"
End If
Wscript.Sleep 2000
Set colItems = Nothing
strProcessKill = "'CMD.EXE'"
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process Where Name = " & strProcessKill)
For Each objProcess in colItems
objProcess.Terminate()
Next
strProcess = "'Client Builder.'"
NumS = 0
Do Until NumS > 1
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = " & strProcess,,48)
For Each objItem in colItems
CPU1 = CPUUSage(objItem.Handle)
Next
Set colItems = Nothing
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = " & strProcess,,48)
For Each objItem in colItems
CPU2 = CPUUSage(objItem.Handle)
Next
Set colItems = Nothing
AverageCPU = (CPU1 + CPU2) / 2
If AverageCPU < 30 Then
NumS = 2
End If
Loop
End If
MemorySize1 = ""
strProcess = "'OpcBridgeServic'"
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = " & strProcess,,48)
For Each objItem in colItems
MemorySize1 = Round(objItem.WorkingSetSize / 1024)
Next
Set colItems = Nothing
MemorySize2 = ""
strProcess = "'Gwx32.exe'"
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = " & strProcess,,48)
For Each objItem in colItems
MemorySize2 = Round(objItem.WorkingSetSize / 1024)
Next
Set colItems = Nothing
If (MemorySize1 <> "") AND (MemorySize2 = "") Then
strProcess = "'OpcBridgeServic'"
NumS = 0
Do Until NumS > 1
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = " & strProcess,,48)
For Each objItem in colItems
CPU1 = CPUUSage(objItem.Handle)
Next
Set colItems = Nothing
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = " & strProcess,,48)
For Each objItem in colItems
CPU2 = CPUUSage(objItem.Handle)
Next
Set colItems = Nothing
AverageCPU = (CPU1 + CPU2) / 2
If AverageCPU < 10 Then
NumS = 2
End If
Loop
strProcess = "'Gwx32.exe'"
StringCMD = "cmd /c C:\transActive\config\G-gui\G01-GWX_Runtime\G0104-Displays\ODS\ODS_PWR_TPSS.gdf"
objShell.Run StringCMD, 0 , False
Wscript.Sleep 5000
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = " & strProcess,,48)
For Each objItem in colItems
ProcessId = objItem.ProcessId
Next
Set colItems = Nothing
If objShell.AppActivate(ProcessId) Then
objShell.SendKeys "^m"
End If
Wscript.Sleep 2000
strProcessKill = "'CMD.EXE'"
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process Where Name = " & strProcessKill)
For Each objProcess in colItems
objProcess.Terminate()
Next
Set colItems = Nothing
Wscript.Sleep 5000
Const ABOVE_NORMAL = 32768
strProcess = "'Gwx32.exe'"
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = " & strProcess,,48)
For Each objItem in colItems
objItem.SetPriority(ABOVE_NORMAL)
Next
End If
Set objShell = Nothing
Set objWMIService = Nothing
Set colItems = Nothing
Set NumS = Nothing
Set MemorySize = Nothing
Set CPU1 = Nothing
Set CPU2 = Nothing
Set ProcessId = Nothing
Set strProcess = Nothing
Set strProcessKill = Nothing
Set MemorySize1 = Nothing
Set MemorySize2 = Nothing
Set AverageCPU = Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment