Skip to content

Instantly share code, notes, and snippets.

@MikeFal
Created January 19, 2016 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeFal/5ad9e68f14319cd23b9f to your computer and use it in GitHub Desktop.
Save MikeFal/5ad9e68f14319cd23b9f to your computer and use it in GitHub Desktop.
#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
}
@MikeFal
Copy link
Author

MikeFal commented Jan 19, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment