1

我有一個需求,我需要在其他服務器上創建自定義事件日誌。我有命令在本地機器上創建事件日誌,但需要它用於遠程服務器。創建本地計算機上的事件日誌的命令是使用PowerShell在遠程服務器上創建自定義事件日誌

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

我想遠程服務器相同。

如果可能,請粘貼該代碼。

謝謝。

+0

什麼是ASP.Net標籤在這裏做什麼?因爲你的powershell命令只是純粹的.Net調用,所以直接在ASP.Net中執行會更容易。那麼爲什麼要使用PowerShell呢? – 2011-05-31 17:22:09

回答

0

還有一個CreateEventSource帶有3個參數,其中最後一個是機器名稱。所以這將使它:

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

請注意,具有3個參數的方法被標記爲過時。通過一些更多的工作,您可以使用需要EventSourceCreationData的變體,您也可以在其中指定機器名稱。從您的文章在辣妹通訊

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

還是吉文利維的更優雅

1

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 
相關問題