{"id":50643,"date":"2010-04-15T00:01:00","date_gmt":"2010-04-15T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/04\/15\/hey-scripting-guy-can-i-use-wmi-to-determine-when-someone-logs-off-a-user-or-shuts-down-a-server\/"},"modified":"2010-04-15T00:01:00","modified_gmt":"2010-04-15T00:01:00","slug":"hey-scripting-guy-can-i-use-wmi-to-determine-when-someone-logs-off-a-user-or-shuts-down-a-server","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-can-i-use-wmi-to-determine-when-someone-logs-off-a-user-or-shuts-down-a-server\/","title":{"rendered":"Hey, Scripting Guy! Can I Use WMI to Determine When Someone Logs Off a User or Shuts Down a Server?"},"content":{"rendered":"<p class=\"MsoNormal\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\">&nbsp;<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\">Hey, Scripting Guy! I have a Windows Server 2003 computer that I need to monitor for reboots. This may sound rather strange, but this server is more of a workstation, and we have a user mode application that runs under an actual user account. Therefore, we log on using the ID we have set up, launch the application, lock the console of the server, and that is the way the program is supposed to run. Once a day, a user goes to the server, unlocks the console, maximizes the application, and runs a report on the locally attached printer. The user is then supposed to minimize the application, lock the workstation, and collect the report. Unfortunately, if someone new runs the report about half the time, that person will shutdown the server, or log off the user account instead of locking the console. When this happens the application misses data, and it <a href=\"http:\/\/www.bing.com\/Dictionary\/search?q=define+hose&amp;FORM=DTPDIA&amp;qpvt=define+hose\"><font face=\"Segoe\">hoses<\/font><\/a> our accounting for the month. Is there a way to use WMI to let me know when someone either logs the user off, or shuts down the server? I know I can use ping to let me know if the server is shutdown, but this does nothing for logoffs. <\/p>\n<\/p>\n<p class=\"MsoNormal\">&#8212; SM<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello SM, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. It has been a lovely day in Charlotte, North Carolina, in the United States of America. The sky has been a deep clear blue that occurs after an onslaught of rain cleanses the sky. It reminds me of the sky I saw in Alaska while I was up there teaching a Windows PowerShell workshop. As you can see in the following photo, it is not the color that was the reminder, but maybe the absence of clouds in the sky. We do not have stark mountains or moose in Charlotte, but we do have trees and a few creek beds. So maybe there is not as strong a parallel as I was originally planning. Maybe I just miss Alaska. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Photo Ed took in Alaska\" alt=\"Photo Ed took in Alaska\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/april\/hey0415\/hsg-04-15-10-01.jpg\" width=\"600\" height=\"450\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">SM, you know your current situation is not ideal, and I hope your application is not running as a member of the Domain Admins, although I would not be surprised. The best solution would be to create a custom profile that does not include the logoff or shutdown buttons on the menu. Or better yet, you could remove the shutdown privilege from the user account that is logged onto the machine. However, you know your own business rules and environment much better than I do. <\/p>\n<p class=\"MsoNormal\">To monitor for shutdown or logoff events, you can use the WMI class <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394101(v=VS.85).aspx\"><font face=\"Segoe\">Win32_ComputerShutdownEvent<\/font><\/a>. The <b>Win32_ComputerShutdownEvent<\/b> WMI class first appeared in Windows XP. It is therefore available on your Windows Server 2003 machine. To use this event class, you do not need to write a generic event query. Use the <b>Register-WmiEvent<\/b> cmdlet, and specify the class name of <b>Win32_ComputerShutdownEvent<\/b>. Target your remote computer by name or by IP address. If you need to supply alternate credentials for the command to work, you can use the <b>&ndash;credentials<\/b> parameter and you will be prompted for the password. This command is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Register-WmiEvent -Class win32_computerShutdownEvent -ComputerName win2003<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After you have an event subscription, by using the <b>Get-Event<\/b> cmdlet you can retrieve any events that may have been generated. If you did not specify a source identifier when you used the <b>Register-WmiEvent<\/b> cmdlet, the <b>Get-Event<\/b> cmdlet will return all events that have been received. This command is seen here as it returns two events:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-Event<\/p>\n<p>ComputerName<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br \/>RunspaceId<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 7bbb485c-6303-4d1e-9084-bc46851990d5<br \/>EventIdentifier<span>&nbsp; <\/span>: 1<br \/>Sender<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: System.Management.ManagementEventWatcher<br \/>SourceEventArgs<span>&nbsp; <\/span>: System.Management.EventArrivedEventArgs<br \/>SourceArgs<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {System.Management.ManagementEventWatcher, System.Management.EventArrivedEventArgs}<br \/>SourceIdentifier : 5ff0a4c6-cb25-45c3-8efc-2c48b722ff34<br \/>TimeGenerated<span>&nbsp;&nbsp;&nbsp; <\/span>: 4\/12\/2010 2:13:06 PM<br \/>MessageData<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<\/p>\n<p>ComputerName<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br \/>RunspaceId<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 7bbb485c-6303-4d1e-9084-bc46851990d5<br \/>EventIdentifier<span>&nbsp; <\/span>: 2<br \/>Sender<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: System.Management.ManagementEventWatcher<br \/>SourceEventArgs<span>&nbsp; <\/span>: System.Management.EventArrivedEventArgs<br \/>SourceArgs<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: {System.Management.ManagementEventWatcher, System.Management.EventArrivedEventArgs}<br \/>SourceIdentifier : 5ff0a4c6-cb25-45c3-8efc-2c48b722ff34<br \/>TimeGenerated<span>&nbsp;&nbsp;&nbsp; <\/span>: 4\/12\/2010 2:13:29 PM<br \/>MessageData<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The data we are interested in comes from the <b>NewEvent<\/b> property. It returns the machine name and the type of event that was generated. A type of 1 is a shutdown or a reboot. A type of 0 means that the event was triggered by a logoff:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; $a[1].sourceEventArgs.NewEvent<\/p>\n<p>__GENUS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 2<br \/>__CLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Win32_ComputerShutdownEvent<br \/>__SUPERCLASS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Win32_ComputerSystemEvent<br \/>__DYNASTY<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: __SystemClass<br \/>__RELPATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br \/>__PROPERTY_COUNT<span>&nbsp;&nbsp;&nbsp; <\/span>: 4<br \/>__DERIVATION<span>&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;<\/span>: {Win32_ComputerSystemEvent, __ExtrinsicEvent, __Event, __IndicationRelated&#8230;}<br \/>__SERVER<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br \/>__NAMESPACE<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br \/>__PATH<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>:<br \/>MachineName<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: WIN2003<br \/>SECURITY_DESCRIPTOR :<br \/>TIME_CREATED<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: 129155696093028224<br \/>Type<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>: 1<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Because you already know the computer name, all you are interested in is the time the event was generated and the type of event. You can obtain this information by piping the results to the <b>Foreach-Object<\/b> cmdlet as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-Event | Foreach-Object { Write-Host $_.timeGenerated $_.sourceEventArgs.NewEvent.type }<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">4\/12\/2010 2:13:06 PM 0<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">4\/12\/2010 2:13:29 PM 1<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">These commands and the associated results are seen in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of commands and associated results\" alt=\"Image of commands and associated results\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/april\/hey0415\/hsg-04-15-10-02.jpg\" width=\"600\" height=\"359\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">SM, that is all there is to using WMI eventing. This also concludes our WMI Week articles. Join us tomorrow when we open our virtual mailbag and examine questions that only require a short answer&mdash;that is right, it is time once again for Quick-Hits Friday. We will talk about&hellip;wait a minute. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><font face=\"Segoe\">Official Scripting Guys Forum<\/font><\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I have a Windows Server 2003 computer that I need to monitor for reboots. This may sound rather strange, but this server is more of a workstation, and we have a user mode application that runs under an actual user account. Therefore, we log on using the ID we have [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[42,3,4,45,6],"class_list":["post-50643","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-events-and-monitoring","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I have a Windows Server 2003 computer that I need to monitor for reboots. This may sound rather strange, but this server is more of a workstation, and we have a user mode application that runs under an actual user account. Therefore, we log on using the ID we have [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50643","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=50643"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/50643\/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=50643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=50643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=50643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}