Skip to Content
PowerShellクックブック 第4版
book

PowerShellクックブック 第4版

by Lee Holmes
March 2025
Intermediate to advanced
1002 pages
14h 55m
Japanese
O'Reilly Media, Inc.
Content preview from PowerShellクックブック 第4版

第13章. ユーザ対話

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

13.0 はじめに

ほとんどのスクリプトは自動的に実行されるように設計されているが、スクリプトをユーザと対話させると便利なことがよく発見される。

ユーザから入力を得る最良の方法は、スクリプトや関数の引数やパラメータを使うことである。これにより、ユーザはスクリプトの実行に立ち会うことなく、スクリプトを実行することができる!

スクリプトにインタラクティブな利点がある(または必要である)場合、PowerShellはさまざまな可能性を提供する。単純にキー入力を待ったり、入力を促したり、よりリッチな選択肢ベースのプロンプトを表示したりすることができる。

ユーザとの対話はユーザ入力だけではない。入力機能に加え、PowerShellは出力もサポートしている。単純な文字列の表示から、より詳細な進捗報告やUIフレームワークとのインタラクションまで。

13.1 ユーザ入力の行を読み取る

問題

スクリプトの中でユーザからの入力を使いたい。

解決策

ユーザ入力を取得するには、 コマンドレットを使用する:Read-Host

PS > $directory = Read-Host "Enter a directory name"
Enter a directory name: C:\MyDirectory
PS > $directory
C:\MyDirectory

ディスカッション

Read-Host コマンドレットは、ユーザから 1 行の入力を読み取る。入力に機密データが含まれている場合、コマンドレットは パラメータをサポートし、この入力を として読み取る。-AsSecureString SecureString

ユーザ入力が日付、時刻、または数値を表す場合、ほとんどのカルチャではこれらのデータ型の表現が異なることに注意する。カルチャを意識したスクリプトの書き方については、レシピ13.6を参照のこと。

Read-Host コマンドレットの詳細については、次のようにタイプする。 Get-Help Read-Host.グラフィカル プロンプトを通してユーザ入力を読み取る例については、本書のコード例に含まれているRead-InputBox スクリプトを参照のこと。これらの例の入手方法については、「コード例の使用」を参照のこと

13.2 ユーザ入力のキーを読み取る

問題

スクリプトはユーザから1回のキー入力を得たい。

解決策

ほとんどのメソッドでは、キーの読み取りには :[Console]::ReadKey()

PS > $key = [Console]::ReadKey($true)
PS > $key

                 KeyChar                   Key              Modifiers
                 -------                   ---              ---------
                       h                     H                    Alt

高度にインタラクティブな使用(例えば、キーダウン・キーアップを気にする場合)には、これを使う:

PS > $key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") PS > $key VirtualKeyCode Character ControlKeyState KeyDown -------------- --------- --------------- ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

生成AI時代の新プログラミング実践ガイド Pythonで学ぶGPTとCopilotの活用ベストプラクティス

生成AI時代の新プログラミング実践ガイド Pythonで学ぶGPTとCopilotの活用ベストプラクティス

松本 直樹
デジタルアイデンティティのすべて ―安全かつユーザー中心のアイデンティティシステムを実現するための知識

デジタルアイデンティティのすべて ―安全かつユーザー中心のアイデンティティシステムを実現するための知識

Phillip J. Windley, Drummond Reed, 富士榮 尚寛, 柴田 健久, 花井 杏夏, 宮崎 貴暉, 塚越 雄登, 田島 太朗, 名古屋 謙彦, 村尾 進一, 瀬在 翔太, 松本 優大, 安永 未来, 池谷 亮平
Pythonクイックリファレンス 第4版

Pythonクイックリファレンス 第4版

Alex Martelli, Anna Martelli Ravenscroft, Steve Holden, Paul McGuire, 鈴木 駿, 株式会社クイープ

Publisher Resources

ISBN: 9798341626737