WinBatch Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: stanl on February 02, 2019, 01:11:39 pm

Title: WMI/CLR question
Post by: stanl on February 02, 2019, 01:11:39 pm
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

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=StrSub(d,7,2):"/":StrSub(d,5,2):"/":Strsub(d,1,4)  ;without using CLR
   message("Last Install Date",oCvt.ToDateTime(d))



Title: Re: WMI/CLR question
Post by: td on February 03, 2019, 01:52:09 pm
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...
Title: Re: WMI/CLR question
Post by: stanl on February 04, 2019, 03:01:52 am
Thanks. In hindsight I should have asked the question differently.  This:
Code: Winbatch
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]
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.
Title: Re: WMI/CLR question
Post by: td on February 04, 2019, 06:52:34 am
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.