Get-/Test-EventLogSource
System.Diagnostics
方法是有限的。计算机上只能有一个来源。不同的计算机可能具有相同的源,但在不同的日志中。根据我的经验,在使用这些方法并创建/删除日志和源之后,您开始遇到问题。我写了以下内容来验证我的自定义日志/源。
Set-StrictMode -Version Latest
function Get-EventLogSource {
[CmdletBinding()]
param(
[string]$LogFile = '*',
[string]$Source = '*'
)
Get-CimInstance -Class Win32_NTEventLOgFile -Verbose:$false | ForEach-Object {
$_logName = $PSItem.FileName
$PSItem.Sources | ForEach-Object {
$oResult = New-Object PSCustomObject -Property @{
Source = $PSItem
LogName = $_logName
} | Select-Object -Property Source, LogName
Write-Output $oResult
}
} | Sort-Object -Property Source | Where-Object { $PSItem.Source -like $Source -and $PSItem.LogName -like $LogFile }
}
function Test-EventLogSource {
[CmdletBinding()]
param(
[string]$LogFile = '*',
[Parameter(Mandatory)]
[string]$Source
)
$_result = Get-EventLogSource -LogFile $LogFile -Source $Source
return ($null -ne $_result)
}
Clear-Host
#Test-EventLogSource -LogFile 'System' -Source '.NET*' -Verbose
#Test-EventLogSource -LogFile 'Application' -Source '.NET*' -Verbose
#Test-EventLogSource -LogFile 'dummy' -Source '.NET*' -Verbose
#Test-EventLogSource -LogFile '*' -Source '.NET*' -Verbose
#Test-EventLogSource -Source '.NET*' -Verbose
#Test-EventLogSource -LogFile 'Application' -Source 'vs' -Verbose
#Test-EventLogSource -LogFile '*' -Source 'vss' -Verbose
#Test-EventLogSource -Source '*power*'
#Get-EventLogSource
#Get-EventLogSource -LogFile 'System' -Source '.NET*' -Verbose | Format-Table
#Get-EventLogSource -LogFile 'Application' -Source '.NET*' -Verbose | Format-Table
#Get-EventLogSource -LogFile 'dummy' -Source '.NET*' -Verbose | Format-Table
#Get-EventLogSource -LogFile '*' -Source '.NET*' -Verbose | Format-Table
#Get-EventLogSource -Source '.NET*' -Verbose | Format-Table
#Get-EventLogSource -LogFile 'Application' -Source 'vs' -Verbose | Format-Table
#Get-EventLogSource -LogFile '*' -Source 'vss' -Verbose | Format-Table
#Get-EventLogSource -Source '*power*'| Format-Table
使用 Get-WinEvent
Get-WinEvent -ListProvider * -ErrorAction SilentlyContinue |
Select-Object -Property Name -ExpandProperty LogLinks |
Select-Object -Property Name, LogName |
Sort-Object -Property Name