WMI/CLR question

Started by stanl, February 02, 2019, 01:11:39 PM

Previous topic - Next topic

stanl

The script below correctly outputs the last install date on Win10. I added a reference to the WB CLR as I assumed it would return the value as mm/dd/yyyy h:m:s AM/PM... which I know could be accomplished by wrapping the ToDateTime() function around WB's TimeFormat().... but just questioning the need, as my Pooh Brain couldn't convert without the CLR in one easy step.


Code (WINBATCH) Select


strComputer = "."
oWMI = GetObject( "winmgmts:\\" : strComputer : "\root\cimv2")
WQL = 'SELECT * FROM Win32_OperatingSystem'
oOP = oWMI.ExecQuery(WQL)
ObjectClrOption( "useany", "System.Management" )
oCvt = ObjectClrNew( "System.Management.ManagementDateTimeConverter")


ForEach i in oOP
   d=i.InstallDate
   ;d=StrSub(d,7,2):"/":StrSub(d,5,2):"/":Strsub(d,1,4)  ;without using CLR
   message("Last Install Date",oCvt.ToDateTime(d))
Next


oCvt=0
oOP=0
oWMI=0


Exit



td

The date returned by WMI is not a valid COM Automation date.  It is a BSTR that COM cannot convert to a variant of type VT_DATE so you can't use ObjectType to convert it from a BSTR to a DATE.  If you could use ObjectType, you could simply pass the result of the conversion from BSTR to VT_DATE to the TimeFormat function because the TimeFormat function expects input to be in the date/time in the YMDHMS WIL time format.

Since you can't convert to VT_DATE using ObjectType you would need to manually parse the date/time to get it into YMDHMS to call TimeFormat or convert it to a COM Automation form that will convert to a VT_DATE and then call TimeFormat.  However, if you decided to parse it yourself, you might as well finish the job and convert it directly to your desired format skipping the TmeFormat function.

Or you could just use the FCL time converter class...
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Thanks. In hindsight I should have asked the question differently.  This:
Code (WINBATCH) Select

message("Last Install Date",TimeFormat(oCvt.ToDateTime(d),"dddd, dd/MM/yyyy hh:mm:ss t"))



works perfectly.  It is equal to


PS> [size=78%](Get-CimInstance -Class Win32_OperatingSystem).InstallDate[/size]
[/size]
[/size]
which I thought should be equal to [/size]oCvt.ToDateTime(d) without the need for Timeformat().  But now I see what was confusing to me.

td

That's because the ToDateTime method returns a VT_DATE (internally FCL data types are converted to COM variant types.)  WinBatch converts VT_DATEs to YMDHMS format whenever a variable containing a VT_DATE is converted to a string as is the case when it is passed to the TimeFormat function.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade