きなきなのFire挑戦日誌

ふつーのサラリーマンがふつーにFireに憧れて、株でFireを目指す日誌です。

PowerShell イベントログ取得(Get-Eventlog、Get-WinEvent)

●基本は、、

 ・テキストファイル(SrvList.txt)を1行ずつ読み込みループ。

  ノード(サーバ、ホスト)のWindowsイベントログを

  重大、警告、エラーでフィルターする

 

 というロジックは変わらない。

 そのうえで、バッチからキックして、自動化・スケジューリング化して

 PowetShellで作成する。

 

 そのロジックの勉強と理解。

 下記は参考で、実際作成したコードとは違う、、、、。

 どんなんやったかな~

 

 

■パターン1

$Services = Get-WmiObject -Class Win32_Service
foreach ($Service in $Services) {
$ts = 0
$ds = 0
switch($Service.StartMode) {
"Manual" { $JS = "手動" }
"Disabled" { $JS = "無効" }
"Auto" { $JS = "自動" }
}
$no = New-Object -TypeName PSObject -Property @{
Status = $Service.State
Name = $Service.Name
DisplayName = $Service.DisplayName
StartMode = $JS
}
if (Test-Path -Path "HKLM:\SYSTEM\CurrentControlSet\Services\$($Service.Name)\TriggerInfo\") {
$ts = 1
}
if ($Service.StartMode -eq "Auto" -and $Service.DelayedAutoStart -eq 1) {
$ds = 1
}
if ($ts -eq 1 -Or $ds -eq 1) {
if ($ts -eq 1 -and $ds -eq 1) {
$no.StartMode = "$($JS) (遅延開始、トリガー開始)"
}
Elseif ($ts -eq 1) {
$no.StartMode = "$($JS) (トリガー開始)"
}
Elseif ($ds -eq 1) {
$no.StartMode = "$($JS) (遅延開始)"
}
}
$no
}

 

■パターン2

$Services = Get-WmiObject -Class Win32_Service
foreach ($Service in $Services) {
$ts = 0
$ds = 0
switch($Service.StartMode) {
"Manual" { $JS = "手動" }
"Disabled" { $JS = "無効" }
"Auto" { $JS = "自動" }
}
$no = New-Object -TypeName PSObject -Property @{
Status = $Service.State
Name = $Service.Name
DisplayName = $Service.DisplayName
StartMode = $JS
}
if (Test-Path -Path "HKLM:\SYSTEM\CurrentControlSet\Services\$($Service.Name)\TriggerInfo\") {
$ts = 1
}
if ($Service.StartMode -eq "Auto" -and $Service.DelayedAutoStart -eq 1) {
$ds = 1
}
if ($ts -eq 1 -Or $ds -eq 1) {
if ($ts -eq 1 -and $ds -eq 1) {
$no.StartMode = "$($JS) (遅延開始、トリガー開始)"
}
Elseif ($ts -eq 1) {
$no.StartMode = "$($JS) (トリガー開始)"
}
Elseif ($ds -eq 1) {
$no.StartMode = "$($JS) (遅延開始)"
}
}
$no
}

 

 

■パターン3

$triggers = Get-ChildItem "HKLM:\SYSTEM\CurrentControlSet\Services" |
Where-Object { $_.GetSubkeyNames().Contains("TriggerInfo") } |
ForEach-Object { $_.Name.Split("\")[-1] }

$startMode = @{ Manual = "手動"; Disabled = "無効"; Auto = "自動"; Unknown = "不明" }
$startOption = @{ 01 = " (トリガー開始)"; 10 = " (遅延開始)"; 11 = " (遅延開始、トリガー開始)" }

$serviceData = Get-CimInstance -ClassName Win32_Service | Select-Object @(
@{ n = "表示名"; e = { $_.DisplayName } }
@{ n = "サービス名"; e = { $_.Name } }
@{ n = "スタートアップの種類"; e = { $startMode[$_.StartMode] + $startOption[10 * ($_.StartMode -eq "Auto" -and $_.DelayedAutoStart) + $triggers.Contains($_.Name)] } }
@{ n = "状態"; e = { if($_.State -eq "Running") { "実行" } else { "停止" } } }
)

$serviceData

 

●参考

Get-WinEvent

https://yvernis.blog.fc2.com/blog-entry-155.html

https://win.just4fun.biz/?PowerShell/%E4%BB%BB%E6%84%8F%E3%81%AE%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88%E3%83%AD%E3%82%B0%E3%82%92%E5%87%BA%E5%8A%9B%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95

 

 

●雑感

便利なんだけど、、、リソース使うな~

 

 

 

以上