PowerShell for SQL Server Flashcards

(14 cards)

1
Q

In an interview, how do you contrast the current Microsoft story for SQL automation in PowerShell with legacy options?

A

The SqlServer module from the PowerShell Gallery (Install-Module SqlServer) is the default path for new work. Older installs used SQLPS / SQL Server PowerShell snap-ins — treat those as legacy unless the org still mandates them. Keep the module updated relative to your SQL Server major version.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does Invoke-Sqlcmd do, and what are typical parameters you’d name?

A

It runs T-SQL or .sql files with a sqlcmd-style surface. Common switches: -Query, -InputFile, -ServerInstance, -Database, -Credential (or integrated auth). It’s the familiar entry point for scripted queries from PowerShell.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is SMO, and when is raw SMO vs Invoke-Sqlcmd the right mental model?

A

SMO (SQL Server Management Objects) is the .NET library tools use for programmatic DDL/DML, scripting objects, and migrations. Invoke-Sqlcmd is lighter for “run this batch”; SMO is heavier but flexible. The SqlServer module cmdlets often wrap SMO; advanced scripts may use [Microsoft.SqlServer.Management.Smo.Server] directly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Name example SqlServer module cmdlets for backup/restore, AGs, and discovery — and what caveat applies?

A

Examples: Backup-SqlDatabase, Restore-SqlDatabase; Join-SqlAvailabilityGroup, Switch-SqlAvailabilityGroup (failover), Resume-SqlAvailabilityDatabase / Suspend-SqlAvailabilityDatabase; Get-SqlInstance, Get-SqlDatabase, Get-SqlErrorLog. Names and parameters vary by module/SQL version — verify Learn docs for your version.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Where do WSFC / cluster operations live in PowerShell relative to the SqlServer module?

A

Not in SqlServer. Use the FailoverClusters module: Get-Cluster, Get-ClusterNode, Add-ClusterNode, etc. That’s the Windows Server Failover Clustering surface that sits next to SQL AG/FCI work.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Give four reasons teams use PowerShell with SQL Server (not just “scripting”).

A

Repeatable automation across instances; remote and scheduled execution (Task Scheduler, Azure Automation, CI/CD); glue between OS tasks (disks, services, WSFC) and SQL tasks; object pipeline output to CSV, JSON, logging, and monitoring hooks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Map one example each for runbooks, provisioning, migrations, and monitoring from a DBA/DevOps angle.

A

Runbooks: patch drain, failover, AG health, post-deploy backup checks. Provisioning: databases, logins, jobs from templates; restore to lower envs. Migrations: drive DACPAC/BACPAC or sqlpackage; schema drift (often SMO or third-party). Monitoring: DMVs, Invoke-Sqlcmd with JSON, feed observability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is dbatools, and how do you position it vs SqlServer in an interview?

A

dbatools is a community PowerShell module for SQL (migration, backup, AG, sp_configure, etc.) built on SMO. Interview line: teams standardize on SqlServer from Microsoft or dbatools for fleet work — both sit above SMO; dbatools is opinionated and feature-rich for DBAs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How should you handle authentication and secrets in PowerShell scripts for SQL?

A

Prefer Windows integrated auth with a service account and least privilege for runbooks. For SQL authentication, use PSCredential, SecretManagement, or vault patterns — never plaintext passwords in scripts in git. Azure SQL / Entra often use Invoke-Sqlcmd with token/AAD options — confirm current docs for your module version.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When is T-SQL the better tool vs PowerShell vs SSMS?

A

T-SQLset-based work, DMVs, one-off diagnostics inside the engine. PowerShellmulti-instance loops, OS + SQL glue, CI/CD, scheduled runbooks. SSMS — interactive triage and GUI wizards — not the automation layer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Finish this sentence for a panel: “PowerShell doesn’t replace T-SQL — …”

A

“…it orchestrates it and ties SQL to Windows and pipelines.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Give a compact “stack” answer: SqlServer, SMO, FailoverClusters, and optional dbatools.

A

“I use the SqlServer module for Invoke-Sqlcmd, backup/restore, and AG cmdlets where they save time; WSFC is FailoverClusters. Under the hood that’s often SMO. For heavy fleet work, teams add dbatools on top of SMO.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What policy/compliance-style tasks does PowerShell excel at across a fleet?

A

Enumerating server settings, permissions, and backup history across many instances — repeatable reporting for audits and standards checks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why pair PowerShell with sqlpackage / DACPAC workflows in migrations?

A

PowerShell orchestrates packaging and deployment (drive sqlpackage, DACPAC/BACPAC), integrates with pipelines, and can combine deployment with pre/post checks — while the schema work is still expressed through those artifacts and/or SMO tooling.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly