跳转到PowerShell脚本中的其他部分

跳转到PowerShell脚本中的其他部分,powershell,Powershell,我想从脚本中的一个部分跳到同一PowerShell脚本中的另一个部分。我的脚本现在只是从上到下,但我有几个任务。e、 g.我希望能够从脚本开头的任务列表中进行选择,并转到任务2跳过任务1 我希望这对你有意义。看看我的脚本: Write-Host -ForegroundColor Yellow "welcome to my powershell script..." "" "" "" Start-Sleep -Seconds 1 Write-Host -ForegroundColor Yellow

我想从脚本中的一个部分跳到同一PowerShell脚本中的另一个部分。我的脚本现在只是从上到下,但我有几个任务。e、 g.我希望能够从脚本开头的任务列表中进行选择,并转到任务2跳过任务1

我希望这对你有意义。看看我的脚本:

Write-Host -ForegroundColor Yellow "welcome to my powershell script..."
""
""
""
Start-Sleep -Seconds 1
Write-Host -ForegroundColor Yellow "Choose a task:"
""
""

Write-Host -ForegroundColor Yellow "1. Clean up"
Write-Host -ForegroundColor Yellow "2. Uninstall Pre-Installed Apps" 
Write-Host -ForegroundColor Yellow "3. Something should be written here"
""
""
""

While ($Valg -ne "1" -or $Valg -ne "2" -or $Valg -ne "3") {

    $Valg = Read-Host "Choose a number from the task list"

    If ($Valg –eq "1") { Break }
    If ($Valg –eq "2") { Break }
    If ($Valg –eq "3") { Break }

    if ($Valg -ne "1" -or $Valg -ne "2" -or $Valg -ne "3") {
        ""
        Write-Host -ForegroundColor Red "Ups. Try again..."
    }
}

#### 1. First task should come here (clean up)
#some code here for the "cleaning up" task 

#### 2. Second task here
#some code here for the "Uninstall Pre-Installed Apps" task

#### 3. Third task there
#Some code for the third task here

#### And so on...

我更新的答案,以完成脚本的基础上输入的“布鲁夫”和“majkinator”。 使用Switch Case构造以及以下功能这是完整的工作解决方案。

#region Function Definitions. These come first before calling them
        function FirstTask
        (
            [string] $inputVariable
        )
        {
            "write any scipt for First task here without quotes. Input is: " + $inputVariable
        }

        function SecondTask
        {    
            "write any scipt for Second task here without quotes"
        }
        function ThirdTask
        {    
            "write any scipt for Third task here without quotes"
        }    

#endregion

#region Showing Options
Write-Host -ForegroundColor Yellow "welcome to my powershell script..."
""
""
""
Start-Sleep -Seconds 1
Write-Host -ForegroundColor Yellow "Choose a task:"
""
""

Write-Host -ForegroundColor Yellow "1. Clean up"
Write-Host -ForegroundColor Yellow "2. Uninstall Pre-Installed Apps" 
Write-Host -ForegroundColor Yellow "3. Something should be written here"
Write-Host -ForegroundColor Yellow "0. Exit"
""
""
""
#endregion

#region Getting input
While ($true) {

    $Valg = Read-Host "Choose a number from the task list"

    If ($Valg –eq "0") 
        { 
            "Thanks for using my utility"; 
            Break; 
        }

    If (($Valg -ne "1") -and ($Valg -ne "2") -and ($Valg -ne "3") -and ($Valg -ne "0")) {
        ""
        Write-Host -ForegroundColor Red "Ups. Try again..."
    }

    #region Main Code

    switch ($Valg) 
        { 
            1{
                FirstTask("sending input");
            } 
            2 {
                SecondTask;
            } 
            3 {
                ThirdTask;
            } 
            default { "Please select a correct option."}
        }
    #endregion
}
#endregion
下面是一个通用解决方案,它使用表示任务(消息和要调用的函数)的
PSCustomObject
数组
。它不需要开关,只需将新任务添加到阵列(并实现所需功能),而无需修改其余脚本:

# define a task list with the messages to display and the functions to invoke:
$taskList = @(
    [PSCustomObject]@{Message = 'Clean up'; Task = { Cleanup }}
    [PSCustomObject]@{Message = 'Uninstall Pre-Installed Apps'; Task = { Uninstall }}
    [PSCustomObject]@{Message = 'Something should be written here'; Task = { Print }}   
)

# define the functions:
function Cleanup()
{
    Write-Host "Cleanup"
}

function Uninstall()
{
    Write-Host "Uninstall"
}

function Print()
{
    Write-Host "Print"
}


# let the user pick a task:
Write-Host -ForegroundColor Yellow "Choose a task:"

$taskList | foreach -Begin { $i = 1;} -Process { 
    Write-Host -ForegroundColor Yellow ('{0}. {1}' -f ($i++), $_.Message) 
}

do 
{
    $value = Read-Host 'Choose a number from the task list'

}
while($value -match '\D+' -or $value -le 0 -or $value -gt $taskList.Count)

# invoke the task:
& $taskList[$value-1].Task

您好@Amen,非常感谢您的回复。你是说像这样$Valg=read host“从任务列表中选择一个数字”If($Valg-eq“1”){Break;}If($Valg-eq“2”){Break;}If($Valg-eq“3”){Break;}$Valg=2开关($Valg){1{Get date Break;}2{Get Service Break;}3{Get-ChildItem break;}默认值{Get-NetAdapter}}`Yes@PhillipLuiSkouGreen,应该可以这样做。尽管您可以进一步简化它,如下所示。除非您需要我从中删除的代码:$Valg=read host“从任务列表中选择一个数字”开关($Valg){1{Get date;break;}2{Get Service break;}3{Get ChildItem break;}默认值{Get NetAdapter}}我会使用函数,并根据脚本的类型决定是否使用switch语句(当然,在开关中我会调用函数)@Bluf您所说的是使用函数来增强代码。但这不是问题。问题只是如何根据变量的值跳转到各个部分,您可以使用Switch语句来执行这些操作。@AmanSharma,“跳转”是函数也可以执行的操作。Son Bluf是正确的,这是比swtich更好的解决方案,可以是e连一条线都没有。这正是我在评论中所想的。你好@jissak。非常感谢你抽出时间来制定一个通用解决方案。不幸的是,这对我现在对powershell的基本理解来说太多了,所以我将选择更简单的解决方案。我肯定会保存这个。也许我可以下次使用你的示例:)谢谢