我正在尝试同时恢复两个sql服务器上的数据库。这就是为什么我需要让restore-sqldatabase作为一个作业在后台运行,这样我就可以同时运行第二个还原作业。
receive-job显示4个错误,将变量从参数列表传递到脚本块是很好的,但是将变量传递到脚本块并将变量传递给cannot index into a null array -object命令“接受自己的参数列表”似乎是个问题。
类似的问题与我想要实现的目标并不完全相同。任何帮助都是非常感谢的。
Start-Job -InitializationScript {
#load assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
#Need SmoExtended for backup
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null
} -Name JobP -ScriptBlock {
$serverConn = new-object ("Microsoft.SqlServer.Management.Smo.Server") $($args[0])
$serverConn.ConnectionContext.StatementTimeout = 0
$RelocateData = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("$($args[1])", "$($arg[2])")
$RelocateLog = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("$($args[3])", "$($arg[4])")
Restore-SqlDatabase -InputObject $serverConn -Database $($args[5]) -BackupFile "$($args[6])" -RelocateFile @($RelocateData,$RelocateLog) -ReplaceDatabase
} -ArgumentList $ServerPrimary,$database_logical_name,$database_path,$log_logical_name,$log_path,$database,$backupfile_full_path发布于 2014-11-16 19:27:20
我要感谢stackoverflow和所有之前通过参数($p1,$p2)将变量传递给脚本块的线程
我设法通过反复试验解决了这个问题,现在脚本正在工作。以下是解决方案:
Start-Job -InitializationScript {
#load assemblies [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
#Need SmoExtended for backup
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null
} -Name JobP -ScriptBlock {
#Define the parameters to be used inside the script block, $p0 will be the first arguments in the "-ArgumentList", $p1 will be the second and so on.
param($p0, $p1, $p2, $p3, $p4, $p5, $p6)
$serverConn = new-object ("Microsoft.SqlServer.Management.Smo.Server") $p0
$serverConn.ConnectionContext.StatementTimeout = 0
#the below variables are necessary incase you have your data and log in a different location than the default.
$RelocateData = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("$p1", "$p2")
$RelocateLog = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("$p3", "$p4")
echo $p0 $p1 $p2 $p3 $p4 $p5 $p6
Restore-SqlDatabase -InputObject $serverConn -Database "$p5" -BackupFile "$p6" -RelocateFile @($RelocateData,$RelocateLog) -ReplaceDatabase
} -ArgumentList $ServerPrimary,$database_logical_name,$database_path,$log_logical_name,$log_path,$database,$backupfile_full_pathhttps://stackoverflow.com/questions/26955109
复制相似问题