为什么我不能两次使用同一个Get-Date变量而不浪费时间

时间:2019-07-31 13:16:22

标签: powershell

我希望我的脚本在没有互联网连接时将时间和日期写入日志。

执行我的if代码时,日志文件会更新日期和时间,但是执行else代码时应该记录下来(如果Internet断开)会记录错误的时间。

$InternetTest = {
    Test-Connection 192.168.8.1 -Quiet
}
$WebsiteTest = {
    $result = Test-Connection 192.168.8.101 -Quiet
    $D = Get-Date

    $Outputreport0 = "Site is down!     $d"
    $Outputreport1 = "Site is Up!       $d"

    if ($result -ne 0) {
        $Outputreport1 | Out-File C:\Users\simeo\Desktop\WebsiteTest1\log.txt -Append
        Write-Host "Up and Running"
    } else {
        $Outputreport0 | Out-File C:\Users\simeo\Desktop\WebsiteTest1\log.txt -Append
        Write-Host "DOWN!!!!!!!!"
    }
}
$Timer1 = {
    function Start-Countdown {
        Param(
            [Int32]$Seconds = 10,
            [string]$Message = "Pausing for 10 seconds..."
        )
        foreach ($Count in (1..$Seconds)) {
            Write-Progress -Id 1 -Activity $Message -Status "Retrying in $Seconds seconds, $($Seconds - $Count) left" -PercentComplete (($Count / $Seconds) * 100)
            Start-Sleep -Seconds 1
        }
        Write-Progress -Id 1 -Activity $Message -Status "Completed" -PercentComplete 100 -Completed
    }

    Start-Countdown -Seconds 10 -Message "Local internet connection unavailable "
}
$Timer2 = {
    function Start-Countdown {
        Param(
            [Int32]$Seconds = 8,
            [string]$Message = "Pausing for 8 seconds..."
        )
        foreach ($Count in (1..$Seconds)) {
            Write-Progress -Id 1 -Activity $Message -Status "Retrying in $Seconds seconds, $($Seconds - $Count) left" -PercentComplete (($Count / $Seconds) * 100)
            Start-Sleep -Seconds 1
        }
        Write-Progress -Id 1 -Activity $Message -Status "Completed" -
        PercentComplete 100 -Completed
    }

    Start-Countdown -Seconds 10 -Message "Website is down"
}

$InternetReport = "Local internet is down    $d"

for () {
    Clear-Host

    if (&$InternetTest -ne 0) {
        Write-Host "Internet UP"
        &$WebsiteTest
        Start-Sleep -Seconds 5
    } else {
        Write-Host "Internet Down"
        $InternetReport | Out-File C:\Users\simeo\Desktop\WebsiteTest1\Link_log.txt -Append
        &$Timer
    }
}

两个日志文件中的正确时间和日期是正确的。

1 个答案:

答案 0 :(得分:0)

我会用这种方式重做。在脚本块外可能有一个$ d2可以解决您的问题。也许您不小心将其设置在其他地方。我更改了自己的测试日志路径。

function InternetTest {
    Test-Connection 192.168.8.1 -Quiet -count 1 # gateway
}

function WebsiteTest {
    $result = Test-Connection microsoft.com -Quiet -count 1  # test failure, timeout 3-4 sec
    # $result = Test-Connection yahoo.com -Quiet -count 1
    $D1 = Get-Date

    $Outputreport0 = "Site is down!     $d1"
    $Outputreport1 = "Site is Up!       $d1"

    if ($result) {
        $Outputreport1 | Out-File log.txt -Append
        Write-Host "Up and Running"
    } else {
        $Outputreport0 | Out-File log.txt -Append
        Write-Host "DOWN!!!!!!!!"
    }
}

function Start-Countdown {
    Param(
        [Int32]$Seconds = 10,
        [string]$Message = "Pausing for $seconds seconds..."
    )
    foreach ($Count in 1..$Seconds) {  # watch big arrays with the .. operator
        Write-Progress -Id 1 -Activity $Message -Status "Retrying in $Seconds seconds, $($Seconds - $Count) left" -PercentComplete ($Count / $Seconds * 100)
        Start-Sleep -Seconds 1
    }
    Write-Progress -Id 1 -Activity $Message -Status "Completed" -PercentComplete 100 -Completed
}

$d2 = get-date
$InternetReport = "Local internet is down    $d2"

for($i = 1; $i -le 3; $i++) {  # trying it 3x
    if (InternetTest) {
        Write-Host "Internet UP"
        WebsiteTest
        Start-Sleep -Seconds 5
    } else {
        Write-Host "Internet Down"
        $InternetReport | Out-File Link_log.txt -Append
        start-countdown # timer
    }
}

实际上,您不能以这种方式从脚本块(或函数)中获得$ d,这是一个不同的范围:

& { $d = get-date }
$d  # unchanged

您才刚刚开始,并且已经在使用匿名函数。大多数人甚至都不了解它们。