0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

リモートホストへのGet-Eventlogが遅いのが自業自得だった

Last updated at Posted at 2021-01-08

何の話か

リモート先のWindowsイベントログを取得したくGet-EventLog -ComputerName HOSTNAMEとするとげろげろ遅い。遅かった理由は自分の勘違い。勘違いしていた書き方を直して早くした。

Get-EventLog

Windowsイベントログを取得するコマンドレット。
例えばチェックディスクの結果ログ(ソース名Wininit)であれば、以下で取得できる。

chkdsk結果ログを取得する
# chkdsk結果ログの情報取得
Get-EventLog -LogName Application -Source "Wininit"

# 内容を表示したい場合
(Get-EventLog -LogName Application -Source "Wininit").Message

-EntryType(Warning、Infomationなど)、-After``-Before(日付時刻指定)、-username(ユーザー名指定)、-Newest n(新しいものからn件)など表示を絞り込めるパラメータがある。

リモートホストに使うと遅い

Get-EventLogをローカル環境で実行した場合と、リモートホストに対して使用(-ComputerName HOSTNAME)した場合で、実行時間に大きな開きがある。

Get-EventLog実行時間
# ローカル環境で
Measure-Command{
  Get-EventLog -LogName Application -Source "Wininit"
} | Select-Object TotalSeconds

# TotalSeconds
# ------------
# 3.7275934

# リモートホストに対して
Measure-Command{
  Get-EventLog -LogName Application -Source "Wininit" -ComputerName HOSTNAME
} | Select-Object TotalSeconds

# TotalSeconds
# ------------
# 128.8473589

これは指定したイベントログの内容を全部読んでパラメータ指定したものに該当するのを抽出しているせいらしい。リモートホストから貰ってたら時間がかかって当然。

そこで、-Newest 100と読み込む個数を指定すれば早くなるかな?と思い実行した。
しかし、これが全然早くならない。あれー?

Get-EventLogが早く…ならない
Measure-Command{
  Get-EventLog -LogName Application -Source "Wininit" -ComputerName HOSTNAME -Newest 100
} | Select-Object TotalSeconds

# TotalSeconds
# ------------
# 124.5613644

-Newest 100の意味

Get-EventLog -LogName Application -Source "Wininit" -ComputerName HOSTNAME -Newest 100と指定した場合の動作は、

誤:HOSTNAMEの イベントログApplicationから 新しいログから順に100個読み込んで ソース名Wininitのものを探せ。
正:HOSTNAMEの イベントログApplicationから ソース名Wininitのものを 新しいログから順に100個見つかるまで探せ。(100個なければ最後まで読むことになる)

…なのであった。
よく読んでみれば自分で書いた通りに動いているだけなんである。

改良案

先にログを100個だけ読んで、それからパイプで渡してフィルタかけてやればいい。

改良案
Measure-Command{
  Get-EventLog -LogName Application -Newest 100 -ComputerName HOSTNAME |
  Where-Object{$_.Source -eq "Wininit"}
} | Select-Object TotalSeconds

# TotalSeconds
# ------------
# 1.4705983

おお早い。

なお、これだと直近100個以内にお目当てのログがないと何も出てこない。
チェックディスクの結果ログなら終わった後すぐ読みに行くので、まず100個か200個以内くらいにあるから、まあ構わないか。

0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?