Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - how to get powershell to write to event log or write a log file?

Reply
 
Old 09-24-2009   #1 (permalink)
scale


 
 

how to get powershell to write to event log or write a log file?

I have a script using

remove-item
and
copy-item

I would like to have a TXT file outputed to say that the scrip ran succesfull.

Something like
remove-item C:\temp -vervbose
Echo the file removal was successful | C:\logs\removal.log
copy-item C:\temp -destination D:\temp
echo the file copy was successful | C:\logs\copy.log




All i am trying to do is verify that the date stamps on the copied files
have changed or that the files are deleted and then are written to the new
location and that succeeded.

If i could do this with an eventlog event that would be great as well and
probably preferred.

any help?


My System SpecsSystem Spec
Old 09-25-2009   #2 (permalink)
Chris Dent


 
 

Re: how to get powershell to write to event log or write a log file?


Would you want to capture any error returned from the two commands? e.g.

$LogFile = "RemoveItem.log"

Remove-Item $SomeItem -ErrorVariable "RemoveErr"
If ($RemoveErr) {
"Error removing $SomeItem" >> $LogFile
$RemoveErr >> $LogFile
} else {
"Removed $SomeItem" >> $LogFile
}

You can write error messages to the Event Log with:

[Diagnostics.EventLog]::WriteEntry( ... )

The simplest form of that is:

[Diagnostics.EventLog]::WriteEntry("Source", "Message")

Which adds an entry to the Application Event Log.

See:

http://msdn.microsoft.com/en-us/libr...riteentry.aspx

Chris
My System SpecsSystem Spec
Old 09-30-2009   #3 (permalink)
Alex


 
 

Re: how to get powershell to write to event log or write a log file?

Another full-blown solution could be using log4net. Once it is
installed, add this to your profile:

[Reflection.Assembly]::LoadFrom("log4net.dll") # you may want to
adjust the path here
[log4net.Config.XmlConfigurator]::Configure( `
(new-object System.IO.FileInfo -ArgumentList `
(ls "log4net_config.xml").FullName `
) )
$global:Log = [log4net.LogManager]::GetLogger("Powershell.PSTest")

After that, you can use something like $Log.Warn("hello, log")

Read log4net documentation on how to configure the format and location
of log files

Cheers

On Sep 25, 9:50*am, Chris Dent <ch...@newsgroup> wrote:
Quote:

> Would you want to capture any error returned from the two commands? e.g.
>
> $LogFile = "RemoveItem.log"
>
> Remove-Item $SomeItem -ErrorVariable "RemoveErr"
> If ($RemoveErr) {
> * *"Error removing $SomeItem" >> $LogFile
> * *$RemoveErr >> $LogFile} else {
>
> * *"Removed $SomeItem" >> $LogFile
>
> }
>
> You can write error messages to the Event Log with:
>
> [Diagnostics.EventLog]::WriteEntry( ... )
>
> The simplest form of that is:
>
> [Diagnostics.EventLog]::WriteEntry("Source", "Message")
>
> Which adds an entry to the Application Event Log.
>
> See:
>
> http://msdn.microsoft.com/en-us/libr...ics.eventlog.w...
>
> Chris
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Write-Error doesn't write anyting PowerShell
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? .NET General
PowerShell Event Logs (read and write) (Local and Remote) PowerShell
The disk is write-protected. Remove the write-protection or... Vista General
newline in Write-Debug or Write-Verbose PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46