在 powershell 中创建日志文件
creating a log file in powershell
我有以下 powershell 代码,其中,
folder1 中的(原始)文件备份在 folder2 中,folder1 中的文件用 folder3 文件更新。
修补程序的概念!!
cls
[xml] $XML = Get-content -Path <path of xml>
$main = $XML.File[0].Path.key
$hotfix = $XML.File[1].Path.key
$backup = $XML.File[2].Path.key
Get-ChildItem -Path $main | Where-Object {
Test-Path (Join-Path $hotfix $_.Name)
} | ForEach-Object {
Copy-Item $_.FullName -Destination $backup -Recurse -Container
}
write-host "`nBack up done !"
Get-ChildItem -Path $hotfix | ForEach-Object {Copy-Item $_.FullName -Destination $main -force}
write-host "`nFiles replaced !"
现在,由于文件备份在文件夹 2 中,我需要创建一个日志文件,其中包含 - 备份文件的名称、日期和时间、备份位置拍摄
谁能帮我解决这个问题?
我做了以下代码,但没有用,因为我无法同步两者。
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
}
感谢任何帮助或不同的方法!!
您可以使用 -PassThru
开关参数让 Copy-Item
return 刚刚复制的新项目 - 然后立即在 ForEach-Object
内部进行日志记录脚本块:
| 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
}
}
我有以下 powershell 代码,其中, folder1 中的(原始)文件备份在 folder2 中,folder1 中的文件用 folder3 文件更新。
修补程序的概念!!
cls [xml] $XML = Get-content -Path <path of xml> $main = $XML.File[0].Path.key $hotfix = $XML.File[1].Path.key $backup = $XML.File[2].Path.key Get-ChildItem -Path $main | Where-Object { Test-Path (Join-Path $hotfix $_.Name) } | ForEach-Object { Copy-Item $_.FullName -Destination $backup -Recurse -Container } write-host "`nBack up done !" Get-ChildItem -Path $hotfix | ForEach-Object {Copy-Item $_.FullName -Destination $main -force} write-host "`nFiles replaced !"
现在,由于文件备份在文件夹 2 中,我需要创建一个日志文件,其中包含 - 备份文件的名称、日期和时间、备份位置拍摄
谁能帮我解决这个问题? 我做了以下代码,但没有用,因为我无法同步两者。
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 }
感谢任何帮助或不同的方法!!
您可以使用 -PassThru
开关参数让 Copy-Item
return 刚刚复制的新项目 - 然后立即在 ForEach-Object
内部进行日志记录脚本块:
| 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
}
}