Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - WMI Local Time format

Reply
 
Old 09-11-2007   #1 (permalink)
Shay Levi


 
 

WMI Local Time format


Can this be shortened? I'm trying to get the local date/time on some remote
servers.

$dt = gwmi win32_localtime
[string]::format("{0:00}/{1:00}/{2} {3:00}:{4:00}:{5:00}", $dt.month,$dt.day,$dt.year,$dt.month,$dt.hour,$dt.minute,$dt.second)

Shay
http://scriptolog.blogspot.com



My System SpecsSystem Spec
Old 09-11-2007   #2 (permalink)
Jeff


 
 

Re: WMI Local Time format

On Sep 11, 6:15 pm, Shay Levi <n...@xxxxxx> wrote:
Quote:

> Can this be shortened? I'm trying to get the local date/time on some remote
> servers.
>
> $dt = gwmi win32_localtime
> [string]::format("{0:00}/{1:00}/{2} {3:00}:{4:00}:{5:00}", $dt.month,$dt.day,$dt.year,$dt.month,$dt.hour,$dt.minute,$dt.second)
>
> Shayhttp://scriptolog.blogspot.com
This is slightly shorter, and it appears to give you the correct time:

"{0:00}/{1:00}/{2} {3:00}:{4:00}:{5:00}" -f $dt.month,$dt.day,$dt.year,
$dt.hour,$dt.minute,$dt.second

It looks to me that $dt.month slipped in there by mistake....

Jeff

My System SpecsSystem Spec
Old 09-11-2007   #3 (permalink)
Jeff


 
 

Re: WMI Local Time format

On Sep 11, 8:39 pm, Jeff <jeff.hill...@xxxxxx> wrote:
Quote:

> On Sep 11, 6:15 pm, Shay Levi <n...@xxxxxx> wrote:
>
Quote:

> > Can this be shortened? I'm trying to get the local date/time on some remote
> > servers.
>
Quote:

> > $dt = gwmi win32_localtime
> > [string]::format("{0:00}/{1:00}/{2} {3:00}:{4:00}:{5:00}", $dt.month,$dt.day,$dt.year,$dt.month,$dt.hour,$dt.minute,$dt.second)
>
Quote:

> > Shayhttp://scriptolog.blogspot.com
>
> This is slightly shorter, and it appears to give you the correct time:
>
> "{0:00}/{1:00}/{2} {3:00}:{4:00}:{5:00}" -f $dt.month,$dt.day,$dt.year,
> $dt.hour,$dt.minute,$dt.second
>
> It looks to me that $dt.month slipped in there by mistake....
>
> Jeff
If you are comfortable with the default date format, this is also
slightly shorter:

New-Object DateTime $dt.year,$dt.month,$dt.day,$dt.hour,$dt.minute,
$dt.second

Jeff


My System SpecsSystem Spec
Old 09-11-2007   #4 (permalink)
Shay Levi


 
 

Re: WMI Local Time format

Thanks Jeff.

Shay
http://scriptolog.blogspot.com


Quote:

> On Sep 11, 8:39 pm, Jeff <jeff.hill...@xxxxxx> wrote:
>
Quote:

>> On Sep 11, 6:15 pm, Shay Levi <n...@xxxxxx> wrote:
>>
Quote:

>>> Can this be shortened? I'm trying to get the local date/time on some
>>> remote servers.
>>>
>>> $dt = gwmi win32_localtime
>>> [string]::format("{0:00}/{1:00}/{2} {3:00}:{4:00}:{5:00}",
>>> $dt.month,$dt.day,$dt.year,$dt.month,$dt.hour,$dt.minute,$dt.second)
>>> Shayhttp://scriptolog.blogspot.com
>>>
>> This is slightly shorter, and it appears to give you the correct
>> time:
>>
>> "{0:00}/{1:00}/{2} {3:00}:{4:00}:{5:00}" -f
>> $dt.month,$dt.day,$dt.year, $dt.hour,$dt.minute,$dt.second
>>
>> It looks to me that $dt.month slipped in there by mistake....
>>
>> Jeff
>>
> If you are comfortable with the default date format, this is also
> slightly shorter:
>
> New-Object DateTime $dt.year,$dt.month,$dt.day,$dt.hour,$dt.minute,
> $dt.second
>
> Jeff
>

My System SpecsSystem Spec
Old 09-11-2007   #5 (permalink)
Jacques Barathon [MS]


 
 

Re: WMI Local Time format

"Shay Levi" <no@xxxxxx> wrote in message
news:8766a9446e518c9c29567c88318@xxxxxx
Quote:

>
> Can this be shortened? I'm trying to get the local date/time on some
> remote servers.
>
> $dt = gwmi win32_localtime
> [string]::format("{0:00}/{1:00}/{2} {3:00}:{4:00}:{5:00}",
> $dt.month,$dt.day,$dt.year,$dt.month,$dt.hour,$dt.minute,$dt.second)
Besides Jeff's tips, the $dt object has a method called ConvertToDateTime()
which sounds like it should provide what you are looking for. However I
can't make it work.

If someone from the PowerShell team reads this, they might tell us a bit
more about it?

Jacques

My System SpecsSystem Spec
Old 09-11-2007   #6 (permalink)
Shay Levi


 
 

Re: WMI Local Time format

Thanks Jacques. When piping $dt to get-member I saw it too.

Eventually, I got the time by querying the Win32_OperatingSystem class, It
has a LocalDateTime property, (Win32_LocalTime isnt available on W2K boxes).
which is what ConvertToDateTime() accepts as an argument.

$dt = (gwmi -query "select LocalDateTime from win32_operatingsystem").LocalDateTime
([wmi]'').ConvertToDateTime($dt).tostring("MM/dd/yyyy HH:mm:ss")


Shay
http://scriptolog.blogspot.com


Quote:

> "Shay Levi" <no@xxxxxx> wrote in message
> news:8766a9446e518c9c29567c88318@xxxxxx
>
Quote:

>> Can this be shortened? I'm trying to get the local date/time on some
>> remote servers.
>>
>> $dt = gwmi win32_localtime [string]::format("{0:00}/{1:00}/{2}
>> {3:00}:{4:00}:{5:00}",
>> $dt.month,$dt.day,$dt.year,$dt.month,$dt.hour,$dt.minute,$dt.second)
>>
> Besides Jeff's tips, the $dt object has a method called
> ConvertToDateTime() which sounds like it should provide what you are
> looking for. However I can't make it work.
>
> If someone from the PowerShell team reads this, they might tell us a
> bit more about it?
>
> Jacques
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Converting date time format to string format PowerShell
Photo Gallery Date/Time Format Vista file management
why need much more time to format USB drive media Vista hardware & devices
why need much more time to format USB drive media Vista hardware & devices
I don't have enough time to get rid of bugs I need to format HDD. Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46