windows - 如何仅在 Powershell 上获取服务器的正常运行时间?
问题描述
我有以下代码,当我直接在 powershell 中使用时它可以工作:
Get-WmiObject win32_operatingsystem | select @{LABEL=’LastBootUpTime’;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)
它返回:
LastBootUpTime
--------------
14/09/2019 10:41:50
.ps1
但是,当我在带有 invoke-command 的Powershell 脚本中使用该命令时,输出会返回更多信息:
LastBootUpTime PSComputerName RunspaceId
-------------- -------------- ----------
9/14/2019 10:41:50 AM 192.168.0.20 af08d2d8-c4f1-4f85-9d6c-e3f4ffe475c6
为什么会这样?
如果可能的话,我也希望没有标题 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"
推荐阅读
- javascript - 如果我根据前 3 个 id 选择 1 或 2 或 3,我在一个 div 中有五个 6 个复选框,其他 3 个复选框应该被禁用,我的意思是 4,5 和 6
- c# - 如何从 .NET 调用 luasql 脚本
- c++ - Google 语音识别(cpp 示例):未定义对 AssignDescriptors() 的引用
- c++ - 模板 Cast-Operator 规范
- hadoop - 我们可以检查 hive 删除表的历史记录或它周围的详细信息吗?
- node.js - 一个进程的标准输出到nodejs中另一个(ffmpeg)的标准输入
- c# - ServicePartitionResolver.ResolveAsync 永远挂起
- elasticsearch - ElasticSearch 中的英语分析器(词干)不起作用
- azure-devops - Azure DevOps 发布管道中的计数器
- angular - 没有将“exportAs”设置为“bs-modal”的指令(“