问题
We have a client with PowerShell 1.0. I know in PowerShell 2.0, I have the Write-EventLog
commandlet. However, this doesn't appear in PowerShell 1.0. I want my script to log events to the event log, mainly for debugging purposes.
How can I do this with PowerShell 1.0:? And, no I can't upgrade this computer to PowerShell 2.0. This is a customer production machine, and they don't want to touch the software on it.
回答1:
You can use the .NET EventLog class to do this. It is pretty simple to use e.g.:
$eventSource = "MyAppName"
if (![Diagnostics.EventLog]::SourceExists($eventSource))
{
[Diagnostics.EventLog]::CreateEventSource($eventSource, "Application")
}
[Diagnostics.EventLog]::WriteEntry($eventSource, "message", [Diagnostics.EventLogEntryType]::Error)
回答2:
You can probably just access the .NET EventLog class to write to the log.
来源:https://stackoverflow.com/questions/10337059/powershell-version-1-0-write-to-the-event-log