Created
January 19, 2016 16:26
-
-
Save MikeFal/5ad9e68f14319cd23b9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Driver variables, will become params | |
$MaxConnections = 50 | |
$Server= 'localhost' | |
#Set Initial collections and objects | |
$SqlInstance = New-Object Microsoft.SqlServer.Management.Smo.Server $Server | |
$DbConnections = @(); | |
$dbs = $SqlInstance.Databases | Where-Object {$_.IsSystemObject -eq 0} | |
#Build DB connection array | |
for($i=0;$i -le $MaxConnections;$i++){ | |
$randdb = Get-Random -Minimum 1 -Maximum $dbs.Count | |
$DbConnections += $dbs[$randdb].Name | |
} | |
#Loop through DB Connection array, create script block for establishing SMO connection/query | |
#Start-Job for each script block | |
foreach ($DBName in $DbConnections ) { | |
$cmdstr =@" | |
`[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | |
`$SqlConn = New-Object Microsoft.SqlServer.Management.Smo.Server $Server | |
`$SqlConn.Databases['$DBName'].ExecuteNonQuery("WAITFOR DELAY '00:05:00'") | |
"@ | |
$cmd = [ScriptBlock]::Create($cmdstr) | |
Start-Job -ScriptBlock $cmd | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I kept all this in the SMO for simplicity. It takes a couple to fire up all the connections and there's probably some refinement that can happen there, but this will get a simple batch of connections open. For the database connections, I randomized what databases are connected to by building an initial collection of database names. I ignore system databases (note, this will blow up if you only have 1 user database). Be aware of the escape characters in the scriptblock building section.