Powershell Timer?
Ich möchte einen Powershell Timer haben, welcher zusätzlich eine Progressbar anzeigt. Der Timer soll anzeigen, wie lange ich noch arbeiten muss bei einem Tag von 8std. Weiß jemand hier weiter? Habe alles ausprobiert aber komme nicht weiter…
Ich bin für jede Hilfe dankbar.
2 Antworten
$Start = Get-Date #Aktuelle Zeit
$Duration = New-TimeSpan -Hours 0 -Minutes 5 #Dauer für den Timer (zur Demo keine Stunden, sonst passiert nichts im Balken)
$End = $Start + $Duration
Do {
Start-Sleep -Seconds 1
$DisplayTime = New-TimeSpan -Start $(Get-Date) -End $End
$ElapsedTime = New-TimeSpan -Start $Start -End $(Get-Date)
$PercentElapsed = $ElapsedTime.Ticks/ ($Duration.Ticks/100) #Eigentlich egal welche Einheit, feiner als Ticks gehts nicht
$Time = "Noch:{0:D2}:{1:D2}:{2:D2} Vergangen:{3:F3}%" -f ($DisplayTime.Hours),($DisplayTime.Minutes), ($DisplayTime.Seconds), $PercentElapsed
Write-Progress -Activity "Hours to Work" -Status $Time -PercentComplete ([int]$PercentElapsed-1)
} While((Get-date) -lt $End)
pause
Edit : -PercentComplete ([int]$PercentElapsed-1) , weil bei ElapsedTime eben doch ein klein wenig mehr als die Vorberechnete Endzeit herauskommt, was dann im Resultat zu 101% für -Percentcomplete führt. Fehler...
Ich hatte etwas Zeit zum "spielen" einen Progressbar bekommt man auch ganz anders hin...
$StartTime = Get-Date
$StartDuration = New-TimeSpan -Hours 0 -Minutes 2 -Sec 0
$EndTime = $StartTime + $StartDuration
$StartTime.ToString("HH:mm:ss")
$EndTime.ToString("HH:mm:ss")
function UpdateDuration() {
$Now = Get-Date
$ElapsedTime = New-TimeSpan -Start $StartTime -End $Now
$PercentElapsed = $ElapsedTime.Ticks/ ($StartDuration.Ticks/100)
if ($PercentElapsed -ge 100) {$PercentElapsed=100}
return [pscustomobject] @{
RemainingTime = New-TimeSpan -Start $Now -End $EndTime
ElapsedTime = $ElapsedTime
PercentElapsed = $PercentElapsed
}
}
function Alert(){
#mache Krach...
}
Add-Type -a 'System.Windows.Forms'
$Form = New-Object 'System.Windows.Forms.Form'
$Form.Text = "End of Work Clock"
$Form.BackColor = "#000000"
$Form.TopMost = $True
$Form.Size = '600,200'
$Form.FormBorderStyle = 5 #https://docs.microsoft.com/de-de/dotnet/api/system.windows.forms.formborderstyle?view=windowsdesktop-6.0#felder
$ClockDisplay = New-Object 'System.Windows.Forms.Label'
$ClockDisplay.Location = "30,10"
$ClockDisplay.AutoSize = $True
$ClockDisplay.Font = "Consolas,32,style=Bold"
$ClockDisplay.ForeColor = "#ff0000"
$ClockDisplay.Text = 'Clock: {0:HH\:mm\:ss\.ff}' -f (Get-Date)
$Form.Controls.Add($ClockDisplay)
$DurationDisplay = New-Object 'System.Windows.Forms.Label'
$DurationDisplay.Location = "30,70"
$DurationDisplay.AutoSize = $True
$DurationDisplay.Font = "Consolas,16"
$DurationDisplay.ForeColor = "#00ff00"
$DurationDisplay.Text = 'Remaining Time: {0:hh\:mm\:ss} Elapsed: {1,7:f3}%' -f $StartDuration, 0
$Form.Controls.Add($DurationDisplay)
$ProgressBar = New-Object System.Windows.Forms.ProgressBar
$ProgressBar.Location = "0, 120"
$ProgressBar.Size = "584,20"
$ProgressBar.Style = "Continuous" #https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.progressbarstyle?view=windowsdesktop-6.0#fields
$ProgressBar.Value = 0
$ProgressBar.ForeColor ="#00FF00"
$Form.Controls.Add($ProgressBar)
$Timer1 = New-Object 'System.Windows.Forms.Timer'
$Timer1.Enabled = $True
$Timer1.Interval = 33 #30 FPS
$Timer1_Action={
$ClockDisplay.Text = 'Clock: {0:HH\:mm\:ss\.ff}' -f (Get-Date)
}
$Timer1.add_Tick($Timer1_Action)
$Timer2 = New-Object 'System.Windows.Forms.Timer'
$Timer2.Enabled = 1
$Timer2.Interval = 100
$Timer2_Action={
$Duration = UpdateDuration
$ProgressBar.Value = $Duration.PercentElapsed
$DurationDisplay.Text = 'Remaining Time: {0:hh\:mm\:ss} Elapsed: {1,7:f3}%' -f $Duration.RemainingTime, $Duration.PercentElapsed
#write-host $Duration
if ($Duration.PercentElapsed -eq 100){
$Timer2.Enabled = $False
$DurationDisplay.ForeColor = "#0090f0"
$ProgressBar.ForeColor ="#0090f0"
Alert
}
}
$Timer2.add_Tick($Timer2_Action)
$Null=$Form.ShowDialog()
$Form.Dispose()
Warum nicht lieber bei Windows Forms. Da hast du in Grafische Oberfläche und Progressbar gibt es dort auch.
Ja aber was hat es überhaupt ein Sinn das ganze mit Powershell zu machen. Also ich würde das lieber mit C# und Windows Forms machen. Da geht es einfacher, besser und schneller. Ach so und Timer als Element gibt es unter Windows Forms auch. Also da hast du alles was du brauchst.
Möchte es bei Powershell mal sehen um es dort zu können.