我有以下代码在 PowerShell 中解析 XML 文件,然后遍历配置文件中的条目(它们是备份作业)并执行备份(通过调用函数)。
if ($xmlfile.configuration.DBAndFilesBackup.item("MSSQL").haschildnodes) {
$xmlfile.configuration.DBAndFilesBackup.MSSQL.backup |
Start-RSJob -Name {$_.Name} -Throttle 2 -ScriptBlock {
# Begin the Backup
BeginBackup $_.Name $log
# Backup the mssql database
MSSQLBackup $_.Name $_.DBPath $Using:backupdir $Using:backuptempdir
# Backup the files
FilesBackup $_.Name $_.FolderName $_.FilesPath $Using:backupdir $Using:backuptempdir
# End the Backup
EndBackup $_.FolderName $_.Name $log $Using:emailTo $Using:backupdir $Using:backuptempdir
} -FunctionsToLoad BeginBackup,MSSQLBackup,FilesBackup,EndBackup,Mailer,PrintHeader |
Wait-RSJob |
Receive-RSJob |
Out-file "$ScriptDir\logs\corebackup\$ScriptName $($xmlfile.configuration.DBAndFilesBackup.MSSQL.backup.Name) $DateStamp.log"
}
Start-RSJob
是一个自定义 PowerShell 模块,类似于Start-Job
处理启动并行作业的 cmdlet。
RSJob 和本机 PowerShell Start-Job
cmdlet 似乎都不处理 PowerShell 转录(日志记录)。Write-Output
因此,除了Out-File
捕获作业的输出之外,我还在使用.
我遇到的问题是,在Write-Output
脚本的一部分中,我想在日志文件名中包含备份的名称。换句话说,我最终得到了一个名为“corebackup 2015-08-24.log”的文件,而不是“corebackup backupname 2015-08-24.log”。
问题是我如何传递$_.Name
给Out-File
. 现在写了一个日志,但没有作业名称。