1

I have an requirement where I need to create an custom event log on different server. I have the command to create the event log on local machine but need it for remote server. The command to create the event log on local machine is

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

I want the same for the remote server.

Please paste the code if possible.

Thanks.

1
  • What does the ASP.Net tag doing here? Because your powershell command is just pure .Net calls so doing it directly in ASP.Net would be easier. So why use powershell anyway? Commented May 31, 2011 at 17:22

2 Answers 2

2

from your post on the 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')}}

Or Shay Levy's more elegant:

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
Sign up to request clarification or add additional context in comments.

Comments

0

There also is a CreateEventSource with 3 arguments of which the last is the machine name. So that would make it:

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

Note that method with 3 arguments is marked obsolete. With some more work you could use the variant that takes an EventSourceCreationData where you can also specify the machine name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.