Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在powershell中创建日志文件_Powershell_Logging_Powershell 2.0 - Fatal编程技术网

在powershell中创建日志文件

在powershell中创建日志文件,powershell,logging,powershell-2.0,Powershell,Logging,Powershell 2.0,我有以下powershell代码,其中, folder1中(原始)文件的备份在folder2中进行,folder1中的文件用folder3文件更新 修补程序的概念!! 任何帮助或其他方法都将不胜感激 您可以使用-PassThru开关参数让复制项返回刚复制的新项,然后立即在ForEach对象脚本块内进行日志记录: | ForEach-Object { $BackupFiles = Copy-Item $_.FullName -Destination $backup -Recurse -Co

我有以下powershell代码,其中, folder1中(原始)文件的备份在folder2中进行,folder1中的文件用folder3文件更新

修补程序的概念!!
任何帮助或其他方法都将不胜感激

您可以使用
-PassThru
开关参数让
复制项
返回刚复制的新项,然后立即在
ForEach对象
脚本块内进行日志记录:

| ForEach-Object {
    $BackupFiles = Copy-Item $_.FullName -Destination $backup -Recurse -Container -PassThru
    $BackupFiles |ForEach-Object { 
        $LogMessage = "[{0:dd-MM-yyyy hh:mm:ss.fff}]File copied: {1}" -f $(Get-Date),$_.FullName 
        $LogMessage | Out-File ".\backups.log" -Append 
    }
}

好的@Mathias。。但是我还需要打印日期和时间。我应该如何管道化它们?在循环中使用
Get Date
检索当前日期和时间,然后使用
-f
操作符格式化日志消息。最新答案
cls
$path = "C:\Log\Nlog.log"   
$numberLines = 25

For ($i=0;$i -le $numberLines;$i++)
{
 $SampleString = "Added sample {0} at {1}" -f $i,(Get-Date).ToString("h:m:s")
 add-content -Path $path -Value $SampleString -Force  
}
| ForEach-Object {
    $BackupFiles = Copy-Item $_.FullName -Destination $backup -Recurse -Container -PassThru
    $BackupFiles |ForEach-Object { 
        $LogMessage = "[{0:dd-MM-yyyy hh:mm:ss.fff}]File copied: {1}" -f $(Get-Date),$_.FullName 
        $LogMessage | Out-File ".\backups.log" -Append 
    }
}