1

I spitted the steps of my setup script into different functions. I want to be able to stop the script sometime. If I restart the script it should continue the last step where I stopped. So I logged the last step in a Tempfile. If I start the script it reads the tempfiles last logged step and jumps to that step on restart.

Function Step1 {
Set-Content -Path $TEMPFILE -value "Step1" #"Setting current step" into a temp file

do first stuff #part in th function. e.g. copy files, add access rights...

Step2 #calling the next function
}

Function Step2 {
Set-Content -Path $TEMPFILE -value "Step2"

do second stuff 

Step3...  
}

...Step 25

On Startup the script checks if a Tempfile is existing and add the last step into a variable[String]

Is there any way to get this String Variable into a "Jump To Function" Sequence? like:...

If ($GETSTEP){
   Write-Host "Continuing with $GETSTEP"
   $GOTO = ${function:GETSTEP}
   &GOTO
   }else{
   Write-Host "GET STARTET"
   Step1
   }

PowerShell version 5.1.14393.3471 ...

0

2 Answers 2

2

Assuming $GETSTEP contains the script as a string:

if($GETSTEP){
  # Use ScriptBlock.Create() to create a scriptblock from a string
  $scriptBlock = [scriptblock]::Create($GETSTEP)
  &$scriptBlock
}

If $GETSTEP contains the name of a function to execute, then it's much simpler - use the call operator (&) to invoke the function:

if($GETSTEP){
    & $GETSTEP
}
Sign up to request clarification or add additional context in comments.

3 Comments

And you can say $function:myfunc = $scriptblock
Thanks for your answer! But in ´$GETSTEP´ will be the NAME of the next Function.
@oberpiller much simpler then, just do & $GETSTEP then
2

This will create the function getstep:

$function:getstep = [scriptblock]::Create($GETSTEP)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.