I've searched for every keyword I can find to see if there's a cmdlet in PS7 that is equivalent to New-EventLog from PS5?
Does something like this exist?
I've searched for every keyword I can find to see if there's a cmdlet in PS7 that is equivalent to New-EventLog from PS5?
Does something like this exist?
Continuing from my comment. For a simple example, and rough way of dealing with your use case on PSCore. You need to review the docs to see all the options for such a case.
Get-CimInstance -ClassName Cim_OperatingSystem
# Results
<#
SystemDirectory Organization BuildNumber RegisteredUser SerialNumber Version
--------------- ------------ ----------- -------------- ------------ -------
C:\Windows\system32 19041 00329-00000-00003-AA986 10.0.19041
#>
$PSVersionTable.PSVersion
# Results
<#
Major Minor Patch PreReleaseLabel BuildLabel
----- ----- ----- --------------- ----------
7 1 3
#>
# Get some event logs, just because
(Get-WinEvent -ListLog '*' -ErrorAction SilentlyContinue).LogName |
Select-Object -First 9
# Results
<#
Windows PowerShell
System
Security
Key Management Service
Internet Explorer
HardwareEvents
Application
Windows Networking Vpn Plugin Platform/OperationalVerbose
Windows Networking Vpn Plugin Platform/Operational
#>
# Create a new event log
New-Item -Path 'HKLM:\SYSTEM\ControlSet001\Services\Eventlog' -Name 'MyEventLog' –Force
# Results
<#
Hive: HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Eventlog
Name Property
---- --------
MyEventLog
#>
# Get some event logs, just because
(Get-WinEvent -ListLog '*' -ErrorAction SilentlyContinue).LogName |
Select-Object -First 9
# Results
<#
Windows PowerShell
System
Security
MyEventLog
Key Management Service
Internet Explorer
HardwareEvents
Application
Windows Networking Vpn Plugin Platform/OperationalVerbose
#>
# Write to it this way:
eventcreate /l MyEventLog /t Information /so TestWrite /id 1 /d "Test message"
# Results
<#
SUCCESS: An event of type 'Information' was created in the 'MyEventLog' log with 'TestWrite' as the source.
#>
Get-WinEvent -LogName 'MyEventLog'
# Results
<#
ProviderName: TestWrite
TimeCreated Id LevelDisplayName Message
----------- -- ---------------- -------
6/4/2021 6:12:30 PM 1 Information Test message
(Get-WinEvent -LogName 'MyEventLog').Count
1
#>
There is no direct equivalent since it is old Win32 API, however there is a New-WinEvent which might help you.
Example:
New-WinEvent -ProviderName Microsoft-Windows-PowerShell -Id 45090 -Payload @("Workflow", "Running")
If you are looking for a simple way to create an event source and log an event, you can use static methods on the [System.Diagnostics.EventLog]
class. Example code to create a new source for the built-in Application event log and write an event to the Application log:
if (-not [System.Diagnostics.EventLog]::SourceExists("MyApp")) {
[System.Diagnostics.EventLog]::CreateEventSource("MyApp", "Application")
}
[System.Diagnostics.EventLog]::WriteEntry("MyApp", "Message Body", "Information", 1)
You can use [System.Diagnostics.EventLog] | Get-Member -Static
to see a list of methods available on this class or see the Microsoft docs for more information on the class.
Creating an event source requires you to be running as Administrator, and all of the methods on EventLog
class will throw on non-windows platforms.
I just ran into the same problem and discovered that while
has been deprecated in PowerShell and replaced with Get-EventLog
it does still work in Windows PowerShell. I was able to run it in Windows PowerShell ISE. If you need more info on this I will be happy to provide what I can with as many examples as possible.Get-WinEvent