2016-04-22 2 views
4

同じPowerShellスクリプトの別のセクションにスクリプト内のあるセクションからジャンプしたいとします。私のスクリプトは今のところトップからボトムに行くだけですが、いくつかのタスクがあります。例えば私はスクリプトの冒頭にあるタスクリストから選択し、タスク番号2のスキップタスク1に行きたいと思っています。PowerShellスクリプト内の別のセクションにジャンプする

これはあなたにとって意味があると思っています。私のスクリプトを見てください:

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... 

答えて

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 
+0

こんにちは@Amenと返事をどうもありがとうございました。 このような意味ですか? '$ Valg = ($ Valg -eq「1” )の場合、 "タスク・リストから番号を選択し、" ホストが読み取り{ブレーク;} IF($ Valg -eq「2” ){ブレーク;} {ブレーク;}($ Valg -eq「3” )の場合= 2 スイッチ($ Valg) {1 { 取得日付 $ Valg。 休憩。 } 2 { Get-Service break; } 3 { Get-ChildItem break; } デフォルト{Get-NetAdapter} } ' –

+0

はい@PhillipLuiSkouGreen、それはうまくいくはずです。あなたは以下のようにそれをさらに簡素化することができます。 *** $ Valg = read-host "タスクリストから番号を選択" switch($ Valg){1 {Get-date;ブレーク; } 2 {Get-Service break; } 3 {Get-ChildItem break;} } default {Get-NetAdapter}} *** –

+1

私は関数を使用し、スクリプトの種類に応じてswitch文を使用するかどうかを決定します(スイッチではもちろん関数を呼び出します) – bluuf

3

タスク(を起動するメッセージ機能)を表すPSCustomObjectarrayを使用ジェネリック溶液です。これは、スイッチを必要としない、あなたは、単に、残りのスクリプトを変更することなく、配列(および所望の機能を実装する)に新しいタスクを追加することができます。

# 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 
+0

これはまさに私がコメントで考えたものです。 – majkinetor

+0

こんにちは@jissak。包括的なソリューションを作成するために、ありがとうございます。残念ながら、PowerShellの基本的な理解のためには今のところだから、もっと簡単な解決策を考えてみましょう。私は間違いなくこれを保存します。おそらく私はあなたの例を別の時間を使用することができます:)ありがとう –