使用 WMI 列舉及啟動所有主控件執行個體
使用下列程式碼,使用 MSBTS_HostInstance 類別列舉和啟動所有主機實例。
using System.Management;
//Function to Enumerate all HostInstances of "InProcess" type and start them
public void EnumerateAndStart()
{
try
{
//Create EnumerationOptions and run wql query
EnumerationOptions enumOptions = new EnumerationOptions();
enumOptions.ReturnImmediately = false;
//Search for all HostInstances of 'InProcess' type in the Biztalk namespace scope
ManagementObjectSearcher searchObject = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer","Select * from MSBTS_HostInstance where HostType=1",enumOptions);
//Enumerate through the result set and start each HostInstance if it is already stopped
foreach(ManagementObject inst in searchObject.Get())
{
//Check if ServiceState is 'Stopped'
if(inst["ServiceState"].ToString() == "1")
{
inst.InvokeMethod("Start",null);
}
Console.WriteLine("HostInstance of Host: " + inst["HostName"] + " and Server: " + inst["RunningServer"] + " was started successfully");
}
Console.WriteLine("All HostInstances started");
return;
}
catch(Exception excep)
{
Console.WriteLine("Failure while starting HostInstances - " + excep.Message);
}
}
Option Explicit
' Host Instance status number
Const HostInstServiceState_Stopped = 1
' Basic WMI operations - Enumerate and invoke method
' Sample to show starting all host instance using MSBTS_HostInstance
Sub StartAllInProcessHostInstance ()
On Error Resume Next
Dim Query, HostInstSet, Inst
' Enumerate all InProcess type Host Instance
Query = "SELECT * FROM MSBTS_HostInstance WHERE HostType =1"
Set HostInstSet = GetObject("Winmgmts:!root\MicrosoftBizTalkServer").ExecQuery(Query)
For Each Inst in HostInstSet
' If host instance is stopped, then it'll start it
If( HostInstServiceState_Stopped = Inst.ServiceState ) Then
wscript.echo "Starting host instance..."
Inst.Start ' Calling MSBTS_HostInstance::Start() method
CheckWMIError
wscript.echo "HostInstance - " & Inst.HostName & " - has been started successfully on server - " & Inst.RunningServer
End If
Next
end Sub
'This subroutine deals with all errors using the WbemScripting object. Error descriptions
'are returned to the user by printing to the console.
Sub CheckWMIError()
If Err <> 0 Then
On Error Resume Next
Dim strErrDesc: strErrDesc = Err.Description
Dim ErrNum: ErrNum = Err.Number
Dim WMIError : Set WMIError = CreateObject("WbemScripting.SwbemLastError")
If ( TypeName(WMIError) = "Empty" ) Then
wscript.echo strErrDesc & " (HRESULT: " & Hex(ErrNum) & ")."
Else
wscript.echo WMIError.Description & "(HRESULT: " & Hex(ErrNum) & ")."
Set WMIError = nothing
End If
wscript.quit 0
End If
End Sub
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將推出:在 2024 年,我們將隨著內容的意見反應機制逐步淘汰 GitHub 問題,並以新的意見反應系統來取代。 如需詳細資訊,請參閱提交並檢視相關的意見反應