1

我有以下代码在 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-Jobcmdlet 似乎都不处理 PowerShell 转录(日志记录)。Write-Output因此,除了Out-File捕获作业的输出之外,我还在使用.

我遇到的问题是,在Write-Output脚本的一部分中,我想在日志文件名中包含备份的名称。换句话说,我最终得到了一个名为“corebackup 2015-08-24.log”的文件,而不是“corebackup backupname 2015-08-24.log”。

问题是我如何传递$_.NameOut-File. 现在写了一个日志,但没有作业名称。

4

3 回答 3

2

尝试这个:

Start-RSJob -ArgumentList @($Name) -Throttle 2 -ScriptBlock {
    $Name = $args[0]
    ...
}
于 2015-08-25T04:29:19.263 回答
2

然后将其包装在另一个For-EachObject块中并分配$_.Name给管道外部可访问的变量。我查看了Start-RsJob文档,它应该可以工作:

if ($xmlfile.Configuration.DBAndFilesBackup.Item('MSSQL').HasChildNodes) {

    $xmlfile.Configuration.DBAndFilesBackup.MSSQL.Backup | ForEach-Object {
        $BackupName = $_.Name
        $_ | Start-RSJob -Name $BackupName -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 $BackupName $DateStamp.log"
    }
}

PS 我正在使用这个模块进行日志记录,它捕获来自 PS 控制台的任何输出:增强的脚本日志记录模块

自动将 PowerShell 控制台输出复制到日志文件(来自输出、错误、警告、详细和调试流),同时仍在控制台上显示输出。日志文件输出前面带有日期/时间和来自哪个流的指示符。

于 2015-08-25T01:21:14.893 回答
0

I wasn't able to get any of the above working with throttling. In other words I could get output but then throttling broke, or i could make throttling work but not get output.

The solution I ended up using was to add a bunch of "Write-Output | Out-file $log -Append" to my script every time I want to output something.

Kinda ugly hack - but by using that inside of the scriptblock I can capture the output.

Thanks to everyone for their help and advise.

于 2015-08-28T11:29:47.337 回答