简体   繁体   English

如何在PowerShell中将-Verbose的输出附加到日志文件?

[英]How to append the output of -Verbose to the log file in PowerShell?

I have a PowerShell script which copies some file from one folder to another folder and I also have code to create a log file and add the steps that are performed by the script to the log file. 我有一个PowerShell脚本,该脚本可以将某些文件从一个文件夹复制到另一个文件夹,并且我还具有创建日志文件并将该脚本执行的步骤添加到日志文件中的代码。

Below is the code - 下面是代码-

[CmdletBinding()]
Param (
    [string]$destRoot = "C:\Program Files\Demo",
    [string]$BackUpFolder = "C:\Program Files\Backup"  
)

$scriptPath = $PSScriptRoot
$logFilePath = Join-path $scriptPath "FileDeployment.log"

# If log file exists, then clear its contents 
if (Test-Path $logFilePath) { clear-content -Path $logFilePath } 

# It displays the date and time of execution of powershell script in log file.
$log = "Date Of Testing: {0} " -f (Get-Date)
$logString = "Process Started." 
add-content -Path $logFilePath -Value $log -Force 
add-content -Path $logFilePath -Value $logString -Force 
add-content -Path $logFilePath -Value "`n" -Force

# Code to take back of programs folder
$ProgramFilesBackUp = "$BackUpFolder\BackupPrograms"
$ProgramsFiles = "$destRoot\Programs\*"

Copy-Item $ProgramsFiles $ProgramFilesBackUp -Recurse -Force -Verbose 

The -Verbose displays all the files names along with its path getting copied from Programs folder to BackupPrograms folder but it fails to add that output to my log files which is shown on command shell. -Verbose显示所有文件名及其路径,该路径已从Programs文件夹复制到BackupPrograms文件夹,但无法将输出添加到命令外壳上显示的我的日志文件中。 I tried something like this but it overwrites the previous output present in the log file. 我尝试过类似的操作,但它会覆盖日志文件中存在的先前输出。

Copy-Item $ProgramsFiles $ProgramFilesBackUp -Recurse -Force -Verbose  *> $logFilePath

I also tried the below code as well by copying the output generated by verbose to a new file and then try to append the content of the file to my log file, here it copied the content but not in the proper format. 我还尝试了以下代码,方法是将详细信息生成的输出复制到一个新文件,然后尝试将该文件的内容附加到我的日志文件中,这里它复制了内容,但格式不正确。

$NewText= "$PSScriptRoot\s.txt"
Copy-Item $ProgramsFiles $ProgramFilesBackUp -Recurse -Force -Verbose  *> $NewText
Get-Content $NewText | Out-File $logFilePath -Verbose -Append 

Below is the format the way in which it copied the content. 以下是复制内容的格式。

在此处输入图片说明

You should read and thoroughly understand Get-Help about_Redirection , which identifies all of the various output streams, and discusses how to redirect them. 您应该阅读并彻底理解Get-Help about_Redirection ,它标识了所有各种输出流,并讨论了如何重定向它们。 In this case, since you've indicated interest in the -Verbose stream, you want to redirect stream #4 to the standard output, which is done by appending 4>&1 to the command, before actually doing the redirect > foolog.txt or >> foolog.txt . 在这种情况下,由于您已表示对-Verbose流感兴趣,因此您想要将流#4重定向到标准输出,这是通过在命令前附加4>&1来完成的,然后实际执行redirect > foolog.txt>> foolog.txt

NOTE: This may not work for Powershell 2.0; 注意:这可能不适用于Powershell 2.0。 it is documented for 3.0 and later. 它记录在3.0及更高版本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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