forked from mmessano/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPS_MoveIndexes_SMO.ps1
82 lines (71 loc) · 2.68 KB
/
PS_MoveIndexes_SMO.ps1
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$scrp = New-Object Microsoft.SqlServer.Management.Smo.ScriptingOptions
$scrp.IncludeDatabaseContext = $true;
$dt = get-date
$logFile = "C:\Dexma\Logs\IndexMovementScript_"+ $dt.Year.ToString() + $dt.Month.ToString() + $dt.Day.ToString() + $dt.Hour.ToString()+ $dt.Minute.ToString() + ".sql"
$errorLogFile = "C:\Dexma\Logs\IndexMovementScriptFailures_"+ $dt.Year.ToString() + $dt.Month.ToString() + $dt.Day.ToString() + $dt.Hour.ToString()+ $dt.Minute.ToString() + ".sql"
$sqlInstanceName = "XSQLUTIL18";
#Get the SMO SQL Object
$sqlServer = New-Object -typeName Microsoft.SqlServer.Management.Smo.Server -argumentList "$sqlInstanceName"
#Get the User Databases that are not in read only
$userDbs = $sqlServer.Databases | Where-Object {$_.IsSystemObject -eq $false -and $_.ReadOnly -eq $false}
function Invoke-Sqlcmd3
{
param(
[string]$ServerInstance,
[string]$Query
)
$QueryTimeout=30
$conn=new-object System.Data.SqlClient.SQLConnection
$constring = "Server=" + $ServerInstance + ";Integrated Security=True"
$conn.ConnectionString=$constring
$conn.Open()
if($conn){
$cmd=new-object system.Data.SqlClient.SqlCommand($Query,$conn)
$cmd.CommandTimeout=$QueryTimeout
$ds=New-Object system.Data.DataSet
$da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd)
[void]$da.fill($ds)
$conn.Close()
$ds.Tables[0]
}
}
foreach($currentDB in $userDbs)
{
#if($currentDB.FileGroups.Contains("SECONDARY"))
#{
foreach($tb in $currentDB.Tables | Where-Object {$_.HasIndex -eq $true -and $_.IsSystemObject -eq $false} `
| Select-Object name,indexes )
{
foreach($ind in $tb.Indexes | Where-Object{$_.IsClustered -eq $false -and $_.IsXmlIndex -eq $false})
{
if($ind.FileGroup -eq "PRIMARY")
{
try
{
#Setup the DROP Index Stmt.
$scrp.IncludeIfNotExists = $true;
$scrp.DriAll = $true;
$scrp.ScriptDrops = $true;
$sql = $ind.Script($scrp);
#Setup the CREATE Index stmt on the new filegroup
$ind.FileGroup = "SECONDARY" # New Filegroup
$scrp.IncludeIfNotExists = $false;
$scrp.ScriptDrops = $false;
# append create to drop statement
$sql += $ind.Script($scrp);
#Log the SQL to a file (named above with a datetime suffix)
$sql | Out-File $logFile -append
#Execute the SQL index move !!! Commented out just in case
#Invoke-Sqlcmd3 $sqlServer.Name $sql
}
catch
{
$errorMsg = "No File in FileGroup:" + $currentDB.Name + ":" + $ind.Name
$errorMsg | Out-File $errorLogFile -append
}
} # index in file group
} # for each index
} #for each table
#} # if DB contains file group
}