Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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 循环Foreach仅在脱机计算机上重复_Powershell - Fatal编程技术网

Powershell 循环Foreach仅在脱机计算机上重复

Powershell 循环Foreach仅在脱机计算机上重复,powershell,Powershell,我不确定哪里出了问题,我想跳过正在恢复联机的计算机,但它一直在检查联机和联机计算机 第一次更新是好的,它即将上线,它应该跳过,只有ping离线的一个输出的屏幕截图 g-10 Is coming online...Skipping to offline one's 192.168.0.1 Is coming online...Skipping to offline one's Testing to see if Hero is coming online... Hero is Offline. P

我不确定哪里出了问题,我想跳过正在恢复联机的计算机,但它一直在检查联机和联机计算机

第一次更新是好的,它即将上线,它应该跳过,只有ping离线的一个输出的屏幕截图

g-10 Is coming online...Skipping to offline one's
192.168.0.1 Is coming online...Skipping to offline one's
Testing to see if Hero is coming online...
Hero is Offline. Pausing for 2 seconds. Remaining attempts: 1
Testing to see if zero is coming online...
zero is Offline. Pausing for 2 seconds. Remaining attempts: 1
到目前为止一切都很好,但在线计算机又在重复

 g-10 Is coming online...Skipping to offline one's
    192.168.0.1 Is coming online...Skipping to offline one's
    Testing to see if Hero is coming online...
    Hero is Offline.
这是我的密码

$Comps = GC c:\restarted.txt
[int]$SleepTimer = "1" #minutes to attempt after
[int]$SleepSeconds = $SleepTimer * 2
[int]$Attempts = "2"
[int]$AttemptsCounter = 0

Do 
{
   $AttemptsCounter++
   $RemainingAttempts = ([int]$Attempts - [int]$AttemptsCounter)
   Foreach($comp in $comps){
   $Online = Test-Connection -ComputerName $Comp -Quiet
   IF($online -eq "True"){
  Write-Host "$comp" -BackgroundColor Green  -NoNewline
   Write-Host " Is coming online...Skipping to offline one's"
   }

   elseIf ($Online -NE "True") 
   {
      Write-Host "Testing to see if $Comp is coming online..."
      Write-Host "$comp" -BackgroundColor Red  -NoNewline
       Write-Host " is Offline" -BackgroundColor Red -ForegroundColor Black -NoNewline
       If ($AttemptsCounter -eq $Attempts) {
          Write-Host "."
       }
       Else {
          Write-Host ". Pausing for $SleepSeconds seconds. Remaining attempts: $RemainingAttempts"
       }
   }
}
   #Check the number of attempts, break out if reached.
   If ($AttemptsCounter -eq $Attempts) {break}

   #Delay
   Start-Sleep -s ($SleepTimer * 60)
}
While ($Online -NE "True")

If ($Online -NE "True") {
   Write-Host "Maximum number of attempts reached"
   }
Else {
   Write-Host
   Write-Host "Computer $Comp is " -NoNewline
   Write-Host "ONLINE" -BackgroundColor Green
}

测试连接
返回对象数组或$null,具体取决于ip或主机名是否处于活动状态

因此,如果你这样做就足够了:

IF($online){
  // if its alive
}
else
{
  // if its not responding
}
另外要指出的是,你不必

if(condition) {
}
elseif(condition) {
}
如果您正在测试“bool-question-answer”,则不需要
elseif
。在您的情况下,如果您正在检查计算机的状态,那么您的答案不能超过2个(是/否)。因此,在本例中,
elseif
是冗余的

if(condition) {
  // if the condition is true
}
else {
  // if the condition is not true. you can't have the third option
}
此外,您的if条件
if($Online-NE“True”)
正在检查变量
$Online
是否与字符串“True”或“True”不相等,因为
-NE
是不区分大小写的条件运算符。
您应该检查变量是否包含任何内容或是否包含$Null,具体取决于ip或主机名是否处于活动状态

因此,如果你这样做就足够了:

IF($online){
  // if its alive
}
else
{
  // if its not responding
}
另外要指出的是,你不必

if(condition) {
}
elseif(condition) {
}
如果您正在测试“bool-question-answer”,则不需要
elseif
。在您的情况下,如果您正在检查计算机的状态,那么您的答案不能超过2个(是/否)。因此,在本例中,
elseif
是冗余的

if(condition) {
  // if the condition is true
}
else {
  // if the condition is not true. you can't have the third option
}
此外,您的if条件
if($Online-NE“True”)
正在检查变量
$Online
是否与字符串“True”或“True”不相等,因为
-NE
是不区分大小写的条件运算符。
您应该检查变量是否包含任何内容或是否包含$Null

请尝试以下代码

它对
c:\restarted.txt
中的每一行基本上执行以下操作:

  • 测试计算机是否联机(注意:我添加了
    -计数1
  • 如果是
    联机
    :打印一些绿色文本并
    中断
  • 如果处于
    脱机状态
    :打印红色文本,等待并递减$RemainingAttempts变量,然后重新执行,直到
    $RemainingAttempts
    0
    或计算机处于
    联机状态
  • 如果
    $RemainingAttempts
    变量为
    0
    则打印更多文本

    $ComputerNameArray = Get-Content -Path c:\restarted.txt
    [int]$SleepTimer = "1" #minutes to attempt after
    [int]$Attempts = "2"
    $DefaultBackgroundColor = (Get-Host).ui.rawui.BackgroundColor
    
    foreach($ComputerName in $ComputerNameArray) {
        $AttemptsCounter = 0
        $RemainingAttempts = $Attempts - $AttemptsCounter
    
        Write-Host "Testing to see if ""$ComputerName"" is coming online..." -BackgroundColor $DefaultBackgroundColor
    
        while($RemainingAttempts -gt 0) {
            if(Test-Connection -ComputerName $ComputerName -Quiet -Count 1) {
                Write-Host """$ComputerName""" -BackgroundColor Green  -NoNewline
                Write-Host " Is coming online...Skipping to offline one's"
                break
            } else {
                Write-Host """$ComputerName""" -BackgroundColor Red  -NoNewline
                Write-Host " is Offline" -BackgroundColor Red -ForegroundColor Black -NoNewline
                Write-Host ". Pausing for $SleepTimer minutes. Remaining attempts: $($RemainingAttempts - 1)"
                Start-Sleep -Seconds ($SleepTimer * 60)
                $RemainingAttempts--
            }
        }
    
        if($RemainingAttempts -eq 0) {
            Write-Host "Maximum number of attempts reached" -BackgroundColor $DefaultBackgroundColor
        }
    }
    
  • 希望这有帮助

    如果您对代码有任何疑问,请告诉我

    韩元
    Guenther

    请尝试以下代码

    它对
    c:\restarted.txt
    中的每一行基本上执行以下操作:

  • 测试计算机是否联机(注意:我添加了
    -计数1
  • 如果是
    联机
    :打印一些绿色文本并
    中断
  • 如果处于
    脱机状态
    :打印红色文本,等待并递减$RemainingAttempts变量,然后重新执行,直到
    $RemainingAttempts
    0
    或计算机处于
    联机状态
  • 如果
    $RemainingAttempts
    变量为
    0
    则打印更多文本

    $ComputerNameArray = Get-Content -Path c:\restarted.txt
    [int]$SleepTimer = "1" #minutes to attempt after
    [int]$Attempts = "2"
    $DefaultBackgroundColor = (Get-Host).ui.rawui.BackgroundColor
    
    foreach($ComputerName in $ComputerNameArray) {
        $AttemptsCounter = 0
        $RemainingAttempts = $Attempts - $AttemptsCounter
    
        Write-Host "Testing to see if ""$ComputerName"" is coming online..." -BackgroundColor $DefaultBackgroundColor
    
        while($RemainingAttempts -gt 0) {
            if(Test-Connection -ComputerName $ComputerName -Quiet -Count 1) {
                Write-Host """$ComputerName""" -BackgroundColor Green  -NoNewline
                Write-Host " Is coming online...Skipping to offline one's"
                break
            } else {
                Write-Host """$ComputerName""" -BackgroundColor Red  -NoNewline
                Write-Host " is Offline" -BackgroundColor Red -ForegroundColor Black -NoNewline
                Write-Host ". Pausing for $SleepTimer minutes. Remaining attempts: $($RemainingAttempts - 1)"
                Start-Sleep -Seconds ($SleepTimer * 60)
                $RemainingAttempts--
            }
        }
    
        if($RemainingAttempts -eq 0) {
            Write-Host "Maximum number of attempts reached" -BackgroundColor $DefaultBackgroundColor
        }
    }
    
  • 希望这有帮助

    如果您对代码有任何疑问,请告诉我

    韩元
    根瑟

    虽然我知道根瑟·施密茨已经拿到了绿色支票,但我想继续提供一些额外的细节和建议


    使用
    -Quiet
    开关测试连接
    返回一个
    [布尔值]
    类型。这是一个简单的对/错。我喜欢在比较字符串值和布尔值时格外小心,因为根据测试框架的不同,可能会得到一些奇怪的结果。例如:

    'false' -eq $true
    $true -eq 'false'
    
    顶部的一个值等于false,底部的一个值等于true,因为PowerShell正在将第二个对象转换为与第一个对象相同的类型,并且非空字符串为true。因此,我强烈建议坚持类型化比较。因此,我首先要做的代码更改是:

    if ($Online -eq $true) {
    
    正如@popularity已经提到的,您正在做一个简单的比较,因此不需要
    elseif
    ,只需要
    else

    第二个问题是,从测试列表中删除计算机是不可能的。按照编写代码的方式,如果测试列表中的最后一台计算机处于“关闭”状态,那么它将再次通过所有计算机。如果列表中的最后一台计算机是“向上”的,那么它将退出
    do | while
    循环,而不考虑您正在测试的计算机数量和关闭的计算机数量。您可以通过在测试文件中放置1个已知主机和1个伪主机的简单测试来验证这一点。如果已知良好的主机是第一个,它将继续循环,直到
    $attemptsconter
    达到允许的最大值。如果你把清单翻过来,它会在测试已知的好主机后立即退出

    您的初始代码在所有计算机中循环一次,然后返回并重新启动。在Guenther的例子中,他们一次循环一台计算机,检查直到它出现,或者在超过测试计数后退出。我认为Guenther的例子可能就是你想要的,但是如果你想让它在所有的计算机中循环,然后只测试那些失败的计算机,我会这样写:

    $comps = Get-Content -path 'c:\restarted.txt'
    [int]$SleepTimer = 1
    [int]$Attempts = 2
    [int]$AttemptsCounter = 0
    
    do {
        Write-Host "Testing Network, attempt $($AttemptsCount + 1)"
        $newComps = @()
        foreach($comp in $comps) {
            $online = Test-Connection -Quiet -Count 1 -ComputerName $comp
            if ($online) {
                Write-Host "$comp" -BackgroundColor Green  -NoNewline
                Write-Host " Is coming online...Skipping to offline one's"
            } else {
                $newComps += $comp
                Write-Host "$comp" -BackgroundColor Red  -NoNewline
                Write-Host " is Offline" -BackgroundColor Red -ForegroundColor Black
            }
        }
        $comps = $newComps
        $AttemptsCounter++
        Start-Sleep -Seconds ($SleepTimer * 60)
    }
    while (($comps.count -gt 0) -and ($AttemptsCounter -lt $Attempts))
    
    if ($comps.Count -gt 0) {
        Write-Host "Exceeded Attempts Counter" -BackgroundColor Red
    }
    

    在这段代码中,它获取计算机列表,当它在计算机上出现故障时,它会将其填充到一个临时列表中以供重用。

    虽然我知道Guenther Schmitz已经获得了绿色检查,但我想继续提供一些额外的细节和指针


    使用
    -Quiet
    开关测试连接
    返回一个
    [布尔值]
    类型。这是一个简单的对/错。我喜欢在比较字符串值和布尔值时格外小心,因为根据测试框架的不同,可能会得到一些奇怪的结果。例如:

    'false' -eq $true
    $true -eq 'false'
    
    上一个等于false,下一个等于true,因为PowerShell正在将第二个对象转换为