从 powershell 获取 UpTime 到可用的方式,但无法让它工作

Get UpTime from powershell into a usable way, but can't get it to work

我一直在制作这个程序,我需要向 powershell 发送命令,在 return 它给了我系统正常运行时间(分钟更好但不是强制性的)

由于我还不习惯使用powershell,我在获取这个intel时遇到了很多问题。

这是我尝试过的:

    (get-date) - (gcim Win32_OperatingSystem).LastBootUpTime

给了我正常运行时间,但我仍然不知道如何使用它,所以我仍然需要以某种方式添加如下内容:

    | Select-String -Pattern "TotalMinutes"

但是我需要(以某种方式)让那个 powershell 给我时间 return 这样我就可以使用它了。 也许到剪贴板?

    | clip

但如果我将所有这些加起来,none 就可以了。 放入剪贴板只是我获取此信息的一种方式,其他方式也可能有效。 我对此还是很陌生,如果我用愚蠢的问题伤害了你的智力,我很抱歉。

提前致谢

通过减去两个 [datetime] (System.DateTime) instances, you get a [timespan] (System.TimeSpan) 实例,您可以将其存储在变量中:

$timeSpanSinceBoot = (Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime

然后您可以根据需要访问其属性,例如 .TotalMinutes:

$timeSpanSinceBoot.TotalMinutes

要检查时间跨度值类型的成员,请使用 Get-Member cmdlet:

$timeSpanSinceBoot | Get-Member # lists properties and methods