Logging function

by aussiebenfica at 2013-01-21 23:45:33

Hello all,

This is my first post, I am new to Powershell / scripting. I did a powershell course at the end of last year and am now trying to get some of it into practice.

I am creating a copy script to grab files from several locations on my network, i have some limited logging.

I would like to actually log all the commands with a time stamp and what was done… I have found some ideas on the internet, a coleague gave me the below funtion, but i just cant understand how to implement it into a copy script, see example below…

Function…

function write-log ([string] $severity, [string] $message)
{
$ts = get-date -format o
$sev = $severity.ToUpper().Substring(0,1)
$log = "C:\Temp\log.log"
$line = "$ts $sev $message"
if ($sev -eq "E")
{
write-error $line
}
else
{
write-output $line
}
$line >> $log
}


Example of my copy script…

# import script

# Data / Source / Destination / Values
$ImportFolder = "c:\Imports"
$log = "C:\Imports\Logs\Import-$(get-date -format yyyyddMM).log"
#$time = get-date -format HH:mm:ss


$SOURCE1 = "\networklocation\export$"
$SOURCE1_file_list = "file1.csv",
"file2.csv",
"file3.csv"

foreach ($file in $SOURCE1_file_list)
{
$str = "File: $file Result: "
try{
copy-item $SOURCE1$file $ImportFolder -ea stop
$str += "Success"
}
catch
{
$str += "Failed - $($.exception.message)"
}
$str | out-file $log -append
}

$SOURCE2 = "\networklocation2\export$"
$SOURCE2_file_list = "file4.csv",
"file5.csv",
"file6.csv"

foreach ($file in $SOURCE2_file_list)
{
$str = "File: $file Result: "
try{
copy-item $SOURCE2$file $ImportFolder -ea stop
$str += "Success"
}
catch
{
$str += "Failed - $($
.exception.message)"
}
$str | out-file $log -append
}

Apologies in advance if this is too long a post , or in the wrong section.

Any help would be greatly appreciated, Many Thanks…

AB
by DonJ at 2013-01-22 05:56:21
Welcome.

As a note, you can sue the CODE or POWERSHELL buttons in the toolbar to surround your code and provide better formatting.

The logging function would go at the top of your script. You simply call it whenever you want to write something to the log. For example, I might do so immediately inside your Try{} construct to log the file I was about to copy, and then inside the Catch{} construct to log any errors.


Write-Log "Severe" "$file failed"


It looks like your script is already creating a log, though, including errors. I’m not sure what you’re looking for in addition to that. There’s no PowerShell capability for simply logging every command that’s run; you have to write the code to create your log and what goes into it.
by ArtB0514 at 2013-01-22 07:35:35
Actually, you can log the activity of a script using the Start-Transcript command. It records both the commands that are executed and any output sent to the host from the point where it is executed.
by DonJ at 2013-01-22 08:00:22
That’s only true if you’re running in the Console host. Keep in mind that other hosts, including the ISE, may not support Start-Transcript. I just want to point that out, since someone working on a script may well be working in the ISE.
by aussiebenfica at 2013-02-04 01:09:10
Hi Guys, Thanks for the replies…

I ended up doing the below , this allowed me to get exactly what i need.


foreach ($file in $SOURCE1_file_list)
{
$str = Get-Date -format HH:mm:ss
$str += " -> $file Result: "

try{
copy-item $SOURCE1$file $ImportFolder -ea stop
$str += " Success"
}
catch
{
$str += "Failed - $($_.exception.message)"
}
$str | Out-File $log -append -Encoding ASCII

This allowed me to have a time stamp on each line of my log.

Cheers

AB