{"id":4228,"date":"2013-02-01T00:01:00","date_gmt":"2013-02-01T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/02\/01\/use-powershell-to-create-and-to-use-a-new-event-log\/"},"modified":"2013-02-01T00:01:00","modified_gmt":"2013-02-01T00:01:00","slug":"use-powershell-to-create-and-to-use-a-new-event-log","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-and-to-use-a-new-event-log\/","title":{"rendered":"Use PowerShell to Create and to Use a New Event Log"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary:<\/strong><span style=\"font-size: 12px\"> Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create and to use a new event log.<\/span>\n<span style=\"font-size: 12px\">Microsoft Scripting Guy, Ed Wilson, is here. Well, it&rsquo;s the weekend, baby! At least for the Scripting Wife and me. You see, the Scripting Manager gave me today and Monday off as sort of &ldquo;comp time&rdquo; because of all the long hours I have been working recently. I said cool, and Teresa said, &ldquo;I know, let&rsquo;s go to the beach,&rdquo; so we are back at the beach.&nbsp;<\/span><span style=\"font-size: 12px\">And I am back at the coffee house, sitting near the sand and playing around with Windows PowerShell. <\/span>\n<span style=\"font-size: 12px\">Now, if this sounds familiar, it is. <\/span><a style=\"font-size: 12px\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/01\/27\/weekend-scripter-use-powershell-to-meter-resources-on-hyper-v-windows-server-2012.aspx\" target=\"_blank\">Teresa and I headed to the beach last week<\/a><span style=\"font-size: 12px\"> and like they say in some cheesy movies, &ldquo;we&rsquo;re baaack &hellip;&rdquo; I will admit it is really hard to see if I am taking time off, or if I am &ldquo;working,&rdquo; even for me, because the two activities overlap so much. If truth be told, I am sitting here right now working&mdash;but hey, it&rsquo;s all fun&mdash;and I would be doing this anyway, even if I was not working, which I am not, because my boss told me to take some time off.&nbsp;<\/span><span style=\"font-size: 12px\">Now, I did read about a company recently that would pay for you to take vacation&mdash;cool, I thought, but you had to leave your cell phone, laptop, surface, etc. behind. That is not a vacation&mdash;that sounds more like torture (at least to me).<\/span><\/p>\n<h2>Use PowerShell to create my own event log<\/h2>\n<p>One of the cool things to do with Windows PowerShell is to create my own event logs. Here, I am talking about an event log that is like one of the traditional event logs (traditional event logs are System, Security, and Application). By using Windows PowerShell, these traditional types of event logs are easy to read, easy to write to, easy to back up, and easy to clear.<\/p>\n<h2>Use PowerShell to create an event log<\/h2>\n<p>I use the <strong>New-EventLog<\/strong> cmdlet to create a new event log. To use this cmdlet, I need three things:<\/p>\n<ol>\n<li>Open the Windows PowerShell console with admin rights&mdash;an error occurs when attempting to create a new event log without elevated rights.<\/li>\n<li>I need the name for the log.<\/li>\n<li>I need to specify a source for the events that write to the log.<\/li>\n<\/ol>\n<p>The following command creates a new traditional event log named <em>ScriptingGuys<\/em> with a source named <em>scripts<\/em>.&nbsp;<\/p>\n<p style=\"padding-left: 30px\">New-EventLog -LogName ScriptingGuys -Source scripts\nWhen the command runs, no output appears to the Windows PowerShell console. To ensure the command actually created a new event log, I use the <strong>Get-EventLog<\/strong> cmdlet with the <strong>&ndash;List<\/strong> parameter. Here is the command and the associated output.<\/p>\n<p style=\"padding-left: 30px\">10:06 C:&gt; Get-EventLog -List<\/p>\n<p style=\"padding-left: 30px\">&nbsp; Max(K) Retain OverflowAction&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Entries Log<\/p>\n<p style=\"padding-left: 30px\">&nbsp; &#8212;&#8212; &#8212;&#8212; &#8212;&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;- &#8212;<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 20,480&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 OverwriteAsNeeded&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 18,504 Application<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 20,480&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 OverwriteAsNeeded&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 HardwareEvents<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; 512&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7 OverwriteOlder&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 Internet Explorer<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 20,480&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 OverwriteAsNeeded&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 Key Management Service<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 10,240&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 OverwriteAsNeeded&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4 Lenovo-Customer Feedback<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; 512&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7 OverwriteOlder&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 Lenovo-Lenovo Patch Utility\/Admin<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; 128&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 OverwriteAsNeeded&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 30 OAlerts<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; 512&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7 OverwriteOlder&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 ScriptingGuys<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Security<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 20,480&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 OverwriteAsNeeded&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 21,437 System<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 15,360&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 OverwriteAsNeeded&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 10,059 Windows PowerShell<\/p>\n<h2>Configuring my event log<\/h2>\n<p>One of the things I notice when I check the ScriptingGuys event log is that it is set to 512 KB, and it will retain entries for 7 days when it will begin deleting older events. This is not the behavior I want. What I want is for the log to be 64 KB in size and to overwrite as needed. To do this, I would think that I use the <strong>Set-EventLog<\/strong> cmdlet&mdash;but no, there is not such a thing. The cmdlet is named <strong>Limit-EventLog<\/strong>. Looking at the Help, it appears there is only one parameter set. Well, I want to <em>OverWriteAsNeeded, <\/em>so I guess I also need to set a retention days of 0. I craft the following command, but as you can see, it fails.<\/p>\n<p style=\"padding-left: 30px\">10:19 C:&gt; Limit-EventLog -OverflowAction OverWriteAsNeeded -RetentionDays 0 -Maximum<\/p>\n<p style=\"padding-left: 30px\">Size 64KB<\/p>\n<p style=\"padding-left: 30px\">Limit-EventLog : Cannot validate argument on parameter &#8216;RetentionDays&#8217;. The 0 argument is less than the minimum allowed range of 1. Supply an argument that is greater than or equal to 1 and then try the command again.<\/p>\n<p style=\"padding-left: 30px\">At line:1 char:65<\/p>\n<p style=\"padding-left: 30px\">+ Limit-EventLog -OverflowAction OverWriteAsNeeded -RetentionDays 0 -MaximumSize<\/p>\n<p style=\"padding-left: 30px\">+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : InvalidData: (:) [Limit-EventLog], ParameterBindingValidationException<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Commands.LimitEventLogCommand\nMajor bummer. Ok, so I try it without the <strong>RetentionDays<\/strong> parameter &hellip; and it works.<\/p>\n<p style=\"padding-left: 30px\">Limit-EventLog -OverflowAction OverWriteAsNeeded -MaximumSize 64KB -LogName scriptingguys\nI now use <strong>Get-EventLog<\/strong> to confirm my changes took place &hellip; the output below confirms that the command worked properly.<\/p>\n<p style=\"padding-left: 30px\">10:23 C:&gt; Get-EventLog -list | ? log -eq scriptingguys<\/p>\n<p style=\"padding-left: 30px\">&nbsp; Max(K) Retain OverflowAction&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Entries Log<\/p>\n<p style=\"padding-left: 30px\">&nbsp; &#8212;&#8212; &#8212;&#8212; &#8212;&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;- &#8212;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 64&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 OverwriteAsNeeded&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 ScriptingGuys<\/p>\n<h2>I can&rsquo;t <em>get <\/em>an event log without entries<\/h2>\n<p>Interestingly enough, I cannot get an event log that has no entries in it &hellip; at least not yet. Because if I use the <strong>Get-EventLog<\/strong> cmdlet to attempt to retrieve the ScriptingGuys event log, an error appears. The command and error are here.<\/p>\n<p style=\"padding-left: 30px\">10:23 C:&gt; Get-EventLog -LogName scriptingguys<\/p>\n<p style=\"padding-left: 30px\">Get-EventLog : No matches found<\/p>\n<p style=\"padding-left: 30px\">At line:1 char:1<\/p>\n<p style=\"padding-left: 30px\">+ Get-EventLog -LogName scriptingguys<\/p>\n<p style=\"padding-left: 30px\">+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ObjectNotFound: (:) [Get-EventLog], ArgumentException<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : GetEventLogNoEntriesFound,Microsoft.PowerShell.Comman<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; ds.GetEventLogCommand<\/p>\n<h2>Writing to the event log<\/h2>\n<p>To write to my new event log, I need to specify the following information:<\/p>\n<ol>\n<li>The log name (scriptingguys in my example)<\/li>\n<li>The source (scripting in my case)<\/li>\n<li>EventID (I generally start with 1)<\/li>\n<li>EntryType (Information, Warning, Error)<\/li>\n<li>Message (this is what I want to log)<\/li>\n<\/ol>\n<p>In this example, I add a new entry to the ScriptingGuys event log.<\/p>\n<p style=\"padding-left: 30px\">Write-EventLog -LogName ScriptingGuys -Source scripts -Message &#8220;Dude, it works &#8230; COOL!&#8221; -EventId 0 -EntryType information\nI can now use the <strong>Get-EventLog<\/strong> cmdlet to retrieve the event. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">10:27 C:&gt; Get-EventLog -LogName scriptingguys<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Index Time&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EntryType&nbsp;&nbsp; Source&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InstanceID Message<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; &#8212;&#8211; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&nbsp;&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&#8212;&#8212;&#8212;- &#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 Jan 29 10:27&nbsp; Information scripts&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 Dude, it wor&#8230;\nThat is all there is to using Windows PowerShell to create and to manage event logs. Join me tomorrow when I will talk about more cool stuff.\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><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create and to use a new event log. Microsoft Scripting Guy, Ed Wilson, is here. Well, it&rsquo;s the weekend, baby! At least for the Scripting Wife and me. You see, the Scripting Manager gave me today and Monday off as sort of &ldquo;comp [&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,45],"class_list":["post-4228","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-event-logs","tag-logs-and-monitoring","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create and to use a new event log. Microsoft Scripting Guy, Ed Wilson, is here. Well, it&rsquo;s the weekend, baby! At least for the Scripting Wife and me. You see, the Scripting Manager gave me today and Monday off as sort of &ldquo;comp [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4228","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=4228"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4228\/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=4228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}