forked from MikeFal/IntroToPowershell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.0-PracticalUse.ps1
More file actions
206 lines (165 loc) · 7.65 KB
/
Copy path4.0-PracticalUse.ps1
File metadata and controls
206 lines (165 loc) · 7.65 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#SQL Connection test
function Test-SQLConnection{
param([parameter(mandatory=$true)][string[]] $Instances)
$return = @()
foreach($InstanceName in $Instances){
$row = New-Object –TypeName PSObject –Prop @{'InstanceName'=$InstanceName;'StartupTime'=$null}
try{
$check=Invoke-Sqlcmd -ServerInstance $InstanceName -Database TempDB -Query "SELECT @@SERVERNAME as Name,Create_Date FROM sys.databases WHERE name = 'TempDB'" -ErrorAction Stop -ConnectionTimeout 3
$row.InstanceName = $check.Name
$row.StartupTime = $check.Create_Date
}
catch{
#do nothing on the catch
}
finally{
$return += $row
}
}
return $return
}
Test-SQLConnection -Instances 'PICARD'
#Now that function is available to us as if
Test-SQLConnection -Instances @('PICARD','RIKER','NotAValidServer')
#Cool. Now let's have some fun
$out = @()
for($port=1430;$port -le 1440;$port++){
$row = Test-SQLConnection -Instances "PICARD,$port" | select InstanceName,StartupTime,@{name='Host';expression={'PICARD'}},@{name='Port';expression={"$port"}}
$out+=$row
}
$out | Where-Object {$_.StartupTime -ne $null} | Format-Table
$CMS='PICARD'
$servers=@((dir "SQLSERVER:\SQLRegistration\Central Management Server Group\$CMS").Name)
$servers+=$cms
Test-SQLConnection -Instances $servers
function Export-SQLDacPacs{
param([string[]] $Instances = 'localhost',
[string] $outputdirectory=([Environment]::GetFolderPath("MyDocuments"))
)
#get the sqlpackage executable
$sqlpackage = get-childitem 'C:\Program Files (x86)\Microsoft SQL Server\130\DAC\bin\sqlpackage.exe'
#declare a select query for databases
$dbsql = @"
SELECT name FROM sys.databases
where database_id >4 and state_desc = 'ONLINE'
"@
#loop through each instance
foreach($instance in $Instances){
#set processing variables
$dbs = Invoke-Sqlcmd -ServerInstance $instance -Database tempdb -Query $dbsql
$datestring = (Get-Date -Format 'yyyyMMddHHmm')
$iname = $instance.Replace('\','_')
#extract each db
foreach($db in $dbs.name){
$outfile = Join-Path $outputdirectory -ChildPath "$iname-$db-$datestring.dacpac"
$cmd = "& '$sqlpackage' /action:Extract /targetfile:'$outfile' /SourceServerName:$instance /SourceDatabaseName:$db"
Invoke-Expression $cmd
}
}
}
#But once you write it, it's easy to call
Export-SQLDacPacs -instances 'PICARD' -outputdirectory 'C:\IntroToPowershell'
function Optimize-SQLMemory{
<#
.SYNOPSIS
Configures a SQL Server instance per the Jonathan Kehayias' guidelines.
.DESCRIPTION
This script will configure your SQL Server instance per the guidelines
found in Jonathan Kehayias' blog post: http://www.sqlskills.com/blogs/jonathan/how-much-memory-does-my-sql-server-actually-need/
The rules are:
- 1GB for initial OS reserve
- +1GB per 4GB server RAM up to 16GB
- +1GB per 8GB server RAM above 16
.PARAMETER
-target SQL instance name, i.e. localhost\SQL2012, DBASERVER01
-apply Switch parameter, call if you want to actually apply the changes. Otherwise, a report will be produced.
.EXAMPLE
Optimize-SQLMemory -instance DBASERVER01 -apply
#>
param([parameter(Mandatory=$true)][string] $target
, [Switch] $apply
)
#load SMO
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$smoserver = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server $target
$sqlhost = $smoserver.ComputerNamePhysicalNetBIOS
#set memory variables
$totalmem = (gwmi Win32_ComputerSystem -computername $sqlhost).TotalPhysicalMemory/1GB
$sqlmem = [math]::floor($totalmem)
#calculate memory
while($totalmem -gt 0){
if($totalmem -gt 16){
$sqlmem -= [math]::floor(($totalmem-16)/8)
$totalmem=16
}
elseif($totalmem -gt 4){
$sqlmem -= [math]::floor(($totalmem)/4)
$totalmem = 4
}
else{
$sqlmem -= 1
$totalmem = 0
}
}
#if not in debug mode, alter config. Otherwise report current and new values.
$srv = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server $target
"Instance:" + $target
"Max Memory:" + $srv.Configuration.MaxServerMemory.ConfigValue/1024 + " -> " + $sqlmem
"Min Memory:" + $srv.Configuration.MinServerMemory.ConfigValue/1024 + " -> " + $sqlmem/2
if($apply){
$srv.Configuration.MaxServerMemory.ConfigValue = $sqlmem * 1024
$srv.Configuration.MinServerMemory.ConfigValue = $sqlmem/2 * 1024
$srv.Configuration.Alter()
"Configuration Complete!"
}
}
Optimize-SQLMemory -target 'PICARD'
function Measure-SQLExecution{
param($instancename
,$databasename = 'tempdb'
,[Parameter(ParameterSetName = 'SQLCmd',Mandatory=$true)]$sqlcmd
,[Parameter(ParameterSetName = 'SQLScript',Mandatory=$true)]$sqlscript)
$output = New-Object System.Object
$errval = $null
$output | Add-Member -Type NoteProperty -Name InstanceName -Value $instancename
$output | Add-Member -Type NoteProperty -Name DatabaseName -Value $databasename
$output | Add-Member -Type NoteProperty -Name StartTime -Value (Get-Date)
if($sqlscript){
$output | Add-Member -Type NoteProperty -Name SQL -Value $sqlscript
$sqlout = Invoke-Sqlcmd -ServerInstance $instancename -Database $databasename -InputFile $sqlscript -ErrorVariable errval
}
else{
$output | Add-Member -Type NoteProperty -Name SQL -Value $sqlcmd
$sqlout = Invoke-Sqlcmd -ServerInstance $instancename -Database $databasename -Query $sqlcmd -ErrorVariable errval
}
$output | Add-Member -Type NoteProperty -Name EndTime -Value (Get-Date)
$output | Add-Member -Type NoteProperty -Name RunDuration -Value (New-TimeSpan -Start $output.StartTime -End $output.EndTime)
$output | Add-Member -Type NoteProperty -Name Results -Value $sqlout
$output | Add-Member -Type NoteProperty -Name Error -Value $errval
return $output
}
#Measure-SQLExecution -instancename 'localhost' -databasename 'demoPartition' -sqlcmd 'exec usp_loadpartitiondata;'
$total = @()
$total += Measure-SQLExecution -instancename 'PICARD' -databasename 'demoPartition' -sqlcmd 'truncate table dbo.orders;'
$total += Measure-SQLExecution -instancename 'PICARD' -databasename 'demoPartition' -sqlcmd 'exec usp_loadpartitiondata;'
$total += Measure-SQLExecution -instancename 'PICARD' -databasename 'demoPartition' -sqlcmd 'exec usp_fragmentpartition;'
$total
$total | Select-Object InstanceName,DatabaseName,StartTime,EndTime,SQL,RunDuration | Export-Csv -Path 'C:\Temp\ExecutionLog.csv' -NoTypeInformation
notepad 'C:\Temp\ExecutionLog.csv'
#SQLInventory example
#first, make sure database objects exist
#.\SQLInventoryObjects.sql
Invoke-Sqlcmd -ServerInstance PICARD -Database master -Query "IF EXISTS (SELECT 1 FROM sys.databases WHERE name = 'MSFADMIN') DROP DATABASE MSFADMIN;"
Invoke-Sqlcmd -ServerInstance PICARD -Database master -InputFile 'C:\IntroToPowershell\SQLInventoryObjects.sql'
#import the module
Import-Module SQLInventory -Verbose
Get-command -Module SQLInventory
#note that the -Verbose tells us the verb it's not happy about and recommends a compliant alternative
#Open the module code and look at it
#run the primary inventory collection function
$CMS='PICARD'
$servers=@((dir "SQLSERVER:\SQLRegistration\Central Management Server Group\$CMS").Name)
$servers+=$cms
Get-SQLInventory -invlist $servers -invserv 'PICARD' -invdb 'MSFADMIN'
Invoke-Sqlcmd -ServerInstance PICARD -Database MSFADMIN -Query 'SELECT * FROM dbo.InstanceInventory;' | ft
Invoke-Sqlcmd -ServerInstance PICARD -Database MSFADMIN -Query 'SELECT * FROM dbo.MachineInventory;' | ft