Execution Flow
The job step runs
powershell.exe -File Repair-Job.ps1 via a CmdExec subsystem step. The output directory is created first and its NTFS ACL is updated to grant the SQL Server Agent service accounts write access, ensuring job output files can be written even under least-privilege configurations.Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| -SqlInstance | String | Optional | Target SQL Server instance. Default: $env:COMPUTERNAME. |
| -SqlCredential | PSCredential | Optional | SQL authentication credentials. |
| -JobName | String | Optional | Agent job name. Default: sqmTool_AlwaysOn_AutoRepair. |
| -ScheduleName | String | Optional | Schedule name. Default: sqmTool_AlwaysOn_AutoRepair_Hourly. |
| -RepairScriptPath | String | Required | Full path to Repair-Job.ps1 on the SQL Server host. |
| -OutputPath | String | Optional | Directory for job output files and logs. Created if absent; ACL set for SQL Agent accounts. |
| -JobCategory | String | Optional | Agent job category. Default: Database Maintenance. |
| -Force | Switch | Optional | Remove and recreate the job if it already exists. |
| -EnableException | Switch | Optional | Throw exceptions immediately on error. |
| -WhatIf | Switch | Optional | Preview actions without creating the job or directory. |
| -Confirm | Switch | Optional | Prompt before creating objects. |
Return Value
Returns a PSCustomObject with: SqlInstance, JobName, ScheduleName, RepairScriptPath, OutputPath, Status (Created / Replaced / CreateFailed / WhatIfSkipped), Message.
Examples
Example 1 — Create repair job with defaults
New-sqmAlwaysOnRepairJob -SqlInstance "SQL01" `
-RepairScriptPath "C:\DBA\Scripts\Repair-Job.ps1"
Example 2 — Custom job name, output path, and force replace
New-sqmAlwaysOnRepairJob -SqlInstance "SQL01" `
-RepairScriptPath "C:\DBA\Scripts\Repair-Job.ps1" `
-JobName "AG_AutoRepair_Finance" `
-OutputPath "D:\AgentOutput\AGRepair" `
-Force
Example 3 — WhatIf preview
New-sqmAlwaysOnRepairJob -SqlInstance "SQL01" `
-RepairScriptPath "C:\DBA\Scripts\Repair-Job.ps1" -WhatIf