Alan's Blog

"Yeah. I wrote a script that will do that."

Menu
  • About My Blog
Menu

CIM_DATETIME Conversion Functions

Posted on September 5, 2016 by Alan

Also known as WMI Time, or WBEM DateTime,  CIM_DATETime, https://msdn.microsoft.com/en-us/library/aa387237(v=vs.85).aspx, is that odd Windows DateTime format that shows values looking like “20160905103517.816236-240”  The COM Object that presents this is  WbemScripting.SWbemDateTime, and you frequently see code to convert to this format using it, or a tortuous series of string manipulations.  My rule of thumb is this:  If you can avoid using a COM object, you should.  Here are two  functions to handle these dates using .NET.

Function ConvertTo-CIMDateTime{
     [OutputType([string])]
        Param
        (
            # oDT a dateTime object
            [Parameter(Mandatory=$true,  Position=0)]
            $oDT,
            # NoUTC is switch to remove UTC data, replacing with 000
            [Parameter(Mandatory=$False,  Position=1)]
            [switch]$NoUTC
)
   $CIMDT = [management.managementdatetimeconverter]::ToDmtfDateTime($oDT)
   if ($NoUTC){$CIMDT.SubString(0,22)+'000' }ELSE { $CIMDT }
}

The second function converts from the CIM DateTime string back to an ordinary date object:

Function ConvertFrom-CIMDateTime{
 [OutputType([datetime])]
    Param
    (
        # strDMTF is string in CIM_DateTime format
        [Parameter( Mandatory=$true, Position=0)]
        $strDMTF
        )
   [management.managementdatetimeconverter]::todatetime($strDMTF)
}

An example of the output:

#Example
$now = Get-date
$now

$CIMDate= ConvertTo-CIMDateTime $now 
$CIMDate
ConvertFrom-CIMDateTime $CIMDate

Monday, September 5, 2016 10:35:17 AM
20160905103517.816236-240
Monday, September 5, 2016 10:35:17 AM

Leave a Reply

You must be logged in to post a comment.

Search

Please Note

All the scripts are saved as .txt files. Newer files have a “View Script” button which will let you save or open a script in notepad. For earlier posts, the easiest way to download with IE is to right click on the link and use “Save Target As”. Rename file from Name_ext.txt to Name.ext.

To see a full post after searching, please click on the title.

PowerShell Scripts were written with version 3 or later.

https connections are supported.

All new users accounts must be approved, as are comments. Please be patient.  If you find a post error or a script which doesn’t work as expected, I appreciate being notified.  My email is my first name at the domain name, and you are welcome to contact me that way.

Tags

1E ACLS Active Directory ActiveDirectory ADSI Advanced Functions Audit Change Administrator Password COMObject Computer Groups DateTime Desktop DNS Excel FileScriptingObject Forms General GPO GPS Group Policy Hacks ISE Lockout logons NAV740 Nessus OU OU permissions Outlook Pick Folder Power PowerShell Powershell Scriptlets RDP SCCM schedule reboot Scripting Security Shell.Application user information VBA Windows Update WMI WordPress WPF

Categories

akaplan.com

  • Back to Home Page

Archives

Scripting Sites

  • A Big Pile of Small Things
  • Adam, the Automator
  • Art of the DBA
  • Ashley McGlone
  • Boe Prox
  • Carlo Mancini
  • DexterPOSH
  • Doug Finke
  • Jaap Brasser's Blog
  • JeffOps The Scripting Dutchman
  • Jonathan Medd's Blog
  • Keith Hill's Blog
  • LazyWinAdmin
  • Nana Lakshmanan
  • PowerShell Magazine
  • PowerShell Team Blog
  • PowerShell.org
  • PwrShell.net
  • Richard Siddaway's Blog
  • Ryan Yates' Blog
  • Skatterbrainz
  • The Lonely Administrator

SQL Site

  • Art of the DBA

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2025 Alan's Blog | Theme by SuperbThemes

Terms and Conditions - Privacy Policy