{"id":3382,"date":"2013-06-18T00:01:00","date_gmt":"2013-06-18T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/06\/18\/translate-a-vbscript-event-log-script-to-powershell-dude\/"},"modified":"2013-06-18T00:01:00","modified_gmt":"2013-06-18T00:01:00","slug":"translate-a-vbscript-event-log-script-to-powershell-dude","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/translate-a-vbscript-event-log-script-to-powershell-dude\/","title":{"rendered":"Translate a VBScript Event Log Script to PowerShell, Dude&#8230;"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, talks about translating a VBScript script that queries the Windows Event logs directly into Windows PowerShell.<\/span><span style=\"font-size: 12px\">&nbsp;<\/span><span style=\"font-size: 12px\">&nbsp;<\/span>\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;Hey, Scripting Guy! Dude! I have this way cool VBScript script that I use every day to query the event logs on our servers. It receives all of the events and displays it to the screen. I look at the events as they scroll past; and in this way, I get a good overview of what&rsquo;s happening on the servers. I am thinking of translating it to Windows PowerShell because I hear that Windows PowerShell is the way of the future. Is this cool or what?<span style=\"font-size: 12px\">&nbsp;<\/span>\n&mdash;SW\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello SW,\nMicrosoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I are busy getting ready for the <a href=\"http:\/\/www.atltechstravaganza.com\/\" target=\"_blank\">Atlanta TechStravaganza<\/a>. We leave Thursday afternoon, and we get back late Friday night. Then leave on Saturday for <a href=\"http:\/\/europe.msteched.com\/#fbid=Y6s0qEgGIo3\" target=\"_blank\">TechEd Europe 2013 in Madrid<\/a>. So things are really hopping around here.&nbsp;\nSW, if you like your Event Log VBScript script, then by all means keep using it. But whatever you do, please do not translate it directly into Windows PowerShell. Here is why&hellip;<span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n<h2>Retrieving event log events: the VBScript way<\/h2>\n<p>Back in the VBScript days, it was not uncommon to use WMI to connect to a local computer or a remote server to retrieve event log events. The way I generally did it was to use WMI. Here is an example VBScript script of performing this very task (this script appears in the Script Center Repository).\n<a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/c9652361-bb78-4689-8d05-1424c256d4db\" target=\"_blank\">List events from a Specific Event Log<\/a><\/p>\n<p style=\"padding-left: 30px\"><strong>VBScript<\/strong><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">strComputer = &#8220;.&#8221;<\/p>\n<p style=\"padding-left: 30px\">Set objWMIService = GetObject(&#8220;winmgmts:&#8221; _<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; &amp; &#8220;{impersonationLevel=impersonate}!\\&#8221; &amp; strComputer &amp; &#8220;rootcimv2&#8221;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Set colLoggedEvents = objWMIService.ExecQuery _<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: 12px\">&nbsp; &nbsp; (&#8220;Select * from Win32_NTLogEvent Where Logfile = &#8216;Application'&#8221;)<\/span><\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">For Each objEvent in colLoggedEvents<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Category: &#8221; &amp; objEvent.Category<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Computer Name: &#8221; &amp; objEvent.ComputerName<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Event Code: &#8221; &amp; objEvent.EventCode<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Message: &#8221; &amp; objEvent.Message<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Record Number: &#8221; &amp; objEvent.RecordNumber<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Source Name: &#8221; &amp; objEvent.SourceName<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Time Written: &#8221; &amp; objEvent.TimeWritten<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;Event Type: &#8221; &amp; objEvent.Type<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Wscript.Echo &#8220;User: &#8221; &amp; objEvent.User<span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\">Next\nThe above script is 19 lines of code&mdash;not too bad really. As you can see, it contains a great deal of repetition, which means much of it is simple cut, paste, and edit. Certainly it is easier to use this script than to open the Event Viewer and connect to a bunch of servers.<\/p>\n<h2>Translating the Event Log events script to PowerShell<\/h2>\n<p>Certainly it is possible to translate the event log script to Windows PowerShell. This is because Windows PowerShell does WMI very well. However, a direct translation does not take advantage of the way that Windows PowerShell works. Here is a translated script of the above VBScript script (you can find the script in the Script Center Repository; it makes a great bad example).\n<a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/de60f0ab-bcc5-47e2-b97d-9ec278be1a6d\" target=\"_blank\">List events from Event Logs<\/a><\/p>\n<p style=\"padding-left: 30px\"><strong>Windows PowerShell<\/strong><\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$strComputer = &#8220;.&#8221;<\/p>\n<p style=\"padding-left: 30px\">$colItems = get-wmiobject -class &#8220;Win32_NTLogEvent&#8221; -namespace &#8220;rootCIMV2&#8221; `<span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\">-computername $strComputer -Filter &#8216;logfile = &#8220;application&#8221;&#8216;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">foreach ($objItem in $colItems) {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<span style=\"font-size: 12px\">&nbsp; &nbsp; &nbsp;write-host &#8220;Category: &#8221; $objItem.Category<\/span><\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Category String: &#8221; $objItem.CategoryString<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Compute rName: &#8221; $objItem.ComputerName<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Data: &#8221; $objItem.Data<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Event Code: &#8221; $objItem.EventCode<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Event Identifier: &#8221; $objItem.EventIdentifier<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Event Type: &#8221; $objItem.EventType<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Insertion Strings: &#8221; $objItem.InsertionStrings<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Logfile: &#8221; $objItem.Logfile<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Message: &#8221; $objItem.Message<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Record Number: &#8221; $objItem.RecordNumber<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Source Name: &#8221; $objItem.SourceName<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Time Generated: &#8221; $objItem.TimeGenerated<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Time Written: &#8221; $objItem.TimeWritten<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;Type: &#8221; $objItem.Type<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host &#8220;User: &#8221; $objItem.User<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write-host<\/p>\n<p style=\"padding-left: 30px\">}\nAs you can see, the translated script follows the syntax of the VBScript original with little variation. So the script works, and &ldquo;now you are doing Windows PowerShell,&rdquo; but there is no Windows PowerShell advantage. If you understand your previous VBScript, you can certainly understand the Windows PowerShell version of that script&mdash;but it is still a decent amount of copy, paste, and edit to create the script.<\/p>\n<h2>A better way to do this sort of thing&hellip;<span style=\"font-size: 12px\">&nbsp;<\/span><\/h2>\n<p>A better way to do this is to at least take advantage of the way that Windows PowerShell works. The following code accomplishes this.<span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject win32_ntlogevent -Filter &#8216;logfile = &#8220;application&#8221;&#8216;\nYep, you have got that right. It is a single line of code&mdash;65 characters instead of 25 lines of code. Now we are beginning to take advantage of Windows PowerShell.<\/p>\n<h2>But it gets better<\/h2>\n<p>OK, so if you are familiar with the previous VBScript script, and the bad version of the Windows PowerShell script, using the <strong>Get-WmiObject<\/strong> approach is probably understandable. But in reality, it is still too much typing, and it does not really make sense. The common question that I used to get all the time with WMI was, &ldquo;So how did I know that class was there?&rdquo; Well, Windows PowerShell has great discoverability, but to be honest I never use WMI to query the event logs. The reason? There is a cmdlet that does this for me. The command is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-EventLog -LogName application\nThe nice thing about this cmdlet, is that it makes sense. The code is very readable. Anyone looking at it will know immediately what it is doing. If I need to connect remotely, I just add the <strong>&ndash;ComputerName<\/strong> parameter as shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-EventLog -LogName application -ComputerName dc1\nIf I need to connect to multiple computers, I can do this:<\/p>\n<p style=\"padding-left: 30px\">Get-EventLog -LogName application -ComputerName dc1, dc2, dc3\nAgain, the syntax is really clean and easy to read. This is the Windows PowerShell advantage.\nSW, that is all there is to using Windows PowerShell to query a specific event log. Join me tomorrow when I will talk some more about translating VBScript to PowerShell.\nI 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=\"http:\/\/blogs.technet.commailto: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.\n<strong>Ed Wilson, Microsoft Scripting Guy<\/strong>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about translating a VBScript script that queries the Windows Event logs directly into Windows PowerShell.&nbsp;&nbsp; &nbsp;Hey, Scripting Guy! Dude! I have this way cool VBScript script that I use every day to query the event logs on our servers. It receives all of the events and displays it [&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":[97,98,3,4,155,45],"class_list":["post-3382","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-event-logs","tag-logs-and-monitoring","tag-scripting-guy","tag-scripting-techniques","tag-vbscript-migration","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about translating a VBScript script that queries the Windows Event logs directly into Windows PowerShell.&nbsp;&nbsp; &nbsp;Hey, Scripting Guy! Dude! I have this way cool VBScript script that I use every day to query the event logs on our servers. It receives all of the events and displays it [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3382","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=3382"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3382\/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=3382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}