windows - 如何在 Powershell 上只获取服务器的正常运行时间?

标签 windows powershell

我有以下代码,当我直接在 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/

相关文章:

powershell - 尝试使用 Invoke-SQLCmd 和 -ErrorAction SilentlyContinue Catch

sql-server - 将数组中的 PowerShell 值插入到 SQL Server 表中

powershell - 如何从 Get-AzureRmResourceGroup 标签检索键值对

python - 如何在启用 visual studio 环境的情况下运行 SublimeText

python - vscode "no refactorings available"for python

powershell - 如何通过 WMI 读取应用程序和服务日志?

powershell - Post Build PowerShell 脚本不包括已安装的模块

windows - 如何通过PowerShell为Windows 10上的当前用户设置Windows开始菜单布局

windows - 批处理文件 - 使用变量

c# - Windows - 以编程方式检查系统硬件上是否可以托管 WLAN 网络