Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Weekend Scripter: Creating a Sample Log File by Using PowerShell

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, shows how to create sample textual log output by using Windows PowerShell.

Microsoft Scripting Guy, Ed Wilson, is here. Well, it is the weekend. It seems like it has been a rather long week, but I guess in reality it was no longer than any other week. I did get a cool book that finally showed up by the package delivery van. I started on it last night; it is a great book about the philosophy of mathematics—neat stuff, actually. Oh, the other cool thing that happened this week is the Scripting Brother’s new baseball book is now available for pre-order. I read the advanced reading copy that my brother sent me, and it is actually pretty good (but hey, I am not a huge baseball fan).

Creating a simple Windows PowerShell script to write a log file

Tomorrow, I plan to write an article about a cool Windows PowerShell feature I found the other day. Therefore, I need script that will create a log file and append to the log file over a certain period of time. To do this, I decided to use three Windows PowerShell cmdlets. The first is Get-Date, the next one is Add-Content, and the last one is Start-Sleep.

First, I define a couple of variables. The first variable is the path to the log file, and the second one is how many lines I want to write to the log file. These two variables are shown here:

$path = "C:\fso\mylogfile.log"

$numberLines = 25

Now, I use the forlanguage statement to add the lines to the log file. I begin counting at zero and add lines until I have reached the number specified by $numberLines. This is shown here.

For ($i=0;$i -le $numberLines;$i++)

{

To create the line of text to add to the log file, I use a variable to hold the results of a little parameter substitution. The first parameter is the line number and the second one is the date. When getting the date, I use a pretty cool trick: I convert the datetime object to a string and choose only the hour, minute, and second. This is shown here.

$SampleString = "Added sample {0} at {1}" -f $i,(Get-Date).ToString("h:m:s")

Now the Add-Content cmdlet writes to the log file. If the log file does not exist, it creates the log file, and then adds to it. This line is shown here.

add-content -Path $path -Value $SampleString -Force

Once I write to the log file, I pause the script for 100 milliseconds. To do this, I use the Start-Sleep cmdlet. This code is shown here.

Start-Sleep -Milliseconds 100

The complete script is shown here.

$path = "C:\fso\mylogfile.log"

$numberLines = 25

For ($i=0;$i -le $numberLines;$i++)

{

 $SampleString = "Added sample {0} at {1}" -f $i,(Get-Date).ToString("h:m:s")

 add-content -Path $path -Value $SampleString -Force

 Start-Sleep -Milliseconds 100

 }

The created log file is shown here.

Image of log file

Fixing the code

Everything is pretty groovy, except for one small thing: I do not want the line numbers starting over at 25. This makes things a bit confusing, if I run the script over multiple days. So, I need a way to persist the value of $i. I could write it to the registry, but that is overkill. Since I am already writing to a text file, I decide to create a file that will simply store a single number. I call the file MyLogFileInit.txt.

Now I need to test to see if the file exists. If it does, I read it to get my starting number for line numbers, but if the file does not exist, I begin at zero. Seems like a great place to use Test-Path. Here is what I came up with.

if(Test-path $init)

 {$initLine = Get-Content $init}

Else {$initline = 0}  

The last major revision I do is persist the value of $i to the file. I use Out-File for this. I always want to overwrite what is there, and I only want the single number stored there. Here is what I use.

$i | Out-File -FilePath $init -Force

The revised script is shown here.

$path = "C:\fso\mylogfile.log"

$init = "C:\fso\mylogfileInit.txt"

[int]$numberLines = 25

if(Test-path $init)

 {$initLine = Get-Content $init}

Else {$initline = 0} 

For ([int]$i=$initline;$i -le ($numberLines + $initLine);$i++)

{

 $SampleString = "Added sample {0} at {1}" -f $i,(Get-Date).ToString("h:m:s")

 add-content -Path $path -Value $SampleString -Force

 Start-Sleep -Milliseconds 100

 }

$i | Out-File -FilePath $init -Force

Here is the revised log file. You will notice that I now continue adding to my line numbers and avoid the recycled numbers.

Image of log file

Well, that is about all that I want to do today. I wish to get back to my book. Hope this gives you ideas for playing around with Windows PowerShell. I invite you back tomorrow when I will use this script to show you something really cool.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>