#Variables utilisateurs à modifier :
$debut = "21:00:00"
$fin = "7:00:00"
$batch = "C:\test.bat"
#Initialisation.
$dstart = get-date([System.DateTime]$debut)
$dstop = get-date([System.DateTime]$fin)
$cmd = "cmd.exe"
$arg = "/C ""$($batch)"""
cls
#Ajustement de la plage horaire.
$now = get-date
if ($dstart -ge $dstop) { $dstop = $dstop.AddDays(1) }
if ($dstart -le $now) { $dstop = $dstop.AddDays(1); $dstart = $dstart.AddDays(1); }
#Information de planification.
Write-Host "`r`n"
Write-Host "#-----------------------------------#"
Write-Host "# Planification #"
Write-Host "#-----------------------------------#"
Write-Host "# Lancement : $($dstart) #"
Write-Host "# Arrêt : $($dstop) #"
Write-Host "#-----------------------------------#"
Write-Host "`r`n"
#Attendre pour lancer la tâche.
While ($true)
{
$now = get-date
if (($now -gt $dstart) -and ($now -lt $dstop)) { break }
Sleep -s 1
}
#Lancement de la tâche.
Write-Host "$(get-date) - Lancement du batch."
$id = start-process -FilePath $cmd -ArgumentList $arg -Passthru
#Attendre pour fermer la tâche.
While ($true)
{
$now = get-date
if (($now -gt $dstop) -or ($id.HasExited)) { break }
Sleep -s 1
}
#Fermer la tâche.
if (!$id.HasExited) { Write-Host "$(get-date) - Kill du batch."; $id.Kill() }
else { Write-Host "$(get-date) - Fin du batch." }
$debut = "21:00:00"
$fin = "7:00:00"
$batch = "C:\test.bat"
#Initialisation.
$dstart = get-date([System.DateTime]$debut)
$dstop = get-date([System.DateTime]$fin)
$cmd = "cmd.exe"
$arg = "/C ""$($batch)"""
cls
#Ajustement de la plage horaire.
$now = get-date
if ($dstart -ge $dstop) { $dstop = $dstop.AddDays(1) }
if ($dstart -le $now) { $dstop = $dstop.AddDays(1); $dstart = $dstart.AddDays(1); }
#Information de planification.
Write-Host "`r`n"
Write-Host "#-----------------------------------#"
Write-Host "# Planification #"
Write-Host "#-----------------------------------#"
Write-Host "# Lancement : $($dstart) #"
Write-Host "# Arrêt : $($dstop) #"
Write-Host "#-----------------------------------#"
Write-Host "`r`n"
#Attendre pour lancer la tâche.
While ($true)
{
$now = get-date
if (($now -gt $dstart) -and ($now -lt $dstop)) { break }
Sleep -s 1
}
#Lancement de la tâche.
Write-Host "$(get-date) - Lancement du batch."
$id = start-process -FilePath $cmd -ArgumentList $arg -Passthru
#Attendre pour fermer la tâche.
While ($true)
{
$now = get-date
if (($now -gt $dstop) -or ($id.HasExited)) { break }
Sleep -s 1
}
#Fermer la tâche.
if (!$id.HasExited) { Write-Host "$(get-date) - Kill du batch."; $id.Kill() }
else { Write-Host "$(get-date) - Fin du batch." }