在脚本块中运行
Restore-AzRecoveryServicesBackupItem
时,-RestorePoint
参数不允许我传入使用 Get-AzRecoveryServicesBackupRecoveryPoint
获得的还原点,我收到以下错误。
Cannot bind parameter 'RecoveryPoint'. Cannot convert value "Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models.AzureVmRecoveryPoint" to type "Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models.RecoveryPointBase". Error: "Cannot convert the "Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models.AzureVmRecoveryPoint" value of type "Deserialized.Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models.AzureVmRecoveryPoint" to type "Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models.RecoveryPointBase"
Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models.AzureVmRecoveryPoint
源自 Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models.RecoveryPointBase
,所以我不确定为什么它拒绝转换以绑定到 -RecoveryPoint
参数。该代码在不使用 Start-Job
时有效,所以我想这与此有关。
下面是我的代码
$recoveryPoints = Get-AzRecoveryServicesBackupRecoveryPoint `
-Item $backupItem `
-StartDate ( (Get-Date).AddDays(-1).ToUniversalTime() ) `
-EndDate ( (Get-Date).ToUniversalTime() )
$rp = $recoveryPoints[0]
$scriptBlock = {
try {
$restoreJob = Restore-AzRecoveryServicesBackupItem `
-RecoveryPoint $args[0] `
-TargetResourceGroupName $args[1] `
-StorageAccountName $args[2].Name `
-StorageAccountResourceGroupName $args[2].ResourceGroupName
Wait-AzRecoveryServicesBackupJob -Job $restoreJob -Timeout 3600
}
catch {
Throw "$_"
}
}
$asyncJob = Start-Job -ScriptBlock $scriptBlock -ArgumentList $rp,$rgName,$storageAccount
此错误的原因是
RestorePoint
的 Restore-AzRecoveryServicesBackupItem
参数需要 Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models.RecoveryPointBase
类型的对象,但您传递的是 Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models.AzureVmRecoveryPoint
类型的对象。
因此,当您向
$rp
块提供 script
对象时,它会被序列化和反序列化,然后传递给由 PowerShell
创建的新 Start-Job
进程。由于此进程无法访问 Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models.AzureVmRecoveryPoint type
,因此无法正确反序列化该对象。
Export-Clixml
命令的方法,因为它创建对象的 XML
表示并将其存储在文件中。
Import-Clixml
导入它,它会创建相应的对象。
$rp | Export-Clixml -Path 'xxxx' #serializing the $rp out of the script block
$rp = Import-Clixml -Path 'xxx' #deserializing inside the script block
完成后,将其与
Start-Job
命令-argument
参数一起使用。
Start-Job -ScriptBlock $scriptBlock -ArgumentList $rp,$rgName,$storageAccount # Or you can directly pass the deserialized path in place of `$rp` variable