简体   繁体   中英

How can I log everything that is written in powershell window?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM