Axel
Axel

Reputation: 113

Powershell script to start where it left after restart

Invoke-WebRequest -UseBasicParsing -Uri "url here" -OutFile  C:\Users\Public\hello.ps1
Invoke-WebRequest -UseBasicParsing -Uri "url here" -OutFile  C:\Users\Public\hello2.ps1

Write-Output "All files are downloaded. Please wait for execution..."

## Executing hello.ps1 and hello2.ps1
## These scripts should be inside the same folder as parent script.

Write-Output "Executing hello.ps1 script..."
. "$PSScriptRoot\hello.ps1" 

Write-Output "Executing hello2.ps1 script..."
. "$PSScriptRoot\hello2.ps1" 

Write-Output "Your Machine is ready!"

In this script, I'm downloading two scripts from the internet and executing them on the machine.

hello.ps1 script handles stopping services and other changes which require reboot. After the reboot, user has to again run the script manually.

How can I handle this situation via some kind of task scheduling within the main powershell script to run the hello.ps1 script again untill the full script is finished executing.

PS. Please let me know if anymore information is required.

Upvotes: 1

Views: 1166

Answers (2)

Axel
Axel

Reputation: 113

I've achieved the same using TaskScheduling. Just before the execution of the first script, I've created a task with the name "MachineCreation", as long as there are reboots, task will run and when there are no reboots, task will get unregistered.

Invoke-WebRequest -UseBasicParsing -Uri "url here" -OutFile  C:\Users\Public\hello.ps1
Invoke-WebRequest -UseBasicParsing -Uri "url here" -OutFile  C:\Users\Public\hello2.ps1

Write-Output "All files are downloaded. Please wait for execution..."

## Executing hello.ps1 and hello2.ps1
## These scripts should be inside the same folder as parent script.

Write-Output "Executing hello.ps1 script..."
if ( !(Get-ScheduledTask -TaskName 'MachineCreation' -ErrorAction SilentlyContinue ) ) {
    $Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "C:\users\public\machine_creation.ps1"
    $Trigger = New-ScheduledTaskTrigger -AtLogOn
    $Task = New-ScheduledTask -Action $Action -Trigger $Trigger
    Register-ScheduledTask -TaskName 'MachineCreation' -InputObject $Task
}
. "$PSScriptRoot\hello.ps1" 

Write-Output "Executing hello2.ps1 script..."
. "$PSScriptRoot\hello2.ps1" 
Unregister-ScheduledTask -TaskName 'MachineCreation' -Confirm:$false

Write-Output "Your Machine is ready!"

Upvotes: 0

Ivan Mirchev
Ivan Mirchev

Reputation: 839

You may considier using workflows:

Workflows are critical in an IT environment because they can survive reboots and recover automatically from common failures. You can disconnect and reconnect from sessions and computers running workflows without interrupting workflow processing, and suspend and resume workflows transparently without data loss. Each activity in a workflow can be logged and audited for reference. Workflows can run as jobs and can be scheduled by using the Scheduled Jobs feature of PowerShell.

More details: https://learn.microsoft.com/en-us/powershell/module/psworkflow/about/about_workflows?view=powershell-5.1

Upvotes: 1

Related Questions