다음을 통해 공유


이벤트에 따라 명령줄에서 프로그램 실행

CommandLineEventConsumer 클래스는 지정된 이벤트가 발생할 때 명령줄에서 지정된 실행 프로그램을 실행합니다. 이 클래스는 WMI에서 제공하는 표준 이벤트 소비자입니다.

CommandLineEventConsumer사용하는 경우 시작하려는 실행 파일을 보호해야 합니다. 실행 파일이 안전한 위치에 있지 않거나 ACL(강력한 액세스 제어 목록)으로 보호되지 않는 경우 액세스 권한이 없는 사용자는 실행 파일을 다른 실행 파일로 바꿀 수 있습니다. Win32_LogicalFileSecuritySetting 또는 Win32_LogicalShareSecuritySetting 클래스를 사용하여 파일 또는 공유의 보안을 프로그래밍 방식으로 변경할 수 있습니다. 자세한 내용은 C++새 개체에 대한 보안 설명자 만들기를 참조하세요.

표준 소비자를 사용하는 기본 절차는 항상 동일하며, 이는 표준 소비자를 사용하여 이벤트를 모니터링하고 응답하는 항목에 위치해 있습니다. 다음 절차는 기본 프로시저에 추가하고, CommandLineEventConsumer 클래스와 관련이 있으며, 프로그램을 실행하는 이벤트 소비자를 만드는 방법을 설명합니다.

주의

CommandLineEventConsumer 클래스에는 특별한 보안 제약 조건이 있습니다. 이 표준 소비자는 로컬 컴퓨터의 Administrators 그룹의 로컬 멤버에 의해 구성되어야 합니다. 도메인 계정을 사용하여 구독을 만드는 경우 LocalSystem 계정에는 작성자가 로컬 관리자 그룹의 구성원인지 확인하는 데 필요한 도메인 권한이 있어야 합니다.

CommandLineEventConsumer 대화형으로 실행되는 프로세스를 시작하는 데 사용할 수 없습니다.

 

다음 절차에서는 명령줄에서 프로세스를 실행하는 이벤트 소비자를 만드는 방법을 설명합니다.

명령줄 프로세스를 실행하는 이벤트 소비자를 만들려면

  1. MOF(관리 개체 형식) 파일에서 쿼리에서 요청하는 이벤트를 수신하는 CommandLineEventConsumer 인스턴스를 만듭니다. 자세한 내용은 MOF(Managed Object Format) 클래스를 디자인하려면을 참조하세요.
  2. __EventFilter 인스턴스를 만들고 이름을 지정합니다.
  3. 이벤트 유형을 지정하는 쿼리를 만듭니다. 자세한 내용을 보려면 WQL을 사용하여 쿼리을 참조하세요.
  4. 필터를 CommandLineEventConsumer인스턴스와 연결하는 __FilterToConsumerBinding 인스턴스를 만듭니다.
  5. Mofcomp.exe사용하여 MOF 파일을 컴파일합니다.

본보기

다음 코드 예제에서는 "MyCmdLineConsumer"라는 새 클래스를 만들어 MOF의 끝에 새 클래스의 인스턴스를 만들 때 이벤트를 생성합니다. 예제는 MOF 코드에 있지만 WMI 스크립팅 API 또는 WMI COM API를 사용하여 프로그래밍 방식으로 인스턴스를 만들 수 있습니다.

다음 절차에서는 MyCmdLineConsumer라는 새 클래스를 만드는 방법을 설명합니다.

MyCmdLineConsumer라는 새 클래스를 만들려면

  1. "calc.exe"와 같이 표시되는 프로그램을 실행하는 명령을 사용하여 c:\cmdline_test.bat 파일을 만듭니다.
  2. MOF를 텍스트 파일에 복사하고 .mof 확장명과 함께 저장합니다.
  3. 명령 창에서 mofcomp filename.mof 명령을 사용하여 MOF 파일을 컴파일합니다.

메모

cmdline_test.bat에 지정된 프로그램이 실행되어야 합니다.

 

// Set the namespace as root\subscription.
// The CommandLineEventConsumer is already compiled
// in the root\subscription namespace. 
#pragma namespace ("\\\\.\\Root\\subscription")

class MyCmdLineConsumer
{
 [key]string Name;
};

// Create an instance of the command line consumer
// and give it the alias $CMDLINECONSUMER

instance of CommandLineEventConsumer as $CMDLINECONSUMER
{
 Name = "CmdLineConsumer_Example";
 CommandLineTemplate = "c:\\cmdline_test.bat";
 RunInteractively = True;
 WorkingDirectory = "c:\\";
};    

// Create an instance of the event filter
// and give it the alias $CMDLINEFILTER
// The filter queries for instance creation event
// for instances of the MyCmdLineConsumer class

instance of __EventFilter as $CMDLINEFILTER
{
    Name = "CmdLineFilter";
    Query = "SELECT * FROM __InstanceCreationEvent"
        " WHERE TargetInstance.__class = \"MyCmdLineConsumer\"";
    QueryLanguage = "WQL";
};

// Create an instance of the binding
// between filter and consumer instances.

instance of __FilterToConsumerBinding
{
     Consumer = $CMDLINECONSUMER;
     Filter = $CMDLINEFILTER;
};

// Create an instance of this class right now. 
// The commands in c:\\cmdline_test.bat execute
// as the result of creating the instance
// of MyCmdLineConsumer.
 
instance of MyCmdLineConsumer
{
     Name = "CmdLineEventConsumer test";
};

표준 소비자를 사용하여 이벤트 모니터링 및 응답