{"id":651,"date":"2014-09-20T00:01:00","date_gmt":"2014-09-20T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/09\/20\/weekend-scripter-playing-with-powershell-processes-and-events\/"},"modified":"2014-09-20T00:01:00","modified_gmt":"2014-09-20T00:01:00","slug":"weekend-scripter-playing-with-powershell-processes-and-events","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-playing-with-powershell-processes-and-events\/","title":{"rendered":"Weekend Scripter: Playing with PowerShell Processes and Events"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about playing with processes and events in Windows PowerShell.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning I woke up and I had an idea. This happens to me sometimes. Not often. But occasionally. And here it was. I could not get it out of my mind&#8230;<\/p>\n<p>Can I use an event from a specific process? I wonder. I should be able to do this, but hmmmm. I know a process that I retrieve by using the <b>Get-Process<\/b> cmdlet is really an instance of System.Diagnostics.Process. I also know that in addition to having methods and properties, it has events. Here are the events I get when I look at a specific process:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-Process notepad | Get-Member -MemberType Event<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; TypeName: System.Diagnostics.Process<\/p>\n<p style=\"margin-left:30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<\/p>\n<p style=\"margin-left:30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<\/p>\n<p style=\"margin-left:30px\">Disposed&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Event&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.EventHandler Disposed(System.Object, System.Event&#8230;<\/p>\n<p style=\"margin-left:30px\">ErrorDataReceived&nbsp; Event&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Diagnostics.DataReceivedEventHandler ErrorDataRec&#8230;<\/p>\n<p style=\"margin-left:30px\">Exited&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Event&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.EventHandler Exited(System.Object, System.EventArgs)<\/p>\n<p style=\"margin-left:30px\">OutputDataReceived Event&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Diagnostics.DataReceivedEventHandler OutputDataRe&#8230;<\/p>\n<p>Without getting all froggy and looking stuff up in MSDN, I figure that I have a pretty good idea of what an <b>Exited<\/b><i> <\/i>event might really be&mdash;it is an event that is raised when a particular process exits.<\/p>\n<h2>Let me try the Windows PowerShell console<\/h2>\n<p>I decided to launch Notepad, and then use the <b>Get-Process<\/b> cmdlet to retrieve that instance of Notepad. Now I use the <b>Register-ObjectEvent<\/b> cmdlet, supply the <b>Process<\/b> object as an <b>InputObject<\/b>, and I specify that I want to monitor for the <b>Exited<\/b> event. In the action portion, I start Notepad, and I unregister the event subscriber.<\/p>\n<p>I press ENTER, and I wait.<\/p>\n<p>I close Notepad&hellip;<\/p>\n<p>And I wait.<\/p>\n<p>And I wait.<\/p>\n<p>And I wait.<\/p>\n<p>Nothing happens. Bummer.<\/p>\n<p>I press ENTER in the Windows PowerShell console, and suddenly Notepad reappears. Hmmm&hellip;I wonder if this is an STA\/MTA kind of thing. I launch Windows PowerShell in MTA mode (<b>powershell &ndash;mta<\/b>) and in STA mode (<b>powershell &ndash;sta<\/b>). Still nothing. It only works after I press ENTER. Bummer. Not such a good deal at this point. By the way, here is a screenshot:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-20-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-20-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>One of the nice things about using the <b>Register-ObjectEvent<\/b> cmdlet is that it does not require me to launch an elevated Windows PowerShell console (or ISE). This is great because I seldom launch Windows PowerShell with elevated permission. Most of the time, I try things without elevation.<\/p>\n<h2>What about the ISE?<\/h2>\n<p>I open the Windows PowerShell ISE and type my script:<\/p>\n<p style=\"margin-left:30px\">Start-Process notepad<\/p>\n<p style=\"margin-left:30px\">$n = Get-Process notepad<\/p>\n<p style=\"margin-left:30px\">$job = Register-ObjectEvent -InputObject $n -EventName exited -SourceIdentifier notepad -Action {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Start-Process notepad<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Get-EventSubscriber | Unregister-Event }<\/p>\n<p>When I run the script, Notepad appears. Cool. I close Notepad, and it immediately reappears. Cool. If I close that instance of Notepad, however, Notepad remains closed. This is due to the fact that I am monitoring a specific instance of Notepad, and once I close that particular instance of Notepad, the new instance is a different process. Therefore, I cannot call the object event from that new process because I have not captured it in the <b>$n<\/b> variable.<\/p>\n<h2>Advantages?<\/h2>\n<p>Using <b>Register-ObjectEvent<\/b> seems to have the following advantages for me:<\/p>\n<ul>\n<li>Does not automatically require admin rights<\/li>\n<li>Syntax is easier<\/li>\n<li>Really fast<\/li>\n<li>Lower overhead<\/li>\n<\/ul>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about playing with processes and events in Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. This morning I woke up and I had an idea. This happens to me sometimes. Not often. But occasionally. And here it was. I could not get it out of my mind&#8230; [&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":[400,42,3,4,61,45],"class_list":["post-651","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-net","tag-events-and-monitoring","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about playing with processes and events in Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. This morning I woke up and I had an idea. This happens to me sometimes. Not often. But occasionally. And here it was. I could not get it out of my mind&#8230; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/651","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=651"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/651\/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=651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}