Skip to content

Instantly share code, notes, and snippets.

@JamesIgoe
Created January 28, 2019 22:01
Show Gist options
  • Save JamesIgoe/76f7497ba9541ca3461b9d1ed9944b66 to your computer and use it in GitHub Desktop.
Save JamesIgoe/76f7497ba9541ca3461b9d1ed9944b66 to your computer and use it in GitHub Desktop.
Imports log4net
Imports System.Management
Imports System.Linq
Namespace GhostImaging
Public Class WmiResult
Property Name As String
Property ProcessId As Int32
Property CpuTime As Single
End Class
Public Interface IServerActions
Function AddProcess(serverName As String, command As String) As Int32
Sub KillProcess(processId As Int32, processName As String, serverName As String)
Function GetProcesses(processName As String, serverName As String) As IList(Of WmiResult)
End Interface
Public Class ServerActions
Implements IServerActions
Private Shared ReadOnly log As ILog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
Public Function AddProcess(serverName As String, command As String) As Int32 Implements IServerActions.AddProcess
Try
log.Info(String.Format("AddProcess: Server = {0}, Command = {1}", serverName, command))
Dim process = {command, Nothing, Nothing, 0}
Dim scope As New ManagementScope("\\" & serverName & "\root\cimv2")
Dim management = New ManagementClass(scope, New ManagementPath("Win32_Process"), New ObjectGetOptions())
management.InvokeMethod("Create", process)
Return process(3)
Catch ex As Exception
log.Error(ex.Message)
Return 0
End Try
End Function
Public Sub KillProcess(processId As Integer, processName As String, serverName As String) Implements IServerActions.KillProcess
Try
log.Info(String.Format("KillProcess: Server = {0}, Process = {1}, ProcessId = {2}", serverName, processName, processId))
Dim scope As New ManagementScope("\\" & serverName & "\root\cimv2")
Dim query As New SelectQuery("SELECT * FROM Win32_Process WHERE Name = '" & processName & ".exe'")
Using searcher As New ManagementObjectSearcher(scope, query)
Dim queryCollection As ManagementObjectCollection = searcher.[Get]()
For Each process As ManagementObject In queryCollection
If process.GetPropertyValue("PROCESSID") = processId Then
process.InvokeMethod("Terminate", Nothing)
Exit For
End If
Next
End Using
Catch ex As Exception
log.Error(ex.Message)
End Try
End Sub
Public Function GetProcesses(processName As String, serverName As String) As IList(Of WmiResult) Implements IServerActions.GetProcesses
Try
log.Debug("GetProceses")
Dim scope As New ManagementScope("\\" & serverName & "\root\cimv2")
Dim query As New SelectQuery("SELECT * FROM Win32_Process WHERE Name = '" & processName & ".exe'")
Using searcher As New ManagementObjectSearcher(scope, query)
Dim remoteProcesses = searcher.[Get]().Cast(Of ManagementObject)().[Select](Function(mo) New With {
Key .Name = mo("Name"),
Key .ProcessId = mo("ProcessID"),
Key .CpuTime = (CSng(mo("KernelModeTime")) + CSng(mo("UserModeTime"))) / 10000000
}).ToArray()
Dim processList As New List(Of WmiResult)
For Each item In remoteProcesses
processList.Add(New WmiResult With {.Name = item.Name, .ProcessId = item.ProcessId, .CpuTime = item.CpuTime})
Next
Return processList
End Using
Catch ex As Exception
log.Error(ex.Message)
Return New List(Of WmiResult)
End Try
End Function
End Class
End Namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment