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
Is PsRemoting Enabled? If So You Could Invoke-Command on the remote
server to return Get-Date -Format HH:mm:ss
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/
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...
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:
> .
>
Method invocation failed because
[System.Management.ManagementObject#\Win32_OperatingSystem] doesn't contain a
method named 'LocalDateTime'.
"Karl Mitschke" wrote:
> .
>
That's strange.
What OS?
Try this:
Get-WmiObject Win32_OperatingSystem |Format-List *time*
Karl
http://unlockpowershell.wordpress.com/
CurrentTimeZone : 0
LastBootUpTime : 20100126114414.375199+000
LocalDateTime : 20100126171322.660000+000
"Karl Mitschke" wrote:
> .
>
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/
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:
> .
>
> 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/
"Karl Mitschke" wrote:
> .
>
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