-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCreateSQLServerConnections.ps1
More file actions
56 lines (46 loc) · 1.76 KB
/
CreateSQLServerConnections.ps1
File metadata and controls
56 lines (46 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@@ -0,0 +1,54 @@
#Error handling
$ErrorActionPreference = "continue";
Trap {
$err = $_.Exception
while ( $err.InnerException )
{
$err = $err.InnerException
write-error $err.Message
};
}
#Load-SMOType (12)
#Driver variables, will become params
$MaxConnections = 3
$Server= "(local)\sql2014cs" ;
#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-1;$i++){
$randdb = Get-Random -Minimum 1 -Maximum $dbs.Count
$DbConnections += $dbs[$randdb].Name
}
#$DbConnections
<#
# Debugging code. Create a connection using code from the $cmdstr below.
Add-Type -AssemblyName "Microsoft.SqlServer.Smo,Version=$(12).0.0.0,Culture=neutral,PublicKeyToken=89845dcd8080cc91"
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$SqlConn = New-Object Microsoft.SqlServer.Management.Smo.Server("$Server")
$SqlConn.Databases['master'].ExecuteNonQuery("WAITFOR DELAY '00:05:00'")
#>
#Loop through DB Connection array, create script block for establishing SMO connection/query
#Start-Job for each script block
foreach ($DBName in $DbConnections ) {
$cmdstr =@"
`Add-Type -AssemblyName "Microsoft.SqlServer.Smo,Version=$(12).0.0.0,Culture=neutral,PublicKeyToken=89845dcd8080cc91"
`[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
`$SqlConn = New-Object Microsoft.SqlServer.Management.Smo.Server ("$Server")
`$SqlConn.Databases['$DBName'].ExecuteNonQuery("WAITFOR DELAY '00:00:30'")
"@
# Display script that will be run in the script block.
#$cmdstr
$cmd = [ScriptBlock]::Create($cmdstr)
Start-Job -ScriptBlock $cmd
}