Surviving a reboot

hello everyone,

need a little help on this one. I and trying to write a script that would survive a reboot and resume when the system/server comes backup. I get the following script from . credit to lasse W… I cant seem to get it to work. if i can get it to do what its suppose to do, i can figure out the rest. or if anyone knows a better way to get this accomplished.

$global:started = $false
$global:startingStep = $step
$global:RegRunKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
$global:restartkey  = "Restart-And-Resume"
$global:powershell = (Join-Path $env:windir "system32\windowsPowerShell\v1.0\powershell.exe")

function Test-key([string]$path, [string] $key)
{
	return ((Test-Path $path) -and ((Get-Key $path $key) -ne $null))
}
function Remove-key([string] $path, [string] $key)
{
Remove-ItemProperty -Path $path -Name $key	
}
function Set-Key([string]$path, [string]$key, [string]$value)
{
	Set-ItemProperty -Path $path -Name $key -Value $value
}
function Get-key([string]$path, [string]$key)
{
return (Get-ItemProperty $path).$key	
}

function Restart-And-Resume([string]$script, [string]$step)
{
Restart-and-Run $global:restartkey "$global:powershell $script -Step $step"	
}
function Clear-Any-Restart([string]$key = $global:restartkey)
{
	If (Test-key $global:RegRunKey $key)
	{
		Remove-key $global:RegRunKey $key}
}
function Should-Run-step([string]$prospectStep)
{
	if ($global:startingStep -eq $prospectStep -or $global:started)
	{
		$global:started = $true
	}	
	Return $global:started
	}
function Restart-and-Run([string]$key, [string]$run)
{
	Set-Key $global:RegRunKey $key $run
	Restart-Computer
	exit
	
}

function Wait-for-keypress
{
	$Host.UI.RawUI.ReadKey("IncludeKeyDown") | Out-Null
	$Host.UI.RawUI.FlushInputBuffer()
	
}


param($step = "A")
$script = $MyInvocation.MyCommand.Definition
$scriptPath = Split-Path -Parent $script 
.(Join-Path $scriptPath asjob .ps1)

Clear-Any-Restart

if (Should-Run-step "A")
{
	Write-Host "A"
	Wait-for-keypress " will continue after reboot, press any key "
	Restart-And-Resume $script "B"
}

if (Should-Run-step "B")
{	
Write-Host "B"
}

If (Should-Run-step "C")
{
Write-Host "C"	
}

Wait-for-Keypress "test script complete, press any key to exit"

the following are the error i get

The term ‘param’ is not recognized as the name of a cmdlet, function, script fi
le, or operable program. Check the spelling of the name, or if a path was inclu
ded, verify that the path is correct and try again.
At C:\Users\Dev\Desktop\Script Test Area\asjob.ps1:62 char:6

  • param <<<< ($step = "A")
    • CategoryInfo : ObjectNotFound: (param:String) , CommandNotFou
      ndException
    • FullyQualifiedErrorId : CommandNotFoundException

Join-Path : A positional parameter cannot be found that accepts argument '.ps1'
.
At C:\Users\Dev\Desktop\Script Test Area\asjob.ps1:65 char:12

  • .(Join-Path <<<< $scriptPath asjob .ps1)
    • CategoryInfo : InvalidArgument: (:slight_smile: [Join-Path], ParameterBindi
      ngException
    • FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
      .Commands.JoinPathCommand

thank you

I never say this but consider workflow. This is exactly why it exists.

from what i read workflow will only work on window 8 and R2 2012.

No. It’s a core feature of PowerShell v3.