9. Public MustInherit Class WMIExecuteActivityBase
Inherits AsyncCodeActivity
Protected _target As String = ""
Protected _where As String = ""
WMI 実行用アクティビティのベース
Protected _service As String = ""
Public Property Scope As InArgument(Of ManagementScope)
Public Property Results As OutArgument(Of List(Of Hashtable))
Private Delegate Function asyncExecuteWMIDelegate(ByVal target As String, ByVal whereCondition As String,
ByVal wmScope As ManagementScope) As List(Of Hashtable)
Protected Overrides Function BeginExecute(context As AsyncCodeActivityContext, callback As AsyncCallback,
state As Object) As IAsyncResult
Dim currentScope = context.GetValue(Me.Scope)
Dim asyncExecute = New asyncExecuteWMIDelegate(AddressOf ExecuteWMI)
context.UserState = asyncExecute
Return asyncExecute.BeginInvoke(_target, _where, currentScope, callback, state)
End Function
Protected Overrides Sub EndExecute(context As AsyncCodeActivityContext, result As IAsyncResult)
Dim asyncExecute = TryCast(context.UserState, asyncExecuteWMIDelegate)
Dim wmiResult = asyncExecute.EndInvoke(result)
context.SetValue(Me.Results, wmiResult)
End Sub
Private Function ExecuteWMI(ByVal target As String, ByVal whereCondition As String,
ByVal wmScope As ManagementScope) As List(Of Hashtable)
End Function
End Class
WMI クエリは時間がかかるので非同期で実行
10. Private Function ExecuteWMI(ByVal target As String, ByVal whereCondition As String,
ByVal wmScope As ManagementScope) As List(Of Hashtable)
Dim result As New List(Of Hashtable)
Try
If wmScope Is Nothing Then
wmScope = New ManagementScope("" + My.Computer.Name + "root" + _service)
End If
If Not wmScope.IsConnected Then wmScope.Connect()
Dim wmQueryString = "SELECT * FROM " + target
If whereCondition.Trim <> "" Then wmQueryString += " WHERE " + whereCondition
Dim wmQuery As New ObjectQuery(wmQueryString)
Using searcher As New ManagementObjectSearcher(wmScope, wmQuery)
Using queryCollection = searcher.Get()
For Each wmResult In queryCollection
Dim resultRecord As New Hashtable
For Each wmResultColumn In wmResult.Properties
resultRecord.Add(wmResultColumn.Name,
wmResultColumn.Value)
Next
If resultRecord.Count > 0 Then result.Add(resultRecord)
Next
End Using
End Using
Catch ex As Exception
result.Clear()
End Try
Return result
WMI 実行部分のロジック例
End Function
ロジックとしては非常に簡単でアクティビティとするのに
それほど手間はかかりません