powershellscheduled-tasksrestart

Restart the computer Twice using combination of Batch or VBS, and PowerShell scripts on Computer


I aim to create a PowerShell Script that restarts the computer twice; however, I have some doubts about this way that I am aiming to do this task. I write the following script with a Start-Sleep in between to Restart-Computer -Force:

The script looks a bit like this:

Restart-Computer -Force

Start-Sleep -Seconds 300

Restart-Computer -Force

Issue is the computer reboots once and it does not reboot again. I have a gut feeling that this wrong however, I know there is another way using scheduled tasks. However, when I think about the action I think it is shutdown /r so I tried this:

PowerShellScript1.ps1

Param(
    [Parameter(Mandatory=$true)]
    [string]$file1='',

    [string]$file2='',

    [string]$file3=''
)
$mypath = $PSCommandPath
$cwd = $PSScriptRoot
if ( (-not [String]::IsNullOrWhiteSpace($file2)) -and [String]::IsNullorWhiteSpace($file3))
{
    $taskname = "LLM"
    $taskExists = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskName }

    if($taskExists) {
    Unregister-ScheduledTask -Taskname "LLM" -Confirm:$false
    } 
    
    $taskTrigger = New-ScheduledTaskTrigger -AtLogOn 
    $taskname = "LLM"
    $taskAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -File $mypath -file1 $file2"
    # Describe the scheduled task.
    $description = "Automation Testing"
    $Principal = New-ScheduledTaskPrincipal -UserID "$env:USERDomain\$env:USERNAME" -LogonType ServiceAccount -RunLevel Highest
    # Register the scheduled task
    try{Register-ScheduledTask -TaskName $taskName -Action $taskAction -Principal $Principal -Trigger $taskTrigger -Description $description
    shutdown -r}
    catch{
    Write-Host $_
    }
}
elseif((-not [String]::IsNullOrWhiteSpace($file2)) -and -not [String]::IsNullorWhiteSpace($file3)){
    $taskTrigger = New-ScheduledTaskTrigger -AtLogOn 
    $taskname = "LLM"
    $taskAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -File $mypath -file1 $file2 -file2 $file3"
    # Describe the scheduled task.
    $description = "Automation Testing"
    $Principal = New-ScheduledTaskPrincipal -UserID "$env:USERDomain\$env:USERNAME" -LogonType ServiceAccount -RunLevel Highest
    # Register the scheduled task
    try{Register-ScheduledTask -TaskName $taskName -Action $taskAction -Principal $Principal -Trigger $taskTrigger -Description $description
    shutdown -r}
    catch{
    Write-Host $_
    }
}
if ( [String]::IsNullOrWhiteSpace($file2))
{
    Unregister-ScheduledTask -Taskname "LLM" -Confirm:$false
    shutdown -r
}

PowerShellScript2.ps1

shutdown /r

PowerShellScript3.ps1

shutdown /r
Unregister-ScheduledTask 'LLM' -Confirm:$false

main2.ps1

Start-Process -WindowStyle hidden -Wait powershell -ArgumentList "$PSScriptRoot\PowerShellScript1.ps1", "$PSScriptRoot\.ps1", "$PSScriptRoot\PowerShellScript3.ps1"

Now I have two ways of calling main2.ps1 I can either use VBS, or Batch. I wrote a batch file that on one-click it runs as admin, and a VBS script on one-click that runs as admin.

I am experiencing difficulty getting the script to cycle through two restarts. I was thinking of writing an exe in C++ or C# to use instead of PowerShell Script files but was unsure whether that would work.


Solution

  • The simplest approach is to use the user-specific RunOnce registry key to specify a one-time action for the next time the current user logs on again:

    Read-Host 'Press Enter to initiate the 1st restart.'
    
    Set-ItemProperty HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce (New-Guid) @'
    powershell.exe -noprofile -c "Read-Host \"Press Enter to restart again.\"; Restart-Computer -Force"
    '@
    
    Restart-Computer -Force
    

    Note: