Restore a database in Azure SQL Managed Instance to a previous point in time

DJAMEL GARAR 20 Reputation points
2025-02-18T14:09:59.4366667+00:00

 

Hello everyone,

I created  a  powershell script below and I scheduled every day with SQL Agent Job SSMS to restore database from automatic backup azure managed instance , but I have this error :

'The term 'Get-AzSubscription' is not recognized as the name of a cmdlet, function, script file, or operable program. Process Exit Code -1.  The step failed

 

I check and install the module powershell:

Install-Module -Name Az -Force -AllowClobber -Scope AllUsers or

but no success , the error Is the same Install-Module not recognized.

 

When I try to run this program in powershell portail azure (https://portal.azure.com ) , it work , but with sql agent job (SSMS ) not work.

How can I install the module azure power shell in azure managed instance (SQL Agent ) ?

 

$subscriptionId = "xxxxxxxxxxxxxxxxxxxxxxxxxx"

$resourceGroupName = "xxxxxx"

$managedInstanceName = "az-xxxxxxx"

$databaseName = "prod"

$pointInTime = "2025-02-16T11:30:39.3882806Z"

$targetDatabase = "supp"

 

Get-AzSubscription -SubscriptionId $subscriptionId

Select-AzSubscription -SubscriptionId $subscriptionId

Restore-AzSqlInstanceDatabase -FromPointInTimeBackup -ResourceGroupName $resourceGroupName -InstanceName $managedInstanceName -Name $databaseName -PointInTime $pointInTime -TargetInstanceDatabaseName $targetDatabase

 

 

Reference :

Restore a database in Azure SQL Managed Instance to a previous point in time:

https://learn.microsoft.com/en-us/azure/azure-sql/managed-instance/point-in-time-restore?view=azuresql&tabs=azure-powershell#restore-to-a-different-subscription

Thanks

Azure SQL Database
{count} votes

Accepted answer
  1. Alex Burlachenko 1,750 Reputation points
    2025-02-18T14:38:38.22+00:00

    Alright, mate!

    so I think you’re trying to run a PowerShell script in a SQL Server Agent job, but it’s failing because the Azure PowerShell module (Az) isn’t installed or recognised in the SQL Agent’s environment.

    Install the Az Module on the SQL Server Machine-> open PowerShell as an admin and run next

    Install-Module -Name Az -Force -AllowClobber -Scope AllUsers

    This installs the module for everyone, including SQL Agent.

    next add Import-Module Az to Your Script

    At the top of your script, add:

    Import-Module Az

    This ensures the script loads the module.

    In SQL Server Agent, make sure the job step is set to run as PowerShell and not SQL Server’s built-in PowerShell.

    Run the script manually in PowerShell on the SQL Server machine to make sure it works.

    Ensure the SQL Server Agent account has the right permissions to access Azure resources.

    If it still doesn’t work, consider using Azure Automation for running PowerShell scripts—it’s much smoother for cloud tasks.

    Cheers! Alex


3 additional answers

Sort by: Most helpful
  1. DJAMEL GARAR 20 Reputation points
    2025-02-19T18:43:34.7866667+00:00

    @Mallaiah Sangi : the issue is persist (the error Is Install-Module not recognized.), i am waiting for my question :

    How can I install the module Azure Power Shell (command below ) in Azure Managed instance (not in machine local SQL Server) ?

    Install-Module -Name Az -Force -AllowClobber -Scope AllUsers

    Thanks


  2. DJAMEL GARAR 20 Reputation points
    2025-02-26T13:23:36.0766667+00:00

    Hi @Mallaiah Sangi

    I found it an alternative solution but my first initiated issue not yet resolved.

    Could you please send me demo (video) URL , to show me how to install module power shell in Cloud Azure managed instance ?

    Thanks


  3. DJAMEL GARAR 20 Reputation points
    2025-02-26T17:16:26.75+00:00

    Excellent, That's fine.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.