はじめに

検証とかするときSyslog受信するのに使うので。
fluentdなどでも大丈夫ですが、簡単に利用できます。

Syslog.ps1

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
$Udp = New-Object Net.Sockets.UdpClient -ArgumentList 514
$Sender = $null
 
Add-Type -TypeDefinition @"
       public enum Syslog_Facility
       {
               kern,
               user,
               mail,
               system,
               security,
               syslog,
               lpr,
               news,
               uucp,
               clock,
               authpriv,
               ftp,
               ntp,
               logaudit,
               logalert,
               cron,
               local0,
               local1,
               local2,
               local3,
               local4,
               local5,
               local6,
               local7,
       }
"@
 
Add-Type -TypeDefinition @"
       public enum Syslog_Severity
       {
               Emergency,
               Alert,
               Critical,
               Error,
               Warning,
               Notice,
               Informational,
               Debug
       }
"@
 
while($true) `
{
  if($Udp.Available) `
  {
    $Buffer = $Udp.Receive([ref]$Sender)
    $MessageString = [Text.Encoding]::UTF8.GetString($Buffer)
    $Priority = [Int]($MessageString -Replace "<|>.*")
    [int]$FacilityInt = [Math]::truncate([decimal]($Priority / 8))
    $Facility = [Enum]::ToObject([Syslog_Facility], $FacilityInt)
    [int]$SeverityInt = $Priority - ($FacilityInt * 8 )
    $Severity = [Enum]::ToObject([Syslog_Severity], $SeverityInt)
    $MessageString = "$MessageString $Facility $Severity"
    $MessageString = $MessageString -Replace "<.*>",""
    #Write-Host $MessageString
    $MessageString >> c:tempsyslog.log
  }
  [Threading.Thread]::Sleep(500)
}

最後にFacilityとSeverityを見やすいように付加

上記のスクリプトをバックグラウンドで動作させます。
コマンドプロンプトで実行

1
powershell -windowsstyle hidden syslog.ps1

まとめ

Windowsでは有料のSyslog Serverはありますが、Powershellだけでできるなら要らないですね。

参考元
http://pcnetbeginners.seesaa.net/article/393185613.html