我有以下代码,当我直接在 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
为什么会发生这种情况?
如果可能的话,我也希望没有标题 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"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)