Commands / New-sqmRestoreTestJob
New-sqmRestoreTestJob
Backup & Restore sqmSQLTool v1.9.23+ · New ✓ -WhatIf supported
Generates a wrapper script in the module's jobs folder and creates a SQL Agent job that runs Invoke-sqmRestoreTest — together with a schedule. Unlike New-sqmRestoreDatabaseJob (an on-demand restore, deliberately unscheduled), a restore test is a recurring obligation, so this job is scheduled: monthly on the 1st at 02:00 by default, matching the usual audit cadence.
No backup path is baked in — that is the point. Without -BackupFile the generated wrapper contains no path, so every run resolves the current backup itself. An Ola environment needs exactly this: every backup file carries a timestamp, so a hardcoded path would work exactly once and then fail with “file not found” — or be gone already via Ola's @CleanupTime.
Before you create the job:
  1. 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.
  2. Use an elevated PowerShell. Without elevation the write to Program Files fails with “Access to the path … is denied”.
  3. sqmSQLTool and dbatools must be installed on the target instance — the job imports the module.
  4. The SQL Agent service account needs rights to restore and drop, plus read access to the backup files. The job runs under its Windows identity.

Parameters

ParameterTypeRequiredDefaultNotes
-SqlInstancestringOptional$env:COMPUTERNAMEInstance where the job is created and runs.
-SqlCredentialPSCredentialOptionalOnly for creating job/step/schedule. Deliberately not embedded into the wrapper.
-DatabaseNamestringRequiredSource database as it appears in the backup.
-BackupFilestring[]Optionalresolved per runOmit for Ola. Only give a path if the same, immutable file must always be tested.
-IncludeChainswitchSwitch$falseTest the whole chain instead of just the newest full backup.
-BackupRootPathstringOptionalConfig / instance backup dirRoot for the directory-scan fallback (forwarded).
-TestDatabaseNamestringOptionalauto, timestampedMust start with RestoreTest_. Omit it for a recurring job — a fixed name would need -AllowReplaceExistingTestDatabase on every run after the first.
-DataFilePathstringOptionalInstance default data pathTarget directory for the test copy's data files.
-LogFilePathstringOptionalInstance default log pathTarget directory for the test copy's log file.
-KeepTestDatabaseswitchSwitch$falseDo not drop the test copy. By default a job run cleans up — see below.
-AllowReplaceExistingTestDatabaseswitchSwitch$falseForwarded. Only needed together with a fixed -TestDatabaseName.
-RetentionMonthsintOptional12 (module config)How long evidence is kept. 0 = forever.
-OutputPathstringOptional<OutputPath>\RestoreTestDirectory for the evidence.
-ScheduleTypestringOptionalMonthlyMonthly, Weekly or Daily.
-ScheduleTimestringOptional02:00Start time as "HH:mm".
-ScheduleDayOfMonthintOptional1Day of month for Monthly (1–28). Capped at 28 so the schedule also fires in February.
-ScheduleDaysstring[]OptionalSundayWeekdays for Weekly.
-NoScheduleswitchSwitch$falseCreate the job without a schedule (manual starts only).
-JobNamestringOptionalsqmRestoreTest_<DB>Name of the Agent job.
-StepNamestringOptionalRunRestoreTestName of the job step.
-ForceswitchSwitch$falseOverwrite an existing job of the same name.
-StartJobswitchSwitch$falseStart the job right after creation — produces the first evidence immediately.
-EnableExceptionswitchSwitch$falseThrow instead of returning a failure result object.

Behaviour worth knowing

A job run drops the test copy. That differs from the manual call, where it stays. Reason: an unattended monthly job would otherwise leave a full-size copy behind on every run until the volume fills up. -KeepTestDatabase opts out — only sensible with a long interval and enough free space.
No password in the wrapper. -SqlCredential exists so you can create the job from a workstation; it is never written into the generated script. The job always runs under the SQL Agent service account's Windows identity. A password in a script on disk would be the wrong trade.
A failed job goes red. The step runs with -EnableException, so a failing restore test fails the Agent job visibly. That is deliberate: a restore test that silently does nothing is worse than none — no evidence would be produced while everyone assumes the obligation is covered.
The RestoreTest_ prefix rule is validated at job-creation time, so an invalid -TestDatabaseName fails immediately instead of at the first scheduled run months later.

Examples

Monthly evidence (default) — 1st at 02:00, backup resolved per run, copy cleaned up
New-sqmRestoreTestJob -SqlInstance "SQL01" -DatabaseName "Kunde"
Weekly Sunday 23:00, files onto a dedicated test volume, run once right away
New-sqmRestoreTestJob `
    -SqlInstance "SQL01" `
    -DatabaseName "Kunde" `
    -ScheduleType Weekly `
    -ScheduleDays "Sunday" `
    -ScheduleTime "23:00" `
    -DataFilePath "T:\RestoreTest" `
    -LogFilePath "T:\RestoreTest" `
    -StartJob
Whole chain, monthly on the 15th, evidence kept for 3 years
New-sqmRestoreTestJob `
    -SqlInstance "SQL01" `
    -DatabaseName "Kunde" `
    -IncludeChain `
    -ScheduleDayOfMonth 15 `
    -RetentionMonths 36
Job without a schedule (manual starts only)
New-sqmRestoreTestJob -SqlInstance "SQL01" -DatabaseName "Kunde" -NoSchedule
Check the job afterwards
Get-DbaAgentJobHistory -SqlInstance "SQL01" -Job "sqmRestoreTest_Kunde"
Get-ChildItem C:\System\WinSrvLog\MSSQL\RestoreTest

Output object

FieldContents
JobName / StepNameName of the created Agent job and its step.
ScheduleName / ScheduleName of the schedule and a readable description, e.g. "monatlich am 1. um 02:00".
RemovesTestDatabaseDoes a run clean up the test copy?
EvidencePathDirectory the evidence is written to.
WrapperPathPath of the generated wrapper script.
StartedWas the job started right away (-StartJob)?
Status / MessageSuccess or Failed, with detail.

See also

Invoke-sqmRestoreTest — the restore test itself, with all safety rules and measurement details.
Invoke-sqmRestoreDatabase — the productive, AlwaysOn-aware restore.