locked
What is the best way to Log information RRS feed

  • Question

  • I have tried numerous ways and forgive me I have only known PowerShell for a few days so a complete novice here. Below almost works as expected but not quite.

    #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"
    
    Try
    {
    Copy-item -Force -Recurse -Verbose $sourceDirectory -Destination $destinationDirectory -ErrorAction Stop
    "$(Get-Date)`tCopying files..." | Out-File -FilePath "C:\Users\Simon.Evans\Documents\Scripts\FileCopyLog.txt" -Append
    }
    Catch
    {
    "$(Get-Date)`tFailed to Copy files..." | Out-File -FilePath "C:\Users\Simon.Evans\Documents\Scripts\FileCopyLog.txt" -Append
    "$(Get-Date)`t$($Error[0].Exception.Message)" | Out-File -FilePath "C:\Users\Simon.Evans\Documents\Scripts\ErrorLog.txt" -Append
    }

    The output I am trying to achieve is something like below where it takes just 4 fields stored in the transcript and enters them into either a CSV or a TXT file as columns and appends each time the script is run.

    However if there is a better method I am not aware of or should be using then please feel free to advise. I simply just want to log the event that has occurred and handle the error also in a separate but similar file.

    Thursday, June 20, 2019 7:49 AM

All replies

  • Instead of having to write it out every time, you could just create a function and then call that function. That way if you want to make modifications to how you log you do it in the function. I use this which I wrote a long time ago, simple and does what I need.

    Function Logger
    {
      param
      (
        [Parameter(Position=0,Mandatory=$true)]
    	[string]$FilePath,
    	
    	[Parameter(Position=1,Mandatory=$true)]
    	[ValidateSet('Info','Error')]
    	[string]$LogType,
    	
    	[Parameter(Position=2,Mandatory=$true)]
    	[string]$Message
      )
      
      If (!(Test-Path $FilePath))
      {
        New-Item -ItemType File -Path $FilePath | Out-Null
      }
      
      Out-File -FilePath $FilePath -InputObject "[$(Get-Date)] - [$LogType]$Message" -Append
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Thursday, June 20, 2019 12:03 PM
  • Instead of having to write it out every time, you could just create a function and then call that function. That way if you want to make modifications to how you log you do it in the function. I use this which I wrote a long time ago, simple and does what I need.

    Function Logger
    {
      param
      (
        [Parameter(Position=0,Mandatory=$true)]
    	[string]$FilePath,
    	
    	[Parameter(Position=1,Mandatory=$true)]
    	[ValidateSet('Info','Error')]
    	[string]$LogType,
    	
    	[Parameter(Position=2,Mandatory=$true)]
    	[string]$Message
      )
      
      If (!(Test-Path $FilePath))
      {
        New-Item -ItemType File -Path $FilePath | Out-Null
      }
      
      Out-File -FilePath $FilePath -InputObject "[$(Get-Date)] - [$LogType]$Message" -Append
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Forgive me I would not know how to call a function in relation to my code etc.
    Thursday, June 20, 2019 12:15 PM
  • Function needs to be created before calling it then just call it as.

    Logger -FilePath C:\... -LogType Info - Message "What do I want to log"

    Logger -FilePath "C:\Users\Simon.Evans\Documents\Scripts\FileCopyLog.txt" -LogType Info -Message "Copying files..."
    Also you would log that you are "Copying files" first, then call the copy-item command, in my opinion


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    Thursday, June 20, 2019 12:29 PM