Restore Managed Instance database with Power Shell

This week I got customer for somehow needed to restore the Managed Instance database with Power Shell, He asked the help as they got issue with restoring database with Power Shell, it seemed every time they got different error messages with new command, the errors were like:

Restore-AzSqlInstanceDatabase: Cannot bind parameter 'DeletionDate'. Cannot convert value "TargetInstanceDatabaseName" to type "System.DateTime". Error: "The string 'TargetInstanceDatabaseName' was not recognized as a valid DateTime. There is an unknown word starting at index '0'."
Restore-AzSqlInstanceDatabase: Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by parameter 'FromPointInTimeBackup'.
Restore-AzSqlInstanceDatabase: Cannot bind parameter 'PointInTime'. Cannot convert value "2022-03-17T03:04:00AM" to type "System.DateTime". Error: "String '2022-03-17T03:04:00AM' was not recognized as a valid DateTime."

So, Let’s see how we can restore Managed Instance Database with Power Shell

I always suggested to use Azure Cloud Shell if you have access to Azure Portal, that’s cool stuff that you do not need to install or modified anything on your computer, even do not need to put subscription details. You can easily Select the Cloud Shell icon on the Azure portal. See Microsoft document for Cloud Shell

I also provided the following links Restore-AzSqlInstanceDatabase (Az.Sql) | Microsoft Docs with those scripts you can restore an instance database from a point in time or different resource group or many other scenarios, let me show you how you can restore the database with point in time, Please look how I use -PointInTime parameter, that needs to be UTC time, If you do not know the details like earliest restore point or Resource group the easiest way is go to the portal and check all those details in Overview:

All details about Managed Instance that you need in Azure Portal

Then as I mention open Azure Cloud Shell and run the following command, You need to change the parameters with your Managed Instance.

Restore-AzSqlinstanceDatabase -Name "Database01" -InstanceName "managedInstance1" -ResourceGroupName "ResourceGroup01" -PointInTime UTCDateTime -TargetInstanceDatabaseName "Database01_restored"

In the following example I try to restore new database, see how I use PointInTime parameter.

Restore-AzSqlinstanceDatabase -Name "DB032022" -InstanceName "misql2022" -ResourceGroupName "myResourceGroup"  -PointInTime 2022-03-17 03:10:00 -TargetInstanceDatabaseName "DB032022_2022-03-16T05-52Z_NEW"

Then execute and you’ll see the confirmation command like the below

If you have some unknown issue like permission and so on using the Power Shell always is a good way as it provides you more details.

As I mentioned if you need to see more command or practice command for managed instance please have a look Restore-AzSqlInstanceDatabase (Az.Sql) | Microsoft Docs

All details like database name, Resource Group only use for this post and I already deleted from my lab.