0
1

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.

3年目の後輩とのやり取り

Posted at

1. あるイベントログにメッセージを出力できないんですけど

と、3年目の後輩から相談がありました。
「そんなの簡単じゃん、powershellで Write-Eventlogってのがあるからそれでやれば?」
と返したら、
「それでやってるんですけど、エラーになっちゃうんですよ…」
とお返事。んじゃ、見てみますかとなりました。

2. 後輩がやりたいこと

ヒアリングしたところ、イベントログのMicrosoft-Windows-DHCP Client Events/Adminに任意のメッセージを出したいとのこと。
image.png
まあ、Write-Eventlogでできると判断。

後輩もできると認識していて、以下のpowershellで実施していたけどエラーになっていたとのこと。

PS C:\Windows\system32> Write-EventLog -LogName "Microsoft-Windows-Dhcp-Client/Admin" -Source test -EventID 100 -Message "test"

3. 後輩がやったこと

まずは後輩とget-help Write-Eventlogでお作法を確認

PS C:\Windows\system32> get-help Write-EventLog -examples

名前
    Write-EventLog

概要
    Writes an event to an event log.

    ---- Example 1: Write an event to the Application event log ----
    PS C:\> Write-EventLog -LogName "Application" -Source "MyApp" -EventID 3001 -EntryType Information -Message "MyApp
    added a user-requested feature to the display." -Category 1 -RawData 10,20
    This command writes an event from the MyApp source to the Application event log.
   
    Example 2: Write an event to the Application event log of a remote computer
    PS C:\> Write-EventLog -ComputerName "Server01" -LogName Application -Source "MyApp" -EventID 3001 -Message "MyApp
    added a user-requested feature to the display."
    This command writes an event from the MyApp source to the Application event log on the Server01 remote computer.

記述方法は問題なさそう。ただし、-sourceでしている "test"っていうソースがないからエラってると判断。
私:「たぶん、ソースでしているtestが引けないからじゃないの?New-Eventlogでソース指定すればいいよ」
後輩:「それもやったんですが…」
私:「マジ!?じゃあ、エラーの画面見せて?」
それがこれ

PS C:\Windows\system32> Write-EventLog -LogName "Microsoft-Windows-Dhcp-Client/Admin" -Source test99 -EventID 100 -Mess
ge "test"
Write-EventLog : ソース 'test99' はログ 'Microsoft-Windows-Dhcp-Client/Admin' に登録されていません (ログ 'Internet Expl
orer' に登録されています)。Source と Log プロパティを一致させるか、または、Log を空の文字列に設定すると、自動的に Sourc
e プロパティに一致します。
発生場所 行:1 文字:1
+ Write-EventLog -LogName "Microsoft-Windows-Dhcp-Client/Admin" -Source ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Write-EventLog]、Exception
    + FullyQualifiedErrorId : ソース 'test99' はログ 'Microsoft-Windows-Dhcp-Client/Admin' に登録されていません (ログ '
Internet Explorer    ' に登録されています)。Source と Log プロパティを一致させるか、または、Log を空の文字列に設定する
と、自動的に Source プロパティに一致します。,Microsoft.PowerShell.Commands    .WriteEventLogCommand

4. 先輩(私)が対応したこと

私:「エラーメッセージに書いてあるじゃん。IEでイベントログ一度テストした?SourceとLogは一致しないとって書いてあるよ?」
これが正解でした

PS C:\Windows\system32> New-EventLog -LogName "Microsoft-Windows-Dhcp-Client/Admin" -source test1
PS C:\Windows\system32> Write-EventLog -LogName "Microsoft-Windows-Dhcp-Client/Admin" -Source test1 -EventID 100 -Message "test"

ほら出たぜ!
20200902.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?