跳转到PowerShell脚本中的另一部分

时间:2016-04-22 16:23:43

标签: powershell

我想从脚本中的一个部分跳转到同一个PowerShell脚本中的另一个部分。我的脚本现在只是从上到下,但我有几个任务。例如我希望能够从脚本开头的任务列表中进行选择,然后转到任务编号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...

2 个答案:

答案 0 :(得分:3)

这是一个通用解决方案,它使用代表任务的array 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

答案 1 :(得分:1)

我正在根据“bluuf”和“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