從收集器起始的訂用帳戶中移除事件來源
您可以從收集器起始的訂用帳戶中移除事件來源,而不刪除整個訂用帳戶。 您必須知道您要刪除的事件來源位址。 您可以使用顯示事件收集器訂閱的屬性中顯示的C++範例,找到與訂用帳戶相關聯的事件來源位址,或在命令提示字元中輸入下列命令:
wecutil gs SubscriptionName
若要列出本機電腦上的目前訂閱,您可以使用列出事件收集器訂閱中顯示的C++程式代碼範例,或在命令提示字元中輸入下列命令:
wecutil es
注意
您可以使用這個範例,從收集器起始的訂用帳戶中移除事件來源,或在命令提示字元中輸入下列命令:
wecutil ss SubscriptionName **/esa:**EventSourceAddress /res
EventSourceAddress 可以是本機電腦的localhost或遠端電腦的完整功能變數名稱。
此範例會遵循一系列步驟,從收集器起始的訂用帳戶中移除事件來源。
從收集器起始的訂用帳戶中移除事件來源
- 提供訂用帳戶名稱和訪問許可權作為 EcOpenSubscription 函式的參數,以開啟現有的訂用帳戶。 如需訪問許可權的詳細資訊,請參閱 Windows 事件收集器常數。
- 呼叫 EcGetSubscriptionProperty 函式,以取得訂用帳戶的事件來源陣列。 如需可擷取之訂用帳戶屬性的詳細資訊,請參閱 EC_SUBSCRIPTION_PROPERTY_ID 列舉。
- 藉由呼叫 EcGetObjectArrayProperty 函式,在訂用帳戶的事件來源陣列中搜尋指定的事件來源。 EcSubscriptionEventSourceAddress 屬性的值會是本機計算機的 Localhost,或是遠端電腦的完整功能變數名稱。 如需可擷取的事件來源屬性詳細資訊,請參閱 EC_SUBSCRIPTION_PROPERTY_ID 列舉。
- 呼叫 EcRemoveObjectArrayElement 函式,從訂用帳戶刪除事件來源。
- 呼叫 EcSaveSubscription 函式以儲存訂用帳戶。
- 呼叫 EcClose 函式以關閉訂用帳戶。
下列C++程式代碼範例示範如何從事件收集器訂用帳戶中移除事件來源。
#include <windows.h>
#include <EvColl.h>
#include <vector>
#include <string>
#include <strsafe.h>
#pragma comment(lib, "wecapi.lib")
// Subscription Information
DWORD GetSubscriptionProperty(EC_HANDLE hSubscription,
EC_SUBSCRIPTION_PROPERTY_ID propID,
DWORD flags,
std::vector<BYTE>& buffer,
PEC_VARIANT& vProperty);
DWORD GetEventSourceProperty(EC_OBJECT_ARRAY_PROPERTY_HANDLE hArray,
EC_SUBSCRIPTION_PROPERTY_ID propID,
DWORD arrayIndex,
DWORD flags,
std::vector<BYTE>& buffer,
PEC_VARIANT& vProper);
void __cdecl wmain()
{
EC_HANDLE hSubscription;
std::wstring eventSource = L"localhost";
BOOL foundEventSource = false;
LPCWSTR lpSubname = L"TestSubscription-CollectorInitiated";
DWORD dwEventSourceCount;
DWORD deleteEvent = 0;
DWORD dwRetVal = ERROR_SUCCESS;
PEC_VARIANT vProperty = NULL;
std::vector<BYTE> buffer;
LPVOID lpwszBuffer;
EC_OBJECT_ARRAY_PROPERTY_HANDLE hArray = NULL;
// Step 1: Open an existing subscription.
hSubscription = EcOpenSubscription(lpSubname,
EC_READ_ACCESS | EC_WRITE_ACCESS,
EC_OPEN_EXISTING);
if (!hSubscription)
{
dwRetVal = GetLastError();
goto Cleanup;
}
// Step 2: Get the event sources array to remove an event
// source from the subscription.
dwRetVal = GetSubscriptionProperty(hSubscription,
EcSubscriptionEventSources,
0,
buffer,
vProperty);
if (ERROR_SUCCESS != dwRetVal)
{
goto Cleanup;
}
// Ensure that a handle to the event sources array has been obtained.
if (vProperty->Type != EcVarTypeNull &&
vProperty->Type != EcVarObjectArrayPropertyHandle)
{
dwRetVal = ERROR_INVALID_DATA;
goto Cleanup;
}
hArray = (vProperty->Type == EcVarTypeNull)? NULL:
vProperty->PropertyHandleVal;
if (!hArray)
{
dwRetVal = ERROR_INVALID_DATA;
goto Cleanup;
}
if (!EcGetObjectArraySize(hArray, &dwEventSourceCount))
{
dwRetVal = GetLastError();
goto Cleanup;
}
// Step 3: Search for the specified event source in the event source array.
for (DWORD I = 0; I < dwEventSourceCount; I++)
{
dwRetVal = GetEventSourceProperty(hArray,
EcSubscriptionEventSourceAddress,
I,
0,
buffer,
vProperty);
if (ERROR_SUCCESS != dwRetVal)
{
goto Cleanup;
}
if (vProperty->StringVal == eventSource)
{
foundEventSource = true;
deleteEvent = I;
break;
}
}
// Step 4: If the event source was found in the array, remove it.
if (foundEventSource)
{
if (!EcRemoveObjectArrayElement(hArray, deleteEvent))
{
dwRetVal = GetLastError();
goto Cleanup;
}
}
else
{
wprintf(L"Could not remove the event source from the subscription.\n");
goto Cleanup;
}
// Step 5: Save the subscription to finalize the removal of the event source.
if( !EcSaveSubscription(hSubscription, NULL) )
{
dwRetVal = GetLastError();
goto Cleanup;
}
// Step 6: Close the subscription.
Cleanup:
if (hArray)
EcClose(hArray);
if (hSubscription)
EcClose(hSubscription);
if (dwRetVal != ERROR_SUCCESS)
{
FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwRetVal,
0,
(LPWSTR) &lpwszBuffer,
0,
NULL);
if (!lpwszBuffer)
{
wprintf(L"Failed to FormatMessage. Operation Error Code: %u." \
L"Error Code from FormatMessage: %u\n", dwRetVal, GetLastError());
return;
}
wprintf(L"\nFailed to Perform Operation.\nError Code: %u\n" \
L"Error Message: %s\n", dwRetVal, lpwszBuffer);
LocalFree(lpwszBuffer);
}
}
DWORD GetSubscriptionProperty(EC_HANDLE hSubscription,
EC_SUBSCRIPTION_PROPERTY_ID propID,
DWORD flags,
std::vector<BYTE>& buffer,
PEC_VARIANT& vProperty)
{
DWORD dwBufferSize, dwRetVal = ERROR_SUCCESS;
buffer.resize(sizeof(EC_VARIANT));
if (!hSubscription)
return ERROR_INVALID_PARAMETER;
// Get the value for the specified property.
if (!EcGetSubscriptionProperty(hSubscription,
propID,
flags,
(DWORD) buffer.size(),
(PEC_VARIANT)&buffer[0],
&dwBufferSize))
{
dwRetVal = GetLastError();
if (ERROR_INSUFFICIENT_BUFFER == dwRetVal)
{
dwRetVal = ERROR_SUCCESS;
buffer.resize(dwBufferSize);
if (!EcGetSubscriptionProperty(hSubscription,
propID,
flags,
(DWORD) buffer.size(),
(PEC_VARIANT)&buffer[0],
&dwBufferSize))
{
dwRetVal = GetLastError();
}
}
}
if (dwRetVal == ERROR_SUCCESS)
{
vProperty = (PEC_VARIANT) &buffer[0];
}
else
{
vProperty = NULL;
}
return dwRetVal;
}
DWORD GetEventSourceProperty(EC_OBJECT_ARRAY_PROPERTY_HANDLE hArray,
EC_SUBSCRIPTION_PROPERTY_ID propID,
DWORD arrayIndex,
DWORD flags,
std::vector<BYTE>& buffer,
PEC_VARIANT& vProperty)
{
UNREFERENCED_PARAMETER(flags);
UNREFERENCED_PARAMETER(propID);
DWORD dwBufferSize, dwRetVal = ERROR_SUCCESS;
buffer.resize(sizeof(EC_VARIANT));
if (!hArray)
return ERROR_INVALID_PARAMETER;
// Obtain the value for the specified property.
if (!EcGetObjectArrayProperty(hArray,
EcSubscriptionEventSourceAddress,
arrayIndex,
0,
(DWORD) buffer.size(),
(PEC_VARIANT)&buffer[0],
&dwBufferSize))
{
dwRetVal = GetLastError();
if (ERROR_INSUFFICIENT_BUFFER == dwRetVal)
{
dwRetVal = ERROR_SUCCESS;
buffer.resize(dwBufferSize);
if (!EcGetObjectArrayProperty(hArray,
EcSubscriptionEventSourceAddress,
arrayIndex,
0,
(DWORD) buffer.size(),
(PEC_VARIANT)&buffer[0],
&dwBufferSize))
{
dwRetVal = GetLastError();
}
}
}
if (dwRetVal == ERROR_SUCCESS)
{
vProperty = (PEC_VARIANT) &buffer[0];
}
else
{
vProperty = NULL;
}
return dwRetVal;
}
相關主題