Home > front end >  Running a script in PowerShell that repeats every 300s that won't print Get-Date
Running a script in PowerShell that repeats every 300s that won't print Get-Date

Time:10-04

super new to scripting and this script was found online and has been edited slightly. I figured I'd try to make it print the time after each cycle, but can't make it print an updated time; it just repeats the first printed time.

$myshell = New-Object -com "Wscript.Shell"
$today = Get-Date -format t

while ($true)
{
    $myshell.sendkeys("{F15}")
    Write-Host "Last Run $today"
    Start-Sleep -Seconds 300
} 

I've tried "Write-Output" as well but no change. Picture of Write-Host

CodePudding user response:

Get-Date is only called once, before your loop so that's why it always displays the same date time.

If you modify your code like this:

$myshell = New-Object -com "Wscript.Shell"
$today = Get-Date -format t

while ($true)
{
    $myshell.sendkeys("{F15}")
    Write-Host "Last Run $(Get-Date -format t)"
    Start-Sleep -Seconds 300
}  

It will call Get-Date from within the loop.

  • Related