forked from MikeFal/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeasure-SQLExecution.ps1
37 lines (29 loc) · 1.89 KB
/
Measure-SQLExecution.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
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 'localhost' -databasename 'demoPartition' -sqlcmd 'exec usp_loadpartitiondata;'
$total += Measure-SQLExecution -instancename 'localhost' -databasename 'demoPartition' -sqlcmd 'exec usp_fragmentpartition;'
$total | Select-Object InstanceName,DatabaseName,StartTime,EndTime,SQL,RunDuration | Export-Csv -Path 'C:\Temp\ExecutionLog.csv' -NoTypeInformation