1: #region Functions
2:
3:
4: function Write-ScriptLog
5: {
6: param($msg,$date,$ReportOn)
7: if ($ReportOn -ne "")
8: { Add-Content -Path (Join-Path $ReportOn "SQLDmRepository_$($ServerName)_$($date).log") -Value $msg}
9: Write-Output $msg
10: }
11:
12: function Show-MessageBox ($title, $msg) {
13: [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
14: [Windows.Forms.MessageBox]::Show($msg, $title, [Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Warning, [System.Windows.Forms.MessageBoxDefaultButton]::Button1, [System.Windows.Forms.MessageBoxOptions]::DefaultDesktopOnly) | Out-Null
15: }
16:
17: function Show-InformationBox ([string]$Title,[string]$Message) {
18: [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
19: [Windows.Forms.MessageBox]::Show($Message, $Title, [Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information, [System.Windows.Forms.MessageBoxDefaultButton]::Button1, [System.Windows.Forms.MessageBoxOptions]::DefaultDesktopOnly) | Out-Null
20: }
21:
22:
23:
24: #endregion functions
25:
26: #region Script
27: Function Gather-Result {
28: param(
29: [Parameter(Position=0, Mandatory=$true)] [String]$Server,
30: [Parameter(Position=1, Mandatory=$true)] [String]$UserName,
31: [Parameter(Position=2, Mandatory=$true)] [String]$Password,
32: [Parameter(Position=3, Mandatory=$true)] [String]$Database
33: )
34:
35:
36: Get-Process "Excel*" | Stop-Process
37: # $Server = '.'
38: # $UserName = 'xx'
39: # $Password = 'xxxxx'
40: #
41: # #Variables
42: # $Database = "Database"
43:
44: #Variable to Change The Folder ##################################################################
45: $ReportOn = "c:\HealthCheck"
46: #Variable to Change The Folder ##################################################################
47:
48:
49: $ConnectionFailed = $false
50: $Date = get-date -format 'yyyy_MM_dd'
51:
52: #test path . If does not exist break the code
53: if (-not (Test-Path -Path $reporton -PathType Container)) {
54: Show-MessageBox -msg "Path $($ReportOn) does not exist" -title 'Error'
55: break
56: }
57:
58: try {
59: #Connect to SQL Server using .net
60: $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
61: $SqlConnection.ConnectionString = "Server = $($Server); Database = $($Database); User = $($Username); PWD = $($Password)"
62: $SqlConnection.Open()
63: } catch {
64: Show-MessageBox -msg "Error to connect to Server $($Server) Additional information at log file" -title 'Error Connection'
65: Write-ScriptLog "$($date) : Operation FAILED for server $($Server) Database $($Database). Error details $($error[0])" -date $($date) -reporton $($ReportOn)
66: break
67: }
68:
69: Try {
70: $Excel = New-Object -Com Excel.Application
71: #$Excel.visible = $True
72: $Excel = $Excel.Workbooks.Add()
73: $NameFile = " HealthCheck_$(get-date -format 'yyyy_MM_dd')"
74: $NameFileTest = "$($NameFile).*"
75: $path = Join-Path -Path $($ReportOn) -ChildPath $NameFile
76: $pathTest = Join-Path -Path $($ReportOn) -ChildPath $NameFileTest
77:
78: if (Test-Path $pathTest -PathType Any) {
79: try {
80: Remove-Item -Path $pathTest -Force
81: } catch {
82: Show-MessageBox -msg "The file $($NameFile) is using by another process" -title 'Error '
83: Write-ScriptLog "$($date) : Operation FAILED for server $($Server) Database $($Database). Error details $($error[0])" -date $($date) -reporton $($ReportOn)
84: break
85: }
86: }
87:
88: $Excel.saveas($path)
89:
90: $HeaderHash = @{}
91: $Itens = 1
92: $Files = Get-childitem $ReportOn -Filter "*.sql"
93:
94:
95: $Files | % {
96: $NumberOfWorkSheets = $Files.count
97:
98:
99: $SqlQuery = Get-Content $_.fullname
100: $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
101: $SqlCmd.CommandText = $SqlQuery
102: $SqlCmd.Connection = $SqlConnection
103: $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
104: $SqlAdapter.SelectCommand = $SqlCmd
105: $DataSet = New-Object System.Data.DataSet
106:
107: $SqlAdapter.Fill($DataSet) | Out-Null
108:
109: $DataExcel = $DataSet.Tables[0] | select * -ExcludeProperty 'RowError','RowState','Table','ItemArray','HasErrors'
110:
111: for ( $i = $Excel.Worksheets.count ;$i -lt $NumberOfWorkSheets; $i++) {
112: $Excel.Worksheets.add()
113: }
114: Write-Host "Processing File $($_.Name)"
115: $Name = "$($_.name) "
116: $Excel.Worksheets.Item($Itens).name = $Name.substring(0,30)
117:
118: $Sheet = $Excel.Worksheets.Item($Itens)
119: $property=@()
120:
121: For ( $i = 0 ;$i -lt $DataSet.Tables[0].columns.count ; $i++) {
122: $property += @($DataSet.Tables[0].columns[$i].columnname)
123: }
124:
125: $Column = 1
126: $Row = 1
127: foreach ($header in $property) {
128: $HeaderHash[$header] = $Column
129: $Sheet.Cells.Item($Row,$Column) = $header.toupper()
130: $Column ++
131: }
132:
133: $WorkBook = $Sheet.UsedRange
134: $WorkBook.Interior.ColorIndex = 19
135: $WorkBook.Font.ColorIndex = 11
136: $WorkBook.Font.Bold = $True
137: $WorkBook.HorizontalAlignment = -4108
138:
139: $Row ++
140: $DataExcel | % {
141: foreach ($header in $property) {
142: if ($thisColumn = $HeaderHash[$header]) {
143: $Sheet.Cells.Item($Row,$thisColumn) = [string]$_.$header
144: }
145: }
146: $Row ++
147: }
148: $Excel.save()
149: $Itens ++
150: }
151: } catch {
152: Write-ScriptLog $("$(get-date -format yyyy/MM/dd) : Operation FAILED for server $($Server) Database $($Database). Error details $($error[0])") -date $($date) -reporton $($ReportOn)
153: } Finally {
154: $SqlConnection.Close()
155: $Excel.Close()
156: Write-Host "Finished"
157: Show-MessageBox -msg "Finished !!! " -title "Information"
158: }
159:
160:
161: }
162:
163:
164: #endregion Script
165:
166: Gather-Result