Simon
Simon

Reputation: 401

How do I create event logging and error logging

New to Powershell, no experience, below is a simple script to move a file from one location to another, I would like to log this action and what I've done somewhere (say another file Logging.txt) but would also like to error log if the script did not run for any reason.

Searched StackOverflow but couldn't find a suitable answer

##########################################################################################################################################
 #Copies a single file from from one location to another location
 #Copy-item never delete extra files or folders in destination, but with -force it will overwrite if file already exists

#Copies the contents of COde_mapping.txt in the LNAT folder in a local location onto a test location on the I drive

[string]$sourceDirectory = "C:\Users\Simon.Evans\Documents\Source Data\LNAT\Code_Mapping.txt"
 [string]$destinationDirectory = "I:\Dev\BI\Projects\Powershell\Test Area\Source Data\LNAT\Code_Mapping.txt"
 Copy-item -Force -Recurse -Verbose $sourceDirectory -Destination $destinationDirectory

all fields to by output/appended into a single CSV/TXT for each instance the script has run

OR

Is it possible to append specific outputs created in Start-Transcript into a CSV file, an example would be I want the following fields to be appended into a csv or txt file.

Start time:

Username:

Machine:

VERBOSE:

These are all present in the Start-Transcript - Stop-Transcript, but instead of having multiple txt files each time the script has run I would like the result to be appended into just 1 txt if this at all possible.

##########################################################################################################################################
#Copies a single file from from one location to another location
#Copy-item never delete extra files or folders in destination, but with  -force it will overwrite if file already exists

#Copies the contents of COde_mapping.txt in the LNAT folder in a local location onto a test location on the I drive

$datevar = get-date -Format ("yyyyMMdd") 
$timevar = Get-date -Format ("hhmmss")
$varfullpath = "C:\Users\Simon.Evans\Documents\ReferenceData_logfile_$datevar_$timevar.txt"

Start-Transcript -Path $varfullpath

Write-Output $datevar
Write-Output $timevar
write-output $varfullpath

[string]$sourceDirectory  = "C:\Users\Simon.Evans\Documents\Source Data\LNAT\Code_Mapping.txt"
[string]$destinationDirectory = "I:\Dev\BI\Projects\Powershell\Test Area\Source Data\LNAT\Code_Mapping.txt"
Copy-item -Force -Recurse -Verbose $sourceDirectory -Destination $destinationDirectory

Stop-Transcript 

************* UPDATE 21/06/2019 9:22am (UK) *************

This is what I now have and it works, however now I want to try and add a TRY CATCH and diplay the Error transaction if it fails and contunie to do as below if successful, again is this possible and how would I go about doing this please

$varfullpath = "C:\Users\Simon.Evans\Documents\ReferenceData__logfile.txt" 
Start-Transcript -Path $varfullpath -Append                                                            
write-output $varfullpath
[string]$sourceDirectory  = "C:\Users\Simon.Evans\Documents\Source Data\LNAT\Code_Mapping.txt"
[string]$destinationDirectory = "I:\Dev\BI\Projects\Powershell\Test Area\Source Data\LNAT\Code_Mapping.txt"
Copy-item -Force  -Verbose $sourceDirectory -Destination $destinationDirectory
Stop-Transcript 

Upvotes: 2

Views: 75

Answers (1)

svanzundert
svanzundert

Reputation: 63

Use the -append parameter in the start-transcript commandlet to append the logfile instead of creating a new one.

Upvotes: 1

Related Questions