{"id":663,"date":"2014-09-18T00:01:00","date_gmt":"2014-09-18T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/09\/18\/use-powershell-to-monitor-specific-process-creation\/"},"modified":"2022-06-21T14:25:58","modified_gmt":"2022-06-21T21:25:58","slug":"use-powershell-to-monitor-specific-process-creation","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-monitor-specific-process-creation\/","title":{"rendered":"Use PowerShell to Monitor Specific Process Creation"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to monitor for the creation of specific processes.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning it is beginning to look like autumn. Who knows, it may become really hot and humid over the weekend, but today I can delude myself into hearing the rustling of leaves, imagine cool breezes blowing across the yard, and think of squirrels as they quickly gather supplies for a long winter.<\/p>\n<p>All of these things are really quite simple, routine, and ordinary. And yet they demand a certain amount of preparation. If I don\u2019t clean the leaf troughs in the early autumn, winter rains and subsequent freezing could spell disaster for the roof. If I don\u2019t roll up hoses and bring them into the garage, pipes could freeze. If the squirrels don\u2019t gather enough supplies for a long winter, they will be out scampering in the snow and risk untoward dangers.<\/p>\n<p>And so it is with our computer systems\u2014they demand monitoring, planning, and preparation for contingencies.<\/p>\n<p>In yesterday\u2019s post, <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-monitor-for-process-startup\/\" target=\"_blank\" rel=\"noopener\">Use PowerShell to Monitor for Process Startup<\/a>, I talked about using the <strong>Register-CimIndicationEvent<\/strong> cmdlet to monitor for process startup. It worked well\u2014maybe a bit too well because it triggers an event for every new process that starts. And in a modern operating system like Windows\u00a08.1, there are all kinds of processes that start and stop all the time.<\/p>\n<p>Today I want to use a query to modify the behavior of <strong>Register-CimIndicationEvent<\/strong>. To do this, I will use the Windows Management Instrumentation Query Language (WQL). Don\u2019t worry. Because I am using the Win32_ProcessStartTrace WMI class, this will be really easy.<\/p>\n<p style=\"margin-left:30px\">\n  <b>Note<\/b>\u00a0 For more information about WMI event monitoring, see <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/an-insiders-guide-to-using-wmi-events-and-powershell\/\" target=\"_blank\" rel=\"noopener\">An Insider\u2019s Guide to Using WMI Events and PowerShell<\/a>. <b><i><\/i><\/b>\n<\/p>\n<p>Here are the steps I will take to permit me to monitor for the startup of a specific process:<\/p>\n<ol>\n<li>Open the Windows PowerShell console with elevated rights.<\/li>\n<li>Create a query that uses WQL syntax.<\/li>\n<li>Register to receive events by using the <strong>Register-CimIndicationEvent<\/strong> cmdlet and supplying the query.<\/li>\n<li>Use <strong>Get-Event<\/strong> to receive the events.<\/li>\n<\/ol>\n<p>In addition, I will use the <strong>Get-EventSubscriber<\/strong> cmdlet to verify that the event was created properly, and I will use <strong>Remove-Event<\/strong> and <strong>Unregister-Event<\/strong> to perform cleanup.<\/p>\n<h2>Create the query<\/h2>\n<p>I first create the query. It looks pretty much like a SQL query. I select everything from the win32_ProcessStartTrace WMI class, and I limit the results to processes that are named Notepad.exe.<\/p>\n<p style=\"margin-left:30px\">\n  <b>Note\u00a0<\/b> When I use <b>Get-Process<\/b> it tells me the <b>ProcessName<\/b> property is equal to notepad. But WMI expects the <b>ProcessName<\/b> property to be equal to notepad.exe. Keep in mind, no error generates if I use notepad, but I will not receive any events either.\n<\/p>\n<p>I store my query string in a variable that I call <strong>$nq<\/strong> (for notepad query). Here is the query:<\/p>\n<p style=\"margin-left:30px\">\n  $nq = &#8220;Select * from win32_ProcessStartTrace where processname = &#8216;notepad.exe'&#8221;\n<\/p>\n<h2>Register to receive events<\/h2>\n<p>Now I use the <strong>Register-CimIndicationEvent<\/strong> cmdlet to register to receive the events I defined in my query. I also specify a <strong>SourceIdentifier<\/strong> that I call <strong>nq<\/strong>. This makes it easy to receive only events generated by my specific notepad query. Here is the command:<\/p>\n<p style=\"margin-left:30px\">\n  Register-CimIndicationEvent -Query $nq -SourceIdentifier nq\n<\/p>\n<h2>Quick check<\/h2>\n<p>Now I do a quick check. Did my registration work? I use <strong>Get-EventSubscriber<\/strong>. Are there any events? (There should not be at this point.) I use <strong>Get-Event<\/strong>.<\/p>\n<p>Here is what my Windows PowerShell console looks like at this point:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-18-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-18-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Generate and receive events<\/h2>\n<p>Now I generate an event by launching Notepad. When I do that, I use the <strong>Get-Event<\/strong> cmdlet to receive the event. Here is the command:<\/p>\n<p style=\"margin-left:30px\">\n  Get-Event -SourceIdentifier nq\n<\/p>\n<p>The returned object contains a number of properties. These properties are shown in the image that follows:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-18-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-18-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I know, from yesterday\u2019s post, that the information I am interested in obtaining is contained in the <strong>SourceEventArgs<\/strong> property and in the <strong>NewEvent<\/strong> property under that. I use dotted notation to gain access to the important properties:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-18-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-18-14-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>That was fun, do it again<\/h2>\n<p>That worked well. I can see that with the <strong>ProcessID<\/strong> I received, I could manage the newly created process if I needed to do so. But what about monitoring for an additional process? One way is to simply create another event registration. This time I will do it for Calc.exe. Here is the command:<\/p>\n<p style=\"margin-left:30px\">\n  $cq = &#8220;Select * from win32_ProcessStartTrace where processname = &#8216;calc.exe'&#8221;\n<\/p>\n<p style=\"margin-left:30px\">\n  Register-CimIndicationEvent -Query $cq -SourceIdentifier cq\n<\/p>\n<p>Now I launch Calculator (calc.exe) and retrieve the event. Here is the command that does that:<\/p>\n<p style=\"margin-left:30px\">\n  calc\n<\/p>\n<p style=\"margin-left:30px\">\n  (Get-Event -SourceIdentifier cq).SourceEventArgs.newevent\n<\/p>\n<p>This command and the output from the command are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-18-14-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-18-14-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>It still works with Notepad also. I simply use the <strong>nq<\/strong> source. I decide to clean up everything. Here are the commands I use:<\/p>\n<p style=\"margin-left:30px\">\n  get-event | Remove-Event\n<\/p>\n<p style=\"margin-left:30px\">\n  Get-EventSubscriber | Unregister-Event\n<\/p>\n<p>Now there are no more events and no more event subscriptions.<\/p>\n<h2>A combined query<\/h2>\n<p>I can, of course, combine my two event queries into a single query. To do this, I use a compound <strong>where<\/strong> clause. Here is my new query (this is a single line query that wraps due to the length. It includes no returns or line continuation marks):<\/p>\n<p style=\"margin-left:30px\">\n  $q = &#8220;Select * from win32_ProcessStartTrace where processname = &#8216;calc.exe&#8217; OR processname = &#8216;notepad.exe'&#8221;\n<\/p>\n<p>I register for events from this query just like I did for the other queries. Here is the command:<\/p>\n<p style=\"margin-left:30px\">\n  Register-CimIndicationEvent -Query $q -SourceIdentifier q\n<\/p>\n<p>Now I launch both Notepad and Calculator, and I use <strong>Get-Event<\/strong> to retrieve my new events. Here is the command that does that:<\/p>\n<p style=\"margin-left:30px\">\n  notepad\n<\/p>\n<p style=\"margin-left:30px\">\n  calc\n<\/p>\n<p style=\"margin-left:30px\">\n  (Get-Event -SourceIdentifier q).SourceEventArgs.newevent\n<\/p>\n<p>The commands and their associated output are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-18-14-05.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-18-14-05.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to using the <strong>Register-CimIndicationEvent<\/strong> cmdlet to monitor for a specific process. Join me tomorrow when I will talk about terminating a specific process when it starts up.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\" rel=\"noopener\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\" rel=\"noopener\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\" rel=\"noopener\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\" rel=\"noopener\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size:12px\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to monitor for the creation of specific processes. Microsoft Scripting Guy, Ed Wilson, is here. This morning it is beginning to look like autumn. Who knows, it may become really hot and humid over the weekend, but today I can delude myself into hearing [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[385,42,41,31,3,4,45,6],"class_list":["post-663","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-cim","tag-events-and-monitoring","tag-monitoring","tag-operating-system","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to monitor for the creation of specific processes. Microsoft Scripting Guy, Ed Wilson, is here. This morning it is beginning to look like autumn. Who knows, it may become really hot and humid over the weekend, but today I can delude myself into hearing [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=663"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/663\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}