LoginSignup
2
1

More than 5 years have passed since last update.

(Powershell)ユーザーに文字列を入力させて、それを元に判定して処理分岐させるサンプル

Last updated at Posted at 2016-09-18
  • ユーザーに文字列を入力させて、それを元に判定して処理分岐させるサンプルです

# ユーザーから受け取った文字列ごとに、処理を分岐させます。

$MAIN = "〜の処理"

# ユーザーに入力をしてもらい、$inputに格納
$input = Read-Host "${MAIN}の処理を開始します。よろしいですか?(y/n)"

Write ↓
Write ↓

  # ユーザーの入力($input)結果から、処理を分岐
  switch ($input) {
    "y" {
        # y の場合の処理
        Write "${MAIN}を実行します"

          # コマンドをここに書く

        }

    "n" {
        # n の場合の処理
        Write "${MAIN}を中止します"
        sleep 5
        exit

        }

    default {
            # yでもnでもない場合の処理 ("default"は「上記の選択肢以外」という意味)
            Write "yでもnでもない文字が入力されました。${MAIN}を中止します"
            sleep 5
            exit

            }
  }

# yのとき、コマンドからの標準出力後に改行がない場合、改行
[System.Console]::WriteLine("")

Write ↓
Write ↓

# y のとき、コマンドの戻り値($lastexitcode)を判定($?にするときもある)
  switch ($lastexitcode) {
    "0" {
        Write "正常終了しました"

        }

    default {
        Write "失敗しました with ${lastexitcode}"

        }

  }


Write "この画面は20秒後に終了します"
sleep 20
exit

2
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1