1

我有一个要求,我需要在不同的服务器上创建自定义事件日志。我有在本地机器上创建事件日志的命令,但远程服务器需要它。在本地机器上创建事件日志的命令是

powershell -command "if([System.Diagnostics.EventLog]::SourceExists('MySource')){Write 'Eventlog already exists'}else{[System.Diagnostics.EventLog]::CreateEventSource('MySource','MyCustomLog')}"

我希望远程服务器也一样。

如果可能,请粘贴代码。

谢谢。

4

2 回答 2

2

来自你在 Posh Comm 上的帖子

Invoke-command -computername server,server -scriptblock {if([System.Diagnostics.EventLog]::SourceExists('MySource')){Write 'Eventlog already exists'}else{[System.Diagnostics.EventLog]::CreateEventSource('MySource','MyCustomLog')}}

或者 Shay Levy 的更优雅:

New-EventLog -LogName application -Source MySource,MyCustomLog -ComputerName PC1 -ErrorAction SilentlyContinue 
Write-EventLog -LogName application -Source MySource -EntryType information -Message test -EventId 1234 -Computer PC1
于 2011-06-02T06:54:06.287 回答
0

还有一个带有 3 个参数的CreateEventSource,其中最后一个是机器名称。这样就可以了:

if([System.Diagnostics.EventLog]::SourceExists('MySource', 'MyMachine'))
{
  Write 'Eventlog already exists'
}
else
{
  [System.Diagnostics.EventLog]::CreateEventSource('MySource','MyCustomLog', 'MyMachine')
}

请注意,带有 3 个参数的方法被标记为已过时。通过更多工作,您可以使用带有EventSourceCreationData的变体,您还可以在其中指定机器名称。

于 2011-05-31T17:37:33.553 回答