Skip to content

Instantly share code, notes, and snippets.

nanoDBA / arrayFromHere-String.ps1
Created May 4, 2021
Converting a Here-String to an Array of Strings
View arrayFromHere-String.ps1
$someThingsArray = ($HereString = @"
).split("`n").TrimEnd("`r") <# Converting a Here-String to an Array of Strings #>
nanoDBA / process-insert.ps1
Last active Dec 11, 2020
Avoid running the query against instances that may be down and time out, as well as adding some output that can be seen when looking at job history when scheduling the PowerShell job using SQL Agent
View process-insert.ps1
# adapted from
# Specify your servers
$servers = "sql2014","sql2012","sql2016","sql2017"
# Setup the T-SQL
$sql = "SELECT SERVERPROPERTY('ServerName') AS SqlInstance, login_name as [Login], [host_name] as Host,
DB_NAME(p.dbid) as [Database], s.[program_name] as Program, s.login_time as LoginTime
FROM sys.dm_exec_sessions s inner join sys.sysprocesses p on s.session_id = p.spid
WHERE p.dbid is not NULL
and DB_NAME(p.dbid) != 'tempdb'
nanoDBA / FakeClient.ps1
Created Dec 11, 2020
Impersonating an Application Name Using Connect-DbaInstance
View FakeClient.ps1
$cred = Get-Credential 'domain\someAccount'
$connFakeClient = Connect-DbaInstance -SqlInstance YOURSQLSRVR -SqlCredential $cred -ClientName "Test App"
Invoke-DbaQuery -SqlInstance $connFakeClient -Query "WAITFOR DELAY '00:02:00'" -MessagesToOutput
nanoDBA / Encryption of Credential with AES Key.ps1
Last active May 27, 2020
Passing Credentials in PowerShell between users and computers using keys
View Encryption of Credential with AES Key.ps1
## Create AES key with random data and export to file
Sometimes a script needs to use a password, but you want it stored securely. Passing a credential by utilizing 2 things:
1. A password file that contains the encrypted file
2. A key file that contains a randomly generated AES key
allows for utilization of credentials within scripts by different users and servers.
**The following method is only as secure as the locations of the files(password and key) themselves.**
View fix page verification by setting it to checksum in databases.sql
--An alternative to sp_msforeachdb
IF OBJECT_ID('tempdb..#tmp') IS NOT NULL DROP TABLE #tmp;
SELECT name, 0 AS completed
INTO #tmp
FROM sys.databases AS s
WHERE s.page_verify_option_desc <> 'CHECKSUM'
nanoDBA / Get-SPIDs_last_batch.sql
Created May 31, 2019
Last_Batch timestamp - From "How to detect SqlServer connection leaks in a applications?"
View Get-SPIDs_last_batch.sql
SELECT S.spid, S.login_time, S.last_batch, S.status, S.hostname, S.program_name, S.cmd, S.loginame,
last_sql = ( select text from sys.dm_exec_sql_text(S.sql_handle) )
, database_name = DB_NAME(dbid)
FROM sys.sysprocesses S
INNER JOIN sys.dm_exec_sessions sx
on sx.session_id = S.spid
and sx.is_user_process = 1
nanoDBA / Practical PowerShell for Busy DBAs.ps1
Created Apr 30, 2019
Intro to PowerShell and dbatools for SQL Server DBAs (originally presented in August 2018 using the ISE)
View Practical PowerShell for Busy DBAs.ps1
#requires -version 5.0
#requires runasadministrator
#region overview
'Practical PowerShell for The Busy DBA' – this will be hands-on (lab + presentation)
- Basic overview of PowerShell and getting started (WMF 5.1 and other versions: prereqs, cmdlets,
transcription, console navigation, and getting quick help/examples)
- Installing Modules with PowerShell 5.0 and up
nanoDBA / Sequential_Restores.ps1
Created Apr 30, 2019
Demo - PowerShell + DevOps Global Summit 2019 - Sequential Restores
View Sequential_Restores.ps1
# How will the backup headers be scanned? SQL Server and Backup Path
$BkParamHash = @{
SqlInstance = "localhost"
Path = "D:\Share01\Demo", "D:\Share02\Demo"
$BackupMetaData = Get-DbaBackupInformation @BkParamHash -Verbose
# Let's restore 11 databases
$DbSuffixRange = 500..510
nanoDBA / Parallel_Restores.ps1
Created Apr 30, 2019
Demo - PowerShell + DevOps Global Summit 2019 - Parallel Restores
View Parallel_Restores.ps1
# Restoring Databases from Files in Parallel
# Let's restore 11, 21, 41 databases!
$DbSuffixRange = 500..510
# $DbSuffixRange = 500..520
# $DbSuffixRange = 500..540
$RSJobparamHash = @{
Throttle = 8
ModulesToImport = "dbatools"
nanoDBA / Get-AlwaysOn_Availability_Groups_Lag,info.sql
Created Apr 22, 2019
How far behind is Secondary in AlwaysOn replica ? ( LAG )
View Get-AlwaysOn_Availability_Groups_Lag,info.sql
--How far behind is Secondary in AlwaysOn replica ? ( LAG )
--"There are a lot of times customers ask me how much time is my secondary behind my primary?
--Such a simple question can be answered easily using some good DMVs."
AGL.dns_name AS Listener_dns_name,
AGL.port AS Listener_port,
AGL.ip_configuration_string_from_cluster AS Cluster_IP_addresses,
AR.replica_server_name AS InstanceName,