Hello,
In addition to “PowerShell: Run command at certain date/time”, we got the request to include a start time also.
In the script below, we are doing such thing. (write-host commands are in Dutch tho).
In case the user specifies a valid start date/time, the script will start the program. (example program).
If the user specifies a valid date/time, the script will kill that program. This script was made for DuvelRadio.
###################################### $Title="DuvelRadio Automatische Stream" $Process="Cyberduck" $ProcessStart="C:\Program Files (x86)\Cyberduck\Cyberduck.exe" ####################### $host.ui.RawUI.WindowTitle = $title Clear-Host #resize, just for fun $pshost = Get-Host # Get the PowerShell Host. $pswindow = $pshost.UI.RawUI # Get the PowerShell Host's UI. $newsize = $pswindow.windowsize # Get the UI's current Window Size. $newsize.width = 70 # Set the new Window Width to 150 columns. $newsize.height = 20 $pswindow.windowsize = $newsize # Set the new Window Size as active. ####start section $Running = Get-Process -Name $Process -ErrorAction SilentlyContinue if($Running -ne $null) # evaluating if the program is running { Write-Host "Stream is al aktief." exit } $StartTijd= Read-Host -Prompt 'Geef start tijdstip in' if (($Starttijd.Length -le 4) -Or (-not $Starttijd.Contains(":"))) { Write-Host " > We starten niks" $Start = "" } else { #date/time validation $timecheck= $starttijd -as [datetime] if (!$timecheck) { Write-Host "Geen valide datum/tijd" exit 1 } Write-Host " > We starten om $Starttijd" $Start = $Starttijd } $StopTijd= Read-Host -Prompt 'Geef stop tijdstip in' if (($Stoptijd.Length -le 4) -Or (-not $Stoptijd.Contains(":"))) { Write-Host " > We stoppen niks" $Stop = "" } else { #date/time validation $timecheck= $stoptijd -as [datetime] if (!$timecheck) { Write-Host "Geen valide datum/tijd" exit 1 } Write-Host " > We stoppen om $Stoptijd" $Stop = $Stoptijd } ### if both are not specified, then we are useless if ($Stop -eq "" -And $Start -eq "" ) { Write-Host "Geen tijden gespecifieerd, we kunnen niks doen." exit } ### start section if ($Start -ne "") { $Running = Get-Process -Name $Process -ErrorAction SilentlyContinue if($Running -ne $null) # evaluating if the program is still running { Write-Host "> Stream is al aktief. We kunnen niks starten" } else { #start block $host.ui.RawUI.WindowTitle = "$title start om $Start" Write-Host "Er zal tot '$Start' gewacht worden om de stream te starten" Write-Host "Er wordt gewacht..." do { Start-Sleep -m 100 } until ((get-date) -ge (get-date $Start)) Write-Host "Het is nu $Start" $Running = Get-Process -Name $Process -ErrorAction SilentlyContinue if($Running -ne $null) # check again if process is running { Write-Host "Stream reeds aktief." } else { Write-Host "Stream wordt gestart." start-process $ProcessStart ##give the process a bit time to start Start-Sleep -m 500 } # end start block } } ### stop section if ($Stop -ne "") { $Running = Get-Process -name $Process -ErrorAction SilentlyContinue if($Running -eq $null) # evaluating if the program is still running { Write-Host "Stream is niet meer aktief." exit } $host.ui.RawUI.WindowTitle = "$title stopt om $Stop" Write-Host "Er zal tot $Stop gewacht worden om de stream te stoppen" Write-Host "Er wordt gewacht..." do { Start-Sleep -m 100 } until ((get-date) -ge (get-date $Stop)) Write-Host "Het is $Stop. Stream wordt gestopt." $Running = Get-Process -name $Process -ErrorAction SilentlyContinue if($Running -eq $null) # evaluating if the program is still running { Write-Host "Stream was niet meer aktief." exit 1 } Stop-Process -name $Process -ErrorAction SilentlyContinue Write-Host "Stream was gestopt op $(Get-Date)" } ###############################