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版

第31章. イベント・ハンドリング

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

31.0 はじめに

システム管理の多く は反応型である。システムサービスがシャットダウンしたとき、ファイルが作成または削除されたとき、Windowsレジストリに変更が加えられたとき、あるいは時間間隔が設定されたときに何らかのアクションを起こす。

システムの変更に対応する最も簡単な方法は、単純にポーリングすることだ。ファイルが作成されるのを待っているなら、それが現れるまで時々チェックすればいい。プロセスの開始を待っている場合は、Get-Process コマンドレットを呼び出し続けるだけだ。

しかし、レジストリやファイルシステム全体など、システムの巨大な部分を監視する必要がある場合には、すぐに破綻してしまう。

システム変更のポーリングに代わるものとして、多くのテクノロジーがイベントと呼ばれる自動通知をサポートしている。アプリケーションがこれらの自動通知に登録すると、ポーリングする必要がなくなり、発生と同時に対応できるようになる。

.NETはあるメソッドを定義し、WMIは別のメソッドを定義している。独自のイベントを生成したいスクリプトがある場合、どちらの技術も選択肢を提供していない。

PowerShellは、イベント関連コマンドレットの単一で一貫したセットを導入することで、この複雑さに対処している。これらのコマンドレットを使用すると、これらすべての異なるイベント ソースを操作できる。イベントが発生すると、PowerShellにその通知をイベントキューに保存させたり、Action スクリプトブロックを使用して自動的に処理させることができる:

PS > "Hello" > file.txt
PS > Get-Item file.txt

    Directory: C:\temp

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         2/21/2010  12:57 PM         16 file.txt

PS > Get-Process notepad

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
     64       3     1140       6196    63     0.06   3240 notepad

PS > Register-CimIndicationEvent Win32_ProcessStopTrace `
    -SourceIdentifier ProcessStopWatcher `
    -Action {
        if($EventArgs.NewEvent.ProcessName -eq "notepad.exe")
        {
            Remove-Item c:\temp\file.txt
        }
    }

PS > Stop-Process -n notepad
PS > Get-Item c:\temp\file.txt
Get-Item : Cannot find path 'C:\temp\file.txt' because it does not exist.

PowerShellのイベント処理をベースにすることで、刻々と変化するシステムに素早く反応するスクリプトを書くことができる。

31.1 自動的に発生するイベントに対応する ...

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