Why Windows Task Scheduler doesnt run my powershell script?

Hi everyone, I’ve created a script where my virtual machins start at a time I choose. When I run the script before the time I put in my script, nothing happened, but when the time come, there is still nothing. But when I run mu script at the time I choose or later, the script does work perfectly. I tried to add the script in the windows task scheduler at X time, but when that time come, the task doesn’t run at all. I put the path for powershell.exe and I’ve tried to put the -ExecutionPolicy Bypass -NoExit -File “my script path” and still nothing works. Here’s my script if ever but I really dont understand I even did an sfc scannow.

function Start-VMWindows2019 {
    param (
        [string]$Nomvm,
        [string]$PathVBox,
        [datetime]$HeureStart,
        [string]$User
    )

    $NomTache = "StartVM $Nomvm"
    $PathVBoxManage = Join-Path $PathVBox "VBoxManage.exe"

    if ((Get-Date).ToString("HH:mm") -ge $HeureStart.ToString("HH:mm")) {
        Start-Process -FilePath $PathVBoxManage -ArgumentList "startvm", $Nomvm
    }

    # Faire en sorte que cette fonction se fasse tous les jours
    $ScriptPath = "C:\Users\user\$Nomvm.ps1" 

    # Créer un déclencheur quotidien à l'heure de démarrage
    $DailyTrigger = New-ScheduledTaskTrigger -Daily -At $HeureStart

    # Créer l'action pour exécuter le script
    $Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File $ScriptPath"

    # Créer les paramètres de la tâche planifiée
    $TaskParams = @{
        Action     = $Action
        Trigger    = $DailyTrigger
        User       = $User
        RunLevel   = "Limited"
        Force      = $true
    }

    # Créer la tâche planifiée
    Register-ScheduledTask -TaskName "StartVM_$Nomvm" @TaskParams
}

function Start-VMKali {
    param (
        [string]$Nomvm,
        [string]$PathVBox,
        [datetime]$HeureStart,
        [string]$User
    )

    $NomTache = "StartVM $Nomvm"
    $PathVBoxManage = Join-Path $PathVBox "VBoxManage.exe"

    if ((Get-Date).ToString("HH:mm") -ge $HeureStart.ToString("HH:mm")) {
        Start-Process -FilePath $PathVBoxManage -ArgumentList "startvm", $Nomvm
    }

    # Faire en sorte que cette fonction se fasse tous les jours
    $ScriptPath = "C:\Users\user\$Nomvm.ps1" 

    # Créer un déclencheur quotidien à l'heure de démarrage
    $DailyTrigger = New-ScheduledTaskTrigger -Daily -At $HeureStart

    # Créer l'action pour exécuter le script
    $Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File $ScriptPath"

    # Créer les paramètres de la tâche planifiée
    $TaskParams = @{
        Action     = $Action
        Trigger    = $DailyTrigger
        User       = $User
        RunLevel   = "Limited"
        Force      = $true
    }

    # Créer la tâche planifiée
    Register-ScheduledTask -TaskName "StartVM_$Nomvm" @TaskParams
}

function Start-VMS {
    param (
        [string]$Nomvm,
        [string]$PathVBox,
        [datetime]$HeureStart,
        [string]$User
    )

    $PathVBoxManage = Join-Path $PathVBox "VBoxManage.exe"

    if ((Get-Date).ToString("HH:mm") -ge $HeureStart.ToString("HH:mm")) {
        try {
            Start-Process -FilePath $PathVBoxManage -ArgumentList "startvm", $Nomvm -ErrorAction Stop
        } catch {
            Write-Host "Erreur lors du démarrage de la machine virtuelle $Nomvm : $_"
        }
    }

    # Faire en sorte que cette fonction se fasse tous les jours
    $ScriptPath = "C:\Users\user\$Nomvm.ps1" 

    # Créer un déclencheur quotidien à l'heure de démarrage
    $DailyTrigger = New-ScheduledTaskTrigger -Daily -At $HeureStart

    # Créer l'action pour exécuter le script
    $Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File $ScriptPath"

    # Créer les paramètres de la tâche planifiée
    $TaskParams = @{
        Action     = $Action
        Trigger    = $DailyTrigger
        User       = $User
        RunLevel   = "Limited"
        Force      = $true
    }
      try {
        # Créer la tâche planifiée
        Register-ScheduledTask -TaskName "StartVM_$Nomvm" @TaskParams -ErrorAction Stop
    } catch {
        Write-Output "Erreur lors de la création de la tâche planifiée pour $Nomvm : $_"
    }

    #Créer la tâche planifiée
    Register-ScheduledTask -TaskName "StartVM_$Nomvm" @TaskParams
}

# Définir les variables pour la première machine virtuelle, soit Windows 2019
$Nomvm1 = "Windows2019"
$PathVBox1 = "C:\Program Files\Oracle\VirtualBox"
$HeureStart1 = Get-Date -Hour 02 -Minute 06 -Second 0

# Appeler la fonction pour la première machine virtuelle
Start-VMWindows2019 -Nomvm $Nomvm1 -PathVBox $PathVBox1 -HeureStart $HeureStart1 -User "user"


# Définir les variables pour la deuxième machine virtuelle, soit Kali
$Nomvm2 = "Kali"
$PathVBox2 = "C:\Program Files\Oracle\VirtualBox"
$HeureStart2 = Get-Date -Hour 02 -Minute 06 -Second 0

# Appeler la fonction pour la deuxième machine virtuelle
Start-VMKali -Nomvm $Nomvm2 -PathVBox $PathVBox2 -HeureStart $HeureStart2 -User "user"

Your code is quite confusing and cumbersome. You’re creating functions you use only once and the function Start-VMS you don’t use at all.

Anyway … after a quick overview I think one of your issues is this:

These two lines of code create a [DateTime] object of the CURRENT DAY 2:06 AM. If you run this after 2:06 AM you create a point in time in the past.

Edit:
Depending on your locale setting you can use

$HeureStart2 = '2:06 AM'

or

$HeureStart2 = '02:06'
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.