3

I'm new to PowerShell and am writing my first application. The application will check the state of a Windows service and (depending on a number of factors) can do a number of things, such as stop it, start it or restart it. I need to produce a log showing what it's done. I'm wanting to use modules so my code will be reusable and also flexible (for example, if I schedule it to run automatically, the log will need to be sent by e-mail or written to a text file, if I run it manually, I will want it outputting with something like Write-Host).

My question is, how do I create the log? In Java for example, I would use a log class with public void addToLog(String log) and public String getLog() methods which just deal with strings and leave it up to e-mail/display it etc.

3 Answers 3

3

I have a quite complex set of scripts for installing our platform (relying on BizTalk, SQL Server, Enterprise Single Sign-on, IIS, Enterprise Library and a few other things). These scripts start and stop services, BizTalk orchestrations, create or update databases and so on. I tried several things for logging and, finally, I picked log4net for its ease of use and flexibility. Using it from PowerShell is a breeze.

Sign up to request clarification or add additional context in comments.

Comments

1

Refer to a similar question which I had asked on SO few days ago. Might help. I am using the same Logging Module referred there in the ANSWER - Powershell: Debug in Production / Good Exception Handling

Comments

0

If you need a log for people who are in charge of the production you can add your own application log using dedicated Cmdlets. A the moment I create and use one PowerShell log for all my scripts to publish details of the execution (information) and errors (in coordination with good exception handling) for people who are in charge of the production. You can dedicate a log for one script (as it exists a log for DNS etc.)

Here is an example :

# List of logs
Get-EventLog -list

# Creating your own log
New-EventLog -LogName "SlxScripting" -Source "MaSource"

# List of logs
Get-EventLog -list

# Writting in your own log
Write-EventLog -LogName "SlxScripting" -EventId 12 `
               -Message "Mon Message" 
               -Source "MaSource" -EntryType Warning

# Reading in your own log
Get-EventLog -LogName "SlxScripting"  

# Suppressing your log
Remove-EventLog -LogName "SlxScripting"

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.