locked
how to write in a log file RRS feed

  • Question

  • Hi

    I try to parse folder and want to send a mail and log file name in a log file. This is what i make :

    $logfile=c:\temp\log.txt

    $checkfile = Get-Childitem $directory *.* -Recurse | where { !($_.LastWriteTime -ge (get-Date).AddHours(-10))} | Format-List directoryname | Out-String Write-Output $logfile

    I get no error and nothing in the log file are write.

    thanks



    Monday, May 29, 2017 10:49 AM

Answers

  • Use this. And don't forget to provide value for $directory
    $checkfile = Get-Childitem $directory -Recurse -File | 
    where { !($_.LastWriteTime -ge (get-Date).AddHours(-80))} | 
    Format-List directoryname,Name | out-file C:\log.txt

    • Proposed as answer by asharma5 Tuesday, May 30, 2017 2:23 AM
    • Marked as answer by Richard MuellerMVP Tuesday, June 6, 2017 3:34 PM
    Monday, May 29, 2017 11:40 AM

All replies

  • Use Out-File.

    help out-file -full


    \_(ツ)_/

    Monday, May 29, 2017 10:58 AM
  • Use this. And don't forget to provide value for $directory
    $checkfile = Get-Childitem $directory -Recurse -File | 
    where { !($_.LastWriteTime -ge (get-Date).AddHours(-80))} | 
    Format-List directoryname,Name | out-file C:\log.txt

    • Proposed as answer by asharma5 Tuesday, May 30, 2017 2:23 AM
    • Marked as answer by Richard MuellerMVP Tuesday, June 6, 2017 3:34 PM
    Monday, May 29, 2017 11:40 AM
  • Thanks to your answer.

    It's working if i use :
    Out-file  $logfile


    I have information into my logfile. But i don't receive mail anymore.

    i try to use outfile and out-string in a same cmdlet line. I don't now if that possible.
    ... Out-string | Out-file  $logfile

    Monday, May 29, 2017 12:13 PM
  • use send-mailmessage and in this Cmdlet you can attach you log file as well.
    Tuesday, May 30, 2017 2:25 AM