Skip to main content

How to Use Date/Time Information From Custom Logs in PowerShell. Part 2 of 2.

Last Tuesday in part I of this series, we looked at how to use the built in Date/Time methos to find how long ago an event was written in a Windows event log. But what about date/time information that we cannot receive in the correct format because it came from a third party product? No problem. We will work with what data is provided.

The Get-Time cmdlet returns an object of System.DateTime fortunatly, using the New-Object cmdlet, we can create a new System.DateTime object with information from our logs. Your first task will be to parse the data so you can extract as much date time information as possible.

Once you have done that, you need to create a DateTime object.

$MyDate = New-Object System.DateTime.

Now take a look at the contents of this object.

$MyDate


To view the information that we need to plug into this object, type $MyDate | FL.


By changing just one property of this object, we will get it to reflect our date. Type $MyDate | GM -MemberType ScriptProperty


The DateTime property accepts arguments in the form of Year, Month, Day, Hour, Minute, Second, The hour must be in 24 hour format. The month is an integer value. To set our date:

$MyDate = New-Object System.DateTime 2009, 3, 24, 15, 24, 00

Now Type $MyDate


Ok, we now have our object with the correct date/time form our log. We can now find the difference between these two dates.

(The $Today is left over from last week. To generate it, first type $Today = Get-Date)

$Today.Subtract($MyDate)


To Get the individual properties, assign this to a variable.

$DateDiff = $Today.Subtract($MyDate)

$DateDiff.Days

The only part that I cannot help you with is the extraction of the date/time info from your event logs. You need to come up with the code for that. I will suggest reading each line of code into a variable and using the Split method to extract what you need.

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.