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. |
| -JobName | String | Optional | Agent job name. Default: sqmTool_AlwaysOn_AutoRepair. |
| -Force | Switch | Optional | Remove and recreate the job if it already exists. |
| -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" `
-ForceExample 3, WhatIf preview
New-sqmAlwaysOnRepairJob -SqlInstance "SQL01" `
-RepairScriptPath "C:\DBA\Scripts\Repair-Job.ps1" -WhatIf