Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get time from a remote server with PowerShell v2

1,723 views
Skip to first unread message

Mehdis

unread,
Jan 22, 2010, 7:30:01 AM1/22/10
to
Hi all,

I'm looking for a way to obtain the time from a remote server. I wanted to
use Get-Date but this doesnt seem to have any options for remote computers so
I'm using WMI Win32_LocalTime option like this:

$hour = Get-WmiObject Win32_LocalTime -computerName servername |
Select-Object -Property Hour
$minute = Get-WmiObject Win32_LocalTime -computerName servername |
Select-Object -Property Minute
$second = Get-WmiObject Wind32_LocalTime -computerName servername |
Select-Object -Property Second

However this returns values in the form of (for example):

@{Hour=16}
@{Minute=26}
@{Second=30}

Is there an easy way to obtain the time from a remote server as just a
numeric value without the '@{Hour=' e.t.c?

Any advice or suggestions would be much appreciated.
Many thanks
Mehds

qa_warrior

unread,
Jan 22, 2010, 9:29:33 AM1/22/10
to

Is PsRemoting Enabled? If So You Could Invoke-Command on the remote
server to return Get-Date -Format HH:mm:ss

Karl Mitschke

unread,
Jan 22, 2010, 10:31:23 AM1/22/10
to
Hello Mehdis,

Hi;

Try this, taken from http://www.vistax64.com/powershell/91254-wmi-local-time-format.html
:

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

You can change the format on the second line ("MM/dd/yyyy HH:mm:ss") to suit
your needs

Karl
http://unlockpowershell.wordpress.com/


Martin Zugec

unread,
Jan 22, 2010, 10:44:08 AM1/22/10
to

Hi Mehdis,

you can use Get-WmiObject Win32_OperatingSystem and retrieve LocalDateTime.

In order to convert it to regular [DateTime], use
([wmi]'').ConvertToDateTime() function

Martin

"Mehdis" <Meh...@discussions.microsoft.com> wrote in message
news:E126F784-858F-4FC0...@microsoft.com...

Mehdis

unread,
Jan 26, 2010, 7:16:01 AM1/26/10
to
Hi qa_warrior, thanks I didn'y know about that command. I did it as:

Invoke-Command -ComputerName servername -ScriptBlock{Get-Date -Format
HH:mm:ss}

However it's a little slow on getting the result. Using WMI is a lot faster.

Thanks again.

"qa_warrior" wrote:

> .
>

Mehdis

unread,
Jan 26, 2010, 9:48:01 AM1/26/10
to
Hi Karl, thanks for this info and link. Unfortuantely I couldnt get it
running. I kept getting:

Method invocation failed because
[System.Management.ManagementObject#\Win32_OperatingSystem] doesn't contain a
method named 'LocalDateTime'.

"Karl Mitschke" wrote:

> .
>

Karl Mitschke

unread,
Jan 26, 2010, 11:00:02 AM1/26/10
to
Hello Mehdis,

That's strange.

What OS?

Try this:
Get-WmiObject Win32_OperatingSystem |Format-List *time*

Karl
http://unlockpowershell.wordpress.com/


Mehdis

unread,
Jan 26, 2010, 12:18:01 PM1/26/10
to
I'm running on windows 7 at the moment. The last line you gave me worked
giving the following:

CurrentTimeZone : 0
LastBootUpTime : 20100126114414.375199+000
LocalDateTime : 20100126171322.660000+000

"Karl Mitschke" wrote:

> .
>

Karl Mitschke

unread,
Jan 26, 2010, 12:52:11 PM1/26/10
to

Hello Mehdis,

OK, So we know the error you received is invalid.

Try this (one line)

$dt = (Get-WmiObject-ComputerName -Query "select LocalDateTime from win32_operatingsystem").LocalDateTime

#dt

Karl
http://unlockpowershell.wordpress.com/


Mehdis

unread,
Jan 27, 2010, 5:57:01 AM1/27/10
to
Hi Karl, thanks again for your help, that one works too, giving:

20100127103947.257000+000

On another forum someone suggested using exapandProperty:

$hour = Get-WmiObject Win32_LocalTime -computerName webserver2 |
Select-Object -expandProperty Hour

....and then obviously do this for minute and second too.

Located here:
http://powershellcommunity.org/Forums/tabid/54/aff/1/aft/4655/afv/topic/Default.aspx

I guess its another option.

"Karl Mitschke" wrote:

> .
>

Karl Mitschke

unread,
Jan 27, 2010, 10:28:23 AM1/27/10
to
Hello Mehdis,

> Hi Karl, thanks again for your help, that one works too, giving:
>
> 20100127103947.257000+000

Then, if $dt = 20100127103947.257000+000, the second line should run fine:


([wmi]'').ConvertToDateTime($dt).tostring("MM/dd/yyyy HH:mm:ss")

Karl

Karl
http://unlockpowershell.wordpress.com/


Mehdis

unread,
Jan 28, 2010, 5:34:03 AM1/28/10
to
Yes it works now. I made the mistake of leaving them on the same line when I
pasted into my script editor. Thank you for your help!

"Karl Mitschke" wrote:

> .
>

Karl Mitschke

unread,
Jan 28, 2010, 10:26:38 AM1/28/10
to
Hello Mehdis,

You are welcome.

Karl

Karl
http://unlockpowershell.wordpress.com/


Ashraf

unread,
Feb 25, 2010, 11:50:48 AM2/25/10
to

Great help.
That works fine
([wmi]'').ConvertToDateTime((Get-WmiObject -ComputerName $servername -
Query "select LocalDateTime from
win32_operatingsystem").LocalDateTime).tostring("MMM dd,yyyy
HH:mm:ss")

Thanks

0 new messages