Logging to a file with today's date and time

Advertisement

IJH
Joined:
Posts:
4
Location:
UK

Logging to a file with today's date and time

Hi,

I'm trying to to write output to a logfile with today's date and time in the name of the file, so i'd have a new file for each day's transfer activities.

Logging works fine if i don't insert a date/time variable into the filename like so:

   $session = New-Object WinSCP.Session
   $session.SessionLogPath = "X:\Logs\TransferLog.log"
 

But if i try and enter the date/time variable - it tries but then fails:

$DateTime = Get-Date -format yyyy/MM/dd
$session = New-Object WinSCP.Session
$session.SessionLogPath = "X:\Logs\TransferLog-$DateTime.log"
Even though it seems to accept the variable in the filename it can't find it or create it - but as I've mentioned, works fine without the variable so the same file location.
Can't open log file 'X:\Logs\TransferLog-2018/09/25.log'.
System Error.  Code: 2.
The system cannot find the file specified"
i know its not permissions since it can write to the same location without the variable inserted.

Any ideas?

Reply with quote

Advertisement

IJH
Joined:
Posts:
4
Location:
UK

Ok so it was just the format of the string in the date - it doesn't like the "/" characters in the filename.

Changed it to
$DateTime = Get-Date -format dd-MM-yyyy
and then
$session.SessionLogPath = "X:\Logs\Daily Download $DateTime.log"
works just fine

Reply with quote

Advertisement

You can post new topics in this forum