gpt4 book ai didi

variables - 在脚本 block 中引用变量

转载 作者:行者123 更新时间:2023-12-03 00:49:43 24 4
gpt4 key购买 nike

我有以下代码在 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是类似于 Start-Job 的自定义 PowerShell 模块处理启动并行作业的 cmdlet。

RSJob 和 native PowerShell Start-Job cmdlet 似乎不处理 PowerShell 转录(日志记录)。因此,我正在使用 Write-Output , 除了 Out-File捕获作业的输出。

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

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

最佳答案

试试这个:

Start-RSJob -ArgumentList @($Name) -Throttle 2 -ScriptBlock {
$Name = $args[0]
...
}

关于variables - 在脚本 block 中引用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32193889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com