OpsaC - Operating as PowerShell code
published: May 24, 2019 author: Tinu tags: PowerShell categories: PowerShell-Eventlog
We known two Eventlogs, Windows (classic) Logs and Application and Services Logs.
The cmdlets that contain the EventLog noun (the EventLog cmdlets) works only on classic event logs such as Application, System, Setup, and Security.
$params = @{
LogName = 'Application'
Newest = 10
}
Get-EventLog @params
To get events from logs that use the Windows Event Log technology in Windows Vista and later versions of the Windows operating system, use the Get-WinEvent cmdlet.
$params = @{
LogName = 'Microsoft-Windows-PowerShell/Operational'
MaxEvents = 10
}
Get-WinEvent @params | Select-Object TimeCreated,Id,LogName,ProviderName,Message
$params = @{
ProviderName = 'Microsoft-Windows-WindowsUpdateClient'
MaxEvents = 10
}
Get-WinEvent @params | Select-Object TimeCreated,Id,LogName,ProviderName,Message
$params = @{
LogName = 'Microsoft-Windows-PowerShell/Operational'
Id = 4104, 4103, 4688, 7045
}
Get-WinEvent -FilterHashtable $params | Select-Object TimeCreated,Id,ProviderName,Message
$params = @{
LogName = 'Microsoft-Windows-PowerShell/Operational'
StartTime = (Get-Date).AddDays(-1)
EndTime = Get-Date
}
Get-WinEvent -FilterHashtable $params | Select-Object TimeCreated,Id,ProviderName,Message
You can write your own ProviderName (EventsourceName). Before that, test if the EventsourceName already exists.
function Test-MWAEventsourceName{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[String] $EventsourceName
)
$ret = $false
try{
$ret = [System.Diagnostics.EventLog]::SourceExists($EventsourceName)
}
catch{
$Error.Clear()
}
return $ret
}
Test-MWAEventsourceName -EventsourceName 'MWA-PowerShell-Automation'
# run as administrator
$eventlogname = 'Application'
$providername = 'MWA-PowerShell-Automation'
New-EventLog –LogName $eventlogname –Source $providername
$eventlogname = 'Application'
$providername = 'MWA-PowerShell-Automation'
Write-EventLog –LogName $eventlogname –Source $providername –EntryType Information –EventID 1024 –Message 'Test new ProviderName'
Run as administrator
$providername = 'MWA-PowerShell-Automation'
[System.Diagnostics.EventLog]::DeleteEventSource($providername)
Create a restart history
$LastBootupTime = (Get-CimInstance Win32_OperatingSystem).LastBootupTime
[DateTime]$StartTime = $LastBootupTime.AddHours(-1)
[DateTime]$EndTime = $LastBootupTime.AddHours(1)
Write-Host "LastBooupTime $(Get-Date $LastBootupTime -f 'yyyy-MM-dd HH:mm:ss')"
EventLog Service messages:
Get-WinEvent -FilterHashtable @{
Logname = 'System'
StartTime = $StartTime
EndTime = $EndTime
} | Where Id -match '600\d' | Select TimeCreated,Id,Message,ProviderName | Format-Table
Application.exe has initiated the restart of computer:
$RestartyApplication = Get-WinEvent -FilterHashtable @{
Logname = 'System'
StartTime = $StartTime
EndTime = $EndTime
} | Where Id -match '1074' | Select TimeCreated,Id,Message,ProviderName
foreach($item in $RestartyApplication){
if($_.Message -match 'CCM\\TSManager\.exe'){
$message = "Restarted by System Center Configuration Manager"
}elseif($_.Message -match 'VMware Tools\\vmtoolsd.exe'){
$message = "Restarted by VMware Tools (API)"
}else{
$message = $_.Message
}
[PSCustomObject]@{
TimeCreated = $item.TimeCreated
EventId = $item.Id
Message = $message
ProviderName = $item.ProviderName
}
}
How to Use PowerShell to Write to Event Logs on devblogs.microsoft.com