2

I am needing to create a logfile for every action that is performed by this script.

I have attempted to use redirection(>>) however, cannot get the output to actually write to the file.

while($true){

$FromMC = Get-ChildItem -Path "\\x\From MC" -Filter *.mpg
Write-Host "Checking '\\x\From MC' for files" -ForegroundColor Cyan
Write-Host $FromMC.count"Files Found" -ForegroundColor Cyan
Write-Host ""
ForEach($file in $FromMC){

    try{
        Move-Item -Filter 7ST* -Path $file.Fullname -Destination "\\x\programs\7TH STREET THEATER" -Verbose -Force -ErrorAction Stop
    }
    catch{...}
Write-Host "Pausing for"$ts.Minutes"minutes..." -ForegroundColor Cyan
Write-Host "Ctrl+C to Stop"
Write-Host ""
Start-Sleep -Seconds $ts.TotalSeconds
}

I expect the output to be exactly as "-verbose" outputs into the shell. Types of output: Write-Host, Verbose, Write-Warning

I feel the solution is extremely simple, and I am just overlooking it.

  • Write-Host is not redirected, it is used for interactions with the user on the console. Use ```Write-Output``` instead (or just don't assign the return values). And error output can be redirected through ```2>>```. – Christoph Jun 08 '19 at 20:08
  • take a look at powershell auditing. that gets almost everything ... and is set at the OS level, not in any particular script. it's a group of GPO settings that can be easily controlled. – Lee_Dailey Jun 09 '19 at 02:43

1 Answers1

4

To log everything that would normally be written to the console, like -verbose you can use Start-Transcript and when you are finished Stop-Transcript.

Example:

Start-Transcript -Path "C:\logs.txt"

#run code you want to capture

Stop-Transcript
jrider
  • 1,571
  • 1
  • 10
  • 26