New-sqmRestoreDatabaseJob
Backup & Restore
sqmSQLTool v1.9.x · New
✓ -WhatIf supported
jobs folder and creates a SQL Agent job that runs Invoke-sqmRestoreDatabase with the restore parameters you specify. Unlike New-sqmRestoreTestJob, this job is created without a schedule — a productive restore is an on-demand operation, not recurring — meant to be started manually (Start-DbaAgentJob) or right away with -StartJob.
Before you create the job:
- Run this on the target instance. The wrapper is written locally to
C:\Program Files\WindowsPowerShell\Modules\sqmSQLTool\jobs\, where the Agent job later finds it. - Use an elevated PowerShell. Without elevation the write to Program Files fails with “Access to the path … is denied”.
- sqmSQLTool and dbatools must be installed on the target instance, the job imports the module.
- The SQL Agent service account needs sysadmin on the target instance and, for an AlwaysOn database, on all replicas — the same requirement
Invoke-sqmRestoreDatabaseitself documents. The job runs under its Windows identity.
No password in the wrapper. The generated script never embeds a SQL credential. When the job runs,
powershell.exe executes as the SQL Agent service account and Invoke-sqmRestoreDatabase connects via that account's Windows identity — the same auth model as New-sqmAlwaysOnRepairJob and New-sqmRestoreTestJob.Parameters
| Parameter | Type | Required | Default | Notes |
|---|---|---|---|---|
| -SqlInstance | string | Optional | $env:COMPUTERNAME | Instance where the job is created and runs. Also baked in as Invoke-sqmRestoreDatabase's -SqlInstance. |
| -DatabaseName | string | Required | , | Database to restore, as it appears in the backup file. |
| -BackupFile | string[] | Required* | , | Full backup path, or an array for a striped backup. Must be readable from the target instance. Mutually exclusive with -BackupFiles. |
| -BackupFiles | string[] | Required* | , | Full + Diff + Logs sequence, in order. Alternative to -BackupFile. |
| -NewDatabaseName | string | Optional | , | Restore under a new name (forwarded). |
| -NewDatabaseFilePath | string | Optional | , | Target directory for the data files (forwarded). |
| -NewLogFilePath | string | Optional | , | Target directory for the log file (forwarded). |
| -BackupBeforeRestore | switch | Switch | $false | Back up the existing database first (forwarded). |
| -NoUserExport | switch | Switch | $false | Skip the database-user export (forwarded). |
| -KeepAlwaysOn | switch | Switch | $false | Forwarded — see Invoke-sqmRestoreDatabase. |
| -AvailabilityGroupName | string | Optional | , | Forwarded — forces AG-aware handling / selects the AG explicitly. |
| -WithNoRecovery | switch | Switch | $false | Forwarded — leaves the database in RESTORING state. |
| -ContinueWithNoRecovery | switch | Switch | $false | Forwarded — the last restore step also uses NORECOVERY. |
| -ForceSingleUser | switch | Switch | $false | Forwarded — force single-user mode before the restore. |
| -NoRejoinAvailabilityGroup | switch | Switch | $false | Forwarded — do AG detection/cleanup but skip the actual (re)join. |
| -JobName | string | Optional | sqmRestore_<DB> | Name of the Agent job. |
| -StepName | string | Optional | RunRestore | Name of the single job step. |
| -Force | switch | Switch | $false | Overwrite an existing job of the same name. |
| -StartJob | switch | Switch | $false | Start the job immediately after creation. |
| -EnableException | switch | Switch | $false | Throw instead of returning a failure result object. |
Execution Flow
Output object
| Field | Contents |
|---|---|
JobName / StepName | Name of the created Agent job and its step. |
DatabaseName | The database the job restores. |
WrapperPath | Path of the generated wrapper script. |
Started | Was the job started right away (-StartJob)? |
Status / Message | Success or Failed, with detail. |
Examples
Create the job, leave it for the DBA to start later
New-sqmRestoreDatabaseJob -SqlInstance "SQL01" -BackupFile "D:\Backup\AdventureWorks.bak" -DatabaseName "AdventureWorks"
Full + Diff + Logs sequence, create and run right away
$seq = @("D:\Backup\App_Full.bak", "D:\Backup\App_Diff.bak", "D:\Backup\App_Log1.trn")
New-sqmRestoreDatabaseJob -SqlInstance "SQL01" -BackupFiles $seq -DatabaseName "App" -StartJobRecreate an existing job of the same name
New-sqmRestoreDatabaseJob -SqlInstance "SQL01" -BackupFile "D:\Backup\App.bak" -DatabaseName "App" -Force
See also
Invoke-sqmRestoreDatabase, the restore itself, with the full AG-aware safety model.
New-sqmRestoreTestJob, the recurring, scheduled counterpart for audit evidence.