从单独的文件中创建 returns 的 powershell 计数器

Creating a powershell counter that returns from a separate file

我是编码新手,但我的任务是解决一个需要一段代码的问题。我需要计算此脚本出现的次数 运行。由于它需要停止,因此需要将所述计数保存在其他地方,因此我认为它需要转到一个单独的文件。我试着用简单的英语输入步骤,希望有人能帮我把它放到 PowerShell 中。

open a file
print number it has
look at the number
    if it is <=2
        than increase the number by 1 in file
            end
    if it is =3
        than wait 10 minutes
            change number to 1 in file
                end

所以这应该完全符合您的要求。但它很糟糕,Start-Sleep 是最糟糕的方法。你可能想看看 Register-ScheduledJob

$Filepath = "C:/myfile.txt"

$Counter = Get-Content -Path $Filepath

#To echo it to the command line
$Counter

If ($Counter -le 2){
    $NewCounter = $Counter + 1
    Set-Content -Path $Filepath -Value $NewCounter
    }

elseif ($Counter -ge 3) {
    Start-Sleep -Seconds 600
    $NewCounter = 1
    Set-Content -Path $Filepath -Value $NewCounter
    }