concept database information in category sql
appears as: database information, database information

This is an excerpt from Manning's book SQL Server MVP Deep Dives Vol. 2.
Now that the job information has been recorded in the spreadsheet, let’s examine each database attached to the SQL Server instance. The following listing creates the column header for the database information.
Listing 8. Creating the row headers for database information
$row++ $row++; #Create column headers for the databases in this instance $Sheet.Cells.Item($row,1) = "DATABASE NAME"; $Sheet.Cells.Item($row,2) = "COLLATION"; $Sheet.Cells.Item($row,3) = "COMPATIBILITY LEVEL"; $Sheet.Cells.Item($row,4) = "AUTOSHRINK"; $Sheet.Cells.Item($row,5) = "RECOVERY MODEL"; $Sheet.Cells.Item($row,6) = "SIZE (MB)"; $Sheet.Cells.Item($row,7) = "SPACE AVAILABLE (MB)"; $Sheet.Cells.Item($row,8) = "AUTOCLOSE"; $Sheet.Cells.Item($row,9) = "CREATEDATE"; $Sheet.Cells.Item($row,10) = "AUTOCREATESTATISTICSENABLED"; $Sheet.Cells.Item($row,11) = "AUTOUPDATESTATISTICSENABLED"; $Sheet.Cells.Item($row,12) = "DATASPACEUSAGE (MB)"; $Sheet.Cells.Item($row,13) = "INDEXSPACEUSAGE (MB)"; $Sheet.Cells.Item($row,14) = "LASTBACKUPDATE"; $Sheet.Cells.Item($row,15) = "LASTDIFFERENTIALBACKUPDATE"; $Sheet.Cells.Item($row,16) = "LASTLOGBACKUPDATE"; $Sheet.Cells.Item($row,17) = "LOGFILES"; $Sheet.Cells.Item($row,18) = "LOGSIZE"; $Sheet.Cells.Item($row,19) = "LOGLOCATION"; $Sheet.Cells.Item($row,20) = "OWNER"; $Sheet.Cells.Item($row,21) = "PRIMARYFILEPATH"; $Sheet.Cells.Item($row,22) = "TABLES"; $Sheet.Cells.Item($row,23) = "VIEWS"; $Sheet.Cells.Item($row,24) = "PROCEDURES"; $Sheet.Cells.Item($row,25) = "FUNCTIONS"; $Sheet.Cells.Item($row,26) = "TRIGGERS"; $Sheet.Cells.Item($row,27) = "STATUS"; $Sheet.Cells.Item($row,28) = "PAGEVERIFYCHECKSUM"; #Format the database column headers for ($col = 1; $col -le 28; $col++){ $Sheet.Cells.Item($row,$col).Font.Bold = $True; $Sheet.Cells.Item($row,$col).Interior.ColorIndex = 48; $Sheet.Cells.Item($row,$col).Font.ColorIndex = 34; } $row++;To gather database information from the SQL Server instance, you’ll employ the Databases collection of the Server object. You’ll iterate through each database attached to the instance using a foreach flow control construct and examine its properties. This next listing demonstrates this technique.
Listing 9. Collecting database information
$dbs = $server.Databases; #Examine each database in the collection foreach ($db in $dbs) { #Perform some calculations and format the results $dbSpaceAvailable = $db.SpaceAvailable/1KB; $dbDataSpaceUsage = $db.DataSpaceUsage/1KB; $dbIndexSpaceUsage= $db.IndexSpaceUsage/1KB; $dbSpaceAvailable = "{0:N3}" -f $dbSpaceAvailable; $Sheet.Cells.Item($row, 1) = $db.Name; $Sheet.Cells.Item($row, 2) = $db.Collation; $Sheet.Cells.Item($row, 3) = $db.CompatibilityLevel; $Sheet.Cells.Item($row, 4) = $db.AutoShrink; switch ($db.AutoShrink){ "False" {$Sheet.Cells.item($row, 4).Interior.ColorIndex = 0} "True" {$Sheet.Cells.item($row, 4).Interior.ColorIndex = 3} } $Sheet.Cells.Item($row, 5) = $db.RecoveryModel.ToString(); $Sheet.Cells.Item($row, 6) = "{0:N3}" -f $db.Size; if ($dbSpaceAvailable -lt 1.00){ $fgColor = 3; } else{ $fgColor = 0; } $Sheet.Cells.Item($row, 7) = $dbSpaceAvailable ; $Sheet.Cells.item($intRow, 7).Interior.ColorIndex = $fgColor; $Sheet.Cells.Item($row, 8) = $db.AUTOCLOSE ; switch ($db.AutoClose){ "False" {$Sheet.Cells.item($row, 8).Interior.ColorIndex = 0} "True" {$Sheet.Cells.item($row, 8).Interior.ColorIndex = 3} } $Sheet.Cells.Item($row, 9) = $db.CREATEDATE; $Sheet.Cells.Item($row, 10) = $db.AUTOCREATESTATISTICSENABLED; switch ($db.AUTOCREATESTATISTICSENABLED){ "True" {$Sheet.Cells.item($row, 10).Interior.ColorIndex = 0} "False" {$Sheet.Cells.item($row, 10).Interior.ColorIndex = 3} } $Sheet.Cells.Item($row, 11) = $db.AUTOUPDATESTATISTICSENABLED; switch ($db.AUTOUPDATESTATISTICSENABLED){ "True" {$Sheet.Cells.item($row, 11).Interior.ColorIndex = 0} "False" {$Sheet.Cells.item($row, 11).Interior.ColorIndex = 3} } $Sheet.Cells.Item($row,12) = $dbDataSpaceUsage; $Sheet.Cells.Item($row,13) = $dbIndexSpaceUsage; $Sheet.Cells.Item($row,14) = $db.LastBackupDate; if (((get-date) - $db.LastBackupDate).days -gt 1) { $fgColor = 3; } else { $fgColor = 0; } $Sheet.Cells.Item($intRow,14) = $db.LastBackupDate; $Sheet.Cells.item($intRow,14).Interior.ColorIndex = $fgColor; $Sheet.Cells.Item($row,15) = $db.LASTDIFFERENTIALBACKUPDATE; if (($db.RecoveryModel -ne 3) -and ($db.LASTLOGBACKUPDATE –eq "12:00:00 AM")) { $fgColor = 3; } else{ $fgColor = 0; } $Sheet.Cells.Item($row,16) = $db.LASTLOGBACKUPDATE; $Sheet.Cells.item($row,16).Interior.ColorIndex = $fgColor; $i = 0; $logFileSize = 0; $logFileName = ""; $logs = $db.LogFiles; foreach ($log in $logs) { $i++; $logName = $log.Name; $logFileName = $log.FileName; $logFileSize += $log.Size; } $Sheet.Cells.Item($row,17) = $i; $Sheet.Cells.Item($row,18) = $logFileSize; if($i -eq 1) { $Sheet.Cells.Item($row,19) = $logFileName; } else { $Sheet.Cells.Item($row,19) = "(multiple)"; } $Sheet.Cells.Item($row,20) = $db.OWNER; switch ($db.Owner){ "sa" {$Sheet.Cells.item($row, 20).Interior.ColorIndex = 0} default {$Sheet.Cells.item($row, 20).Interior.ColorIndex = 3} } $dbpath = $db.PRIMARYFILEPATH; if($logFileName.SubString(0, 3) -eq $dbpath.SubString(0,3)) { $fgColor = 3; } else { $fgColor = 0; } $Sheet.Cells.Item($row,21) = $dbpath; $Sheet.Cells.item($row,21).Interior.ColorIndex = $fgColor; $Sheet.Cells.Item($row,22) = $db.Tables.Count; $Sheet.Cells.Item($row,23) = $db.Views.Count; $Sheet.Cells.Item($row,24) = $db.StoredProcedures.Count; $Sheet.Cells.Item($row,25) = $db.UserDefinedFunctions.Count; $Sheet.Cells.Item($row,26) = $db.Triggers.Count; $Sheet.Cells.Item($row,27) = $db.Status; $Sheet.Cells.Item($row,28) = $db.pageVerify; switch ($db.Owner){ "Checksum" {$Sheet.Cells.item($row, 28).Interior.ColorIndex = 0} default {$Sheet.Cells.item($row, 28).Interior.ColorIndex = 3} } $row++; }