I have a powershell script I want to be able to define different starting points for. Once the starting point is hit the script would pick up from that point and continue through the remaining code in the script. I don't believe that a case statement will work since I don't think that will just let the script flow through from whatever starting point is defined.
I would expect to see something like this when the script was started.
Please choose your starting point:
When the selection is made the script jumps to that point then will run through the remainder of the script.
Answer: The code is going to end up looking something like this:
#steps
$stepChoice = read-host 'Where would you like to start.'
switch($stepChoice)
{
1{Step1}
2{Step2}
3{Step3}
}
function Step1 {
'Step 1'
Step2
}
function Step2 {
'Step 2'
Step3
}
function Step3 {
'Step 3'
'Done!'
}
Thanks for your help
ps1. Then, to execute the PowerShell script from the command line, launch the Windows command line by typing "cmd" into the search box and clicking the result. Type the full path of the PowerShell script, such as "C:\Example\example_script. ps1" and press enter.
Right-click the line where you want to set a line breakpoint, and then click Toggle Breakpoint. Or, click the line where you want to set a line breakpoint, and press F9 or, on the Debug menu, click Toggle Breakpoint.
AFAIK, there is nothing like this in PowerShell. If you need something simple this might work for you:
*) Create a script with steps defined as functions. Each function in the end calls the next step-function:
# Steps.ps1
function Step1 {
'Step 1'
Step2
}
function Step2 {
'Step 2'
Step3
}
function Step3 {
'Step 3'
'Done!'
}
*) If you want to start with step 1: dot-source the Steps.ps1 and call Step1:
. .\Steps.ps1
Step1
*) If you want to start with step 2: dot-source the Steps.ps1 and call Step2:
. .\Steps.ps1
Step2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With