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

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.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the mapped drive. If the