gpt4 book ai didi

powershell - 在Powershell中检查远程服务器的UPTIME

转载 作者:行者123 更新时间:2023-12-03 00:29:43 26 4
gpt4 key购买 nike

我正在尝试使用脚本中的以下代码片段来检索远程服务器的UPTIME。

$lastboottime = (Get-WMIObject -Class Win32_OperatingSystem -ComputerName     $server -Credential $altcreds -ErrorAction SilentlyContinue).LastBootUpTime
$sysuptime = (Get-Date) - [System.Management.ManagementDateTimeconverter]::ToDateTime($lastboottime)
$uptime = " UPTIME : $($sysuptime.days) Days, $($sysuptime.hours) Hours, $($sysuptime.minutes) Minutes, $($sysuptime.seconds) Seconds"

执行脚本时出现以下错误:
Exception calling "ToDateTime" with "1" argument(s): "Specified argument was out of the range of valid values.
Parameter name: dmtfDate"

我无法确定错误消息是什么参数?

谢谢!

最佳答案

您很可能从第一行得到空值。就像Arco444在他的评论中显示的那样,您正在告诉命令如果命令失败,则不会通知您并继续处理。如果失败,$lastboottime将为$ null。如果您故意输入$null,可能会遇到类似的错误。

System.DirectoryServices.dll[System.Management.ManagementDateTimeconverter]::ToDateTime($null)

System.DirectoryServices.dll[System.Management.ManagementDateTimeconverter]::ToDateTime : The term 'System.DirectoryServices.dll[System.Management.ManagementDateTimeconverter]::ToDateTime' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + System.DirectoryServices.dll[System.Management.ManagementDateTimeconverter]::ToD ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (System.Director...er]::ToDateTime:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException



一个简单的if将检查变量中是否存在数据。 $uptime值也将反射(reflect)这一点。
$lastboottime = (Get-WMIObject -Class Win32_OperatingSystem -ComputerName     $server -Credential $altcreds -ErrorAction SilentlyContinue).LastBootUpTime
If($lastboottime){
$sysuptime = (Get-Date) - [System.Management.ManagementDateTimeconverter]::ToDateTime($lastboottime)
$uptime = " UPTIME : $($sysuptime.days) Days, $($sysuptime.hours) Hours, $($sysuptime.minutes) Minutes, $($sysuptime.seconds) Seconds"
} Else {
$uptime = " UPTIME : Unable to determine for host $server"
}

关于powershell - 在Powershell中检查远程服务器的UPTIME,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28045009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com