1

i need you help again :D

I have created a function to put the error logs in a file who take the name of my script (i call multiples scripts so it's very helpful), here is my function :

function ExportLog{
$path = Get-Location

$LogFile = [io.path]::ChangeExtension($MyInvocation.ScriptName,"log")
Write-Host $LogFile
$timestamps = Get-Date
$string_err = $_ | Out-String
$ExportError = "[" + $timestamps.DateTime + "]`n" + $string_err + "`n"| Out-File -FilePath $LogFile -Append

Read-Host “Appuyez sur ENTRER pour quitter...”}

This works fine but the log file created or edited is in the path of my script.

My question is how can i add \log\ in the path who is in my variable $LogFile ?

I tried to use Join-Path, but it just add path like this : C:\import\Modif_CSV.log\Logs ... I wan't to add the Logs folder before the name of the file ^^

Ty for help :)

2
  • 3
    (Join-Path (Join-Path $PSScriptRoot 'log') $LogFile) Commented Aug 20, 2021 at 11:18
  • This don't work, this give me : c:\import\logs\modif_csv.log\ , the name of the file is a folder name ... I found the solution, ty for your help :) Commented Aug 21, 2021 at 22:38

2 Answers 2

1

You can split the current script filename from the full path and change the extension with:

$LogFileName = [IO.Path]::ChangeExtension((Split-Path $PSCommandPath -Leaf), 'log')

Next combine the current script path with the subfolder 'log' and with the new filename

$LogFullName = [IO.Path]::Combine($PSScriptRoot, 'log', $LogFileName)
Sign up to request clarification or add additional context in comments.

Comments

1

Theo's helpful answer shows a .NET API-based solution that works in both Windows PowerShell and PowerShell (Core) 7+.

Here's a PowerShell (Core) 7+ solution that showcases new features (relative to Windows PowerShell):

$dir, $name = $PSCommandPath -split '\\', -2
Join-Path $dir log ((Split-Path -LeafBase $name) + '.log')
  • -split '\\', -2 splits the path into two strings by \: the last \-separated token, preceded by everything before the last \, thereby effectively splitting a file path into its directory path and file name. That is, -split now accepts a negative number as the count of tokens to return, with -$n meaning: return $n-1 tokens from the right of the input string (albeit in left-to-right order), and save any remaining part of the string in the return array's first element; e.g., 'a/b/c/d' -split '/', -3 yields 'a/b', 'c', 'd'

  • Split-Path -LeafBase returns a file path's file-name base, i.e. the file name without its extension.

  • Join-Path now accepts an open-ended number of child paths to join to the parent path; e.g., Join C:\ dir subdir now works to create C:\dir\subdir, whereas in Windows PowerShell you had to nest calls: Join-Path (Join-Path C:\ dir) subdir

Note: It would be handy if Split-Path supported returning all components of a given path in a single operation; GitHub issue #6606 proposes an -All switch that returns an object whose properties reflect all the constituent parts of the path, which would enable the following simplified solution:

# WISHFUL THINKING, as of PowerShell 7.2
$pathInfo = Split-Path -All $PSCommandPath
Join-Path $pathInfo.Parent log ($pathInfo.LeafBase + '.log')

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.