GWMI Help - Newbie

by MarkJF at 2012-11-12 11:30:03

Hi guys,

Hoping you can help, I need to retrive a date value from the BIOS on a workstation, then use that value in another command. The machine is a Dell workstation and the OMCI software is installed on it.
I think I have sorted the individual powershell commands to retreive the date value and then use it, but I’m having trouble “putting them all together” in one script.
The date I’m after is called “FirstPowerOnDate” and is in the DCIM_Chassis class. If I run the following powershell command:
gwmi dcim_chassis -namespace root\dcim\sysman FirstPowerOnDate
It returns the following sample output:
__GENUS : 2
__CLASS : DCIM_Chassis
__PATH :
FirstPowerOnDate : 20110830000000.000000-000

I’d like to be able to grab just the digits of the date (so “20110830”) and then use this value in a dos command.
I’ve read about the select line statement which could be used to grab just the line that starts with FirstPowerOnDate, and also the substring command which could strip out everything but 20110830, but I’m not sure how to put those commands together in one script. Any help or advice you can give?
Thanks very much for your time.

by kittH at 2012-11-12 12:07:39
I don’t have that class, so can’t test, but have you tried:

(gwmi dcim_chassis -namespace root\dcim\sysman FirstPowerOnDate | Select -ExpandProperty FirstPowerOnDate).Substring(0,8)
by MarkJF at 2012-11-12 12:25:00
Hi Kitt, that is brilliant thanks very much!
I’ve added $date = to the beginning of it, which I think makes the output a variable, which i can then pass to my dos command line. I’ll test it out some more.
Thanks for your help :slight_smile:
by RichardSiddaway at 2012-11-30 02:37:05
One of the drawbacks to WMI is the way it handles dates - it uses its own back to front format as you have seen.

With PowerShell v2 the easiest wat to get to a date you can use is to use the ConvertToDateTime method which PowerShell adds to the object. As an example
PS> gwmi Win32_OperatingSystem | select LastBootUpTime


PS> $boot = gwmi Win32_OperatingSystem
PS> $boot.ConvertToDateTime($boot.LastBootUpTime)

30 November 2012 09:16:22

With PowerShell v3 its even easier - use the CIM cmdlets

PS> Get-CimInstance Win32_OperatingSystem | select -ExpandProperty LastBootUpTime

30 November 2012 09:16:22