Viewing entries tagged

PowerShell Quick Tip: Simple logging with time stamps

PowerShell Quick Tip: Simple logging with time stamps

Today I was writing a script at work to check sysvol replication. We have a DC that will sometimes not share the love outbound, yet inbound works just fine. That means to test it I needed to create a file on that DC, and ensure it replicates outward. Normally, I would check event log or use WMI queries, however everything looks good even when it isn't working.

After writing the script, I wanted to add some very simple logging so I could save the results a file (which is then emailed out). 


I kept this simple, as I needed something that I could write while not taking up too much time to do it. In the script, I created a function to format a time stamp.

Timestamp function

function Get-TimeStamp {
    return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)

Told you it was simple! You can use it with Write-Output and use Out-File to add content into a log file.

Write-Output "$(Get-TimeStamp) I can connect to $dc" | Out-file C:\dclog.txt -append

Here are the results in the text file from the script after it runs:

[2/15/2016 9:27 AM] File test927AM created dc1
[2/15/2016 9:27 AM] File test927AM exists on dc1
[2/15/2016 9:30 AM] File test927AM removed from dc1
[2/15/2016 9:32 AM] I can connect to dc1

I added some Start-Sleep statements in there as you can see with the time stamp differences. Once I tidy up the sysvol replication checker script I'll share that here.

I hope you found this helpful, let me know if you have a better way or any questions/comments!

-Ginger Ninja