如何在Powershell中的指定点启动脚本

Dan*_*ell 6 powershell

我有一个PowerShell脚本,我希望能够定义不同的起点.一旦命中起点,脚本就会从该点开始接收并继续执行脚本中的其余代码.我不相信case语句会起作用,因为我认为不会让脚本从任何起点定义出来.

我希望在脚本启动时会看到类似的内容.

请选择您的起点:

  1. 开始
  2. 从第2步开始
  3. 从第3步开始等.....

选择完成后,脚本跳转到该点,然后将运行脚本的其余部分.

答:代码最终会看起来像这样:

#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!' 
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

Rom*_*min 5

AFAIK,在PowerShell中没有这样的东西.如果您需要简单的东西,这可能适合您:

*)使用定义为函数的步骤创建脚本.最后的每个函数都调用下一个步骤函数:

# Steps.ps1
function Step1 {
    'Step 1'
    Step2
}
function Step2 {
    'Step 2'
    Step3
}
function Step3 {
    'Step 3'
    'Done!'
}
Run Code Online (Sandbox Code Playgroud)

*)如果你想从第1步开始:dot-source the Steps.ps1并调用Step1:

. .\Steps.ps1
Step1
Run Code Online (Sandbox Code Playgroud)

*)如果你想从第2步开始:dot-source the Steps.ps1并调用Step2:

. .\Steps.ps1
Step2
Run Code Online (Sandbox Code Playgroud)