我有以下代码,当我直接在 powershell 中使用时它可以工作:
Get-WmiObject win32_operatingsystem | select @{LABEL=’LastBootUpTime’;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)
它返回:
LastBootUpTime
--------------
14/09/2019 10:41:50
但是当我在 Powershell 脚本 .ps1
中使用带有 invoke-command 的命令时,输出会返回更多信息:
LastBootUpTime PSComputerName RunspaceId
-------------- -------------- ----------
9/14/2019 10:41:50 AM 192.168.0.20 af08d2d8-c4f1-4f85-9d6c-e3f4ffe475c6
为什么会这样?
如果可能的话,我也希望没有 header LastBootUpTime。
最佳答案
Invoke-Command 将始终返回附加信息,在本例中为运行命令的位置和运行空间 ID。您始终可以将结果放入变量中,然后简单地打印出您想要的属性。例如
$result = invoke-command {your-command}
$result.LastBootUpTime
或简称
(invoke-command {your-command}).LastBootupTime
注意,当你使用wmi时,不一定非要使用invoke-command,你也可以直接给它传递-computer参数,在远程计算机上运行命令:
Get-WmiObject win32_operatingsystem -computer "remote_computer_name"
关于windows - 如何在 Powershell 上只获取服务器的正常运行时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57938580/