如何在Powershell上仅获得服务器的正常运行时间? [英] How to get only the uptime of the server on Powershell?
问题描述
我有以下代码,当我直接在powershell中使用时它可以工作:
I have the following code, It's work when I use directly within powershell:
Get-WmiObject win32_operatingsystem | select @{LABEL=’LastBootUpTime’;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)
它返回:
LastBootUpTime
--------------
14/09/2019 10:41:50
但是当我在 Powershell 脚本 .ps1
中使用带有 invoke-command 的命令时,输出会返回更多信息:
But when I use the command within a Powershell script .ps1
with invoke-command the output returns more informations:
LastBootUpTime PSComputerName RunspaceId
-------------- -------------- ----------
9/14/2019 10:41:50 AM 192.168.0.20 af08d2d8-c4f1-4f85-9d6c-e3f4ffe475c6
为什么会这样?
如果可能,我也希望没有标题 LastBootUpTime.
If possible, I'd like without the header LastBootUpTime too.
推荐答案
Invoke-Command 将始终返回附加信息,在这种情况下,命令运行的位置和运行空间 ID.您始终可以将结果放入变量中,然后简单地打印出您想要的属性.例如.
Invoke-Command will always return additional information, in this case where the command was run and the runspace id. You can always get results into a variable and simply print out the property you want.E.g.
$result = invoke-command {your-command}
$result.LastBootUpTime
或简称
(invoke-command {your-command}).LastBootupTime
注意,当你使用wmi的时候,你不一定要使用invoke-command,你也可以直接给它传递-computer参数来对远程计算机运行命令:
Note that when you are using wmi, you do not need to necessarily use invoke-command, you can also directly pass -computer parameter to it to run the command against a remote computer:
Get-WmiObject win32_operatingsystem -computer "remote_computer_name"
这篇关于如何在Powershell上仅获得服务器的正常运行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!