Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 检查ping,然后检查正常运行时间?_Powershell_Powershell 2.0_Powershell 3.0 - Fatal编程技术网

Powershell 检查ping,然后检查正常运行时间?

Powershell 检查ping,然后检查正常运行时间?,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,下面检查正常运行时间。我没有提及的一个条件(除了现在)是,如果服务器可以ping,但无法获得正常运行时间-如果是这种情况,我需要下面的脚本出错。我想不出怎么做——有什么想法吗 代码: 您还可以使用WMI获取计算机的上次启动时间: $wmi = [WMI] "" $operatingSystem = get-wmiobject Win32_OperatingSystem -computername "." $wmi.ConvertToDateTime($operatingSystem.LastBo

下面检查正常运行时间。我没有提及的一个条件(除了现在)是,如果服务器可以ping,但无法获得正常运行时间-如果是这种情况,我需要下面的脚本出错。我想不出怎么做——有什么想法吗

代码:


您还可以使用WMI获取计算机的上次启动时间:

$wmi = [WMI] ""
$operatingSystem = get-wmiobject Win32_OperatingSystem -computername "."
$wmi.ConvertToDateTime($operatingSystem.LastBootUpTime)

Bill

您还可以使用WMI获取计算机的上次启动时间:

$wmi = [WMI] ""
$operatingSystem = get-wmiobject Win32_OperatingSystem -computername "."
$wmi.ConvertToDateTime($operatingSystem.LastBootUpTime)
票据出错通常定义为返回非零退出代码,这可以通过

Exit 1

或者你喜欢什么号码

你也可以抛出异常,如果你想发送一个特定的错误消息和一个非常严重的错误,有一个答案。

出错通常被定义为返回一个非零的退出代码,这可以通过

Exit 1

或者你喜欢什么号码


你也可以抛出异常,如果你想发送一个特定的错误消息和一个非常严重的错误,答案是肯定的。

你可以将代码封装在try/catch中,并使用timespan对象。类似这样的东西(未经测试,但它给了您想法):


您可以将代码封装在try/catch中,并使用timespan对象。类似这样的东西(未经测试,但它给了您想法):


这个人也有同样的问题。您可以尝试创建一个作业来查询WMI并获取LBUT。然后在工作中设置一个计时器。但是如果您只想执行一些错误处理,那么应该使用
try{}
catch{}

try{
    $lbut = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime
}
catch{
    "FAILED"
    continue
}

.

这个人也有同样的问题。您可以尝试创建一个作业来查询WMI并获取LBUT。然后在工作中设置一个计时器。但是如果您只想执行一些错误处理,那么应该使用
try{}
catch{}

try{
    $lbut = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime
}
catch{
    "FAILED"
    continue
}

.

我现在无法从这里测试这一点,但也许可以尝试嵌套另一个if语句来枚举调用WMI的成功或失败

if (Test-Connection -ComputerName $computer -Quiet)
    {

    if((Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime){

        $LastBoot = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime
        $sysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($LastBoot)

我现在无法从这里测试这一点,但也许可以尝试嵌套另一个if语句来枚举调用WMI的成功或失败

if (Test-Connection -ComputerName $computer -Quiet)
    {

    if((Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime){

        $LastBoot = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime
        $sysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($LastBoot)

我的解决办法如下。我正在ping主机名并获取引导时间,如果它没有得到错误

[System.Net.Dns]::GetHostAddresses("hostname").ipaddresstostring
if($? -eq $true){
   Get-CimInstance -ClassName win32_operatingsystem | select lastbootuptime
}

我的解决办法如下。我正在ping主机名并获取引导时间,如果它没有得到错误

[System.Net.Dns]::GetHostAddresses("hostname").ipaddresstostring
if($? -eq $true){
   Get-CimInstance -ClassName win32_operatingsystem | select lastbootuptime
}

虽然我不确定这是否是个离题的话题,我认为您有更好的机会在Powershell on上获得答案。您可以通过获取运行时间最长的进程或查询系统事件日志以获取事件ID 6005来计算运行时间。我想我问得有点不正确-很多人认为我想要获得正常运行时间-这是不正确的-上面的代码确实获得了正常运行时间。我特别询问特定服务器的正常运行时间何时不可用,我希望它出错…即使服务器已启动并正在ping。虽然我不确定这是否是离题,我认为您有更好的机会在Powershell on上获得答案。您可以通过获取运行时间最长的进程或查询系统事件日志以获取事件ID 6005来计算运行时间。我想我问得有点不正确-很多人认为我想要获得正常运行时间-这是不正确的-上面的代码确实获得了正常运行时间。我特别询问特定服务器的正常运行时间何时不可用,我希望它出错…即使服务器已启动并正在ping。谢谢你,但我认为我问得有点不正确-很多人认为我希望获得正常运行时间-这是不正确的-上面的代码确实获得了正常运行时间。我特别询问特定服务器的正常运行时间何时不可用,我希望它出错…即使服务器已启动并正在ping。谢谢你,但我认为我问得有点不正确-很多人认为我希望获得正常运行时间-这是不正确的-上面的代码确实获得了正常运行时间。我特别询问某个特定服务器的正常运行时间何时不可用,我希望它出错…即使服务器已启动并正在ping。谢谢David-这帮助了我,并且认为您知道我在问什么!谢谢大卫,这帮了我的忙,我想你知道我在问什么!我将尝试这个方法-看起来类似于David用try-catch方法给出的方法。我将尝试这个方法-看起来类似于David用try-catch方法给出的方法。