Running the Audit Scripts from One Job and One Server List

The audit collectors (integrity checks, patching, security, capacity and the rest) run from one scheduled job, Run-DbaAudits.ps1. It reads a settings file, DbaAudits.config.json, and runs each script in turn. The server list is not a text file someone keeps up to date: it is a table, rebuilt each night from SCOM plus a baseline of servers that must always be audited, minus anything excluded with a reason.

Everything connects with Windows Authentication as the account that runs the job. There are no passwords in any file, and the runner refuses to start if the settings file has a key or value that looks like a password or secret.

How it fits together

  • Run-DbaAudits.ps1: the one scheduled job. Each script runs in its own try/catch, so a failure is logged with the script name and line and the next script still runs.
  • DbaAudits.config.json: the inventory server and database, the SCOM management server, the log folder and the list of tasks, in order. Set Enabled to false to skip a task.
  • Sync-ServerInventory.ps1: the first task each night. Reads every SQL Server instance SCOM monitors into dbo.ServerInventory and saves a fallback copy of the list.
  • Import-BaselineServers.ps1: run once, to load the servers that must always be audited, whether SCOM monitors them or not.
  • dbo.GetAuditServers: the final list the collectors read. SCOM instances, plus baseline servers SCOM missed, minus excluded servers.

Step 1: Create the tables

Run the table script on the inventory server. It creates dbo.ServerInventory, dbo.ServerSyncRun, dbo.ServerSyncIssue and the dbo.GetAuditServers function, along with the collectors’ own tables. It is safe to run again.

sqlcmd -S SQLMON01 -E -d DBA -i "C:\DBA\sql\01_Create_Tables.sql"

Step 2: Load the baseline

Rename the old hand-kept C:\DBA\Servers.txt to C:\DBA\BasicServers.txt, one server or SERVER\INSTANCE per line, then import it. Servers already in the table are marked as baseline, and their include or exclude setting is left alone. After this, the table is the list: add and exclude servers there, not in the file.

.\Import-BaselineServers.ps1 -BaselinePath "C:\DBA\BasicServers.txt" -InventoryServer SQLMON01 -InventoryDatabase DBA

Step 3: Run the SCOM sync by hand once

The job server needs the SCOM console (the OperationsManager PowerShell module), and the service account needs a SCOM role that can read monitored objects. Read-Only Operator is enough. The sync has not yet been run against a live SCOM management group, so check the instance names in the first run’s output before relying on it.

.\Sync-ServerInventory.ps1 -ScomManagementServer SCOM01 -InventoryServer SQLMON01
SELECT TOP (5) * FROM dbo.ServerSyncRun ORDER BY SyncRunID DESC;

SELECT i.ServerName, i.Issue
FROM dbo.ServerSyncIssue AS i
WHERE i.SyncRunID = (SELECT MAX(SyncRunID) FROM dbo.ServerSyncRun);

SELECT ServerName FROM dbo.GetAuditServers(7) ORDER BY ServerName;

Each run writes a row to dbo.ServerSyncRun: SUCCESS, WARNING or FAILED. A baseline server SCOM didn’t return is logged in dbo.ServerSyncIssue, because it is probably not being monitored. If SCOM returns nothing, or more than 20% fewer instances than last time, the run is flagged rather than quietly auditing fewer servers. A server SCOM misses stays on the list for seven days.

Step 4: Add or exclude a server

Always give a reason. Excluded servers stay in the table, so the history is kept and nobody adds them back by mistake.

-- Add a server SCOM does not monitor
INSERT dbo.ServerInventory (ServerName, IsBaseline, Source, Reason)
VALUES (N'SQLDEV05', 1, 'Manual', N'Not in SCOM: dev server, audit anyway');

-- Exclude a server
UPDATE dbo.ServerInventory
SET IncludeFlag = 0, Reason = N'Decommissioned, change ticket ref', ModifiedDate = SYSDATETIME()
WHERE ServerName = N'SQLOLD01';

Step 5: Schedule the runner

One Windows scheduled task, nightly, under the audit service account. To run only some tasks while testing, use -Only with task names or script names.

powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\DBA\Run-DbaAudits.ps1"

.\Run-DbaAudits.ps1 -Only 'Capacity', 'Invoke-SecurityAudit.ps1'

Step 6: Check the run

  • Exit code 0: every task ran. 1: at least one task failed, so Task Scheduler shows the run as failed. 2: the runner could not start (missing or unsafe settings file).
  • The log is in the LogFolder from the settings file (C:\DBA\Logs by default), kept for 90 days. It holds each script’s output and warnings, and for a failure, the script name and the line that failed.
  • If the inventory database is down, the collectors read the fallback file, C:\DBA\Servers.fallback.txt, which each sync rewrites with the final list.

From my SQL Server DBA handbook.