Converting to/from WMI Date Time
August 1, 2012 Leave a comment
You will find many posts for how to convert a wmi date-time string to a human-friendly date time here’s one example from Don Jones.
First, here’s an example of the wmi date-time string:
Get-WmiObject -class Win32_OperatingSystem | select __Server, LastBootUpTime
The value for LastBootUpTime will look something like this: 20120718141700.473048-300. This output is very handy for alpha-sort, by the way…
And Here’s Don’s code:
Get-WmiObject -class Win32_OperatingSystem | Select-Object __SERVER,@{label='LastRestart';expression={$_.ConvertToDateTime($_.LastBootUpTime)}}
Notice how Don elegantly converts the string on the fly with the label and expression arguments.
Now here’s an example of using a COM object to convert the current date time to a wmi date-time string.
$wmidate = new-object -com Wbemscripting.swbemdatetime $date = get-date -format g $wmidate.SetVarDate($date,$true) $wmidate.value
Not nearly as elegant, but it gets the job done. When you create objects in ConfigMgr that require a date-time field, you will usually enter a wmi date-time string.
Happy Scripting!
Greg