[玩转系统] SQL 数据库报告已修订
作者:精品下载站 日期:2024-12-14 07:46:11 浏览:12 分类:玩电脑
SQL 数据库报告已修订
去年我写了一篇文章,解释了如何使用 SQLSERVER PSDrive 创建 HTML 报告,突出显示一些服务器和数据库信息。如果您想复习一下,可以在这里找到该文章。简而言之,您可以在客户端桌面上安装 SQL Server PowerShell 模块并使用它来管理远程服务器。
在我本周教授的 PowerShell 课程中,我添加了一些有关 SQL cmdlet 的内容,并有机会重新访问原始脚本。尽管原始版本旨在与远程服务器一起使用,但我有一些错误阻止了这种情况的发生。我还决定添加一些附加信息以及更多条件格式。例如,根据剩余的可用空间量,报告可能会将值格式化为黄色以指示警告,或以红色格式化以指示更严重的情况。
在 SQLSERVER PSdrive 中,数据库目录默认不显示任何系统数据库,除非您使用 -Force。
这是修改后的脚本:
#requires -version 4.0
#requires -module SQLPS
<#
Create a SQL Server database report
This script includes a graphic file which by default should be in the
same directory as this script. Otherwise, edit the $ImageFile variable.
It is recommended that you import the SQLPS module first and then run
this script.
Usage:
c:\scripts\SQLServerReport -computername CHI-SQL01 -includesystem -path c:\work\CHI-SQL01-DB.htm
#>
[cmdletbinding()]
Param(
[Parameter(Position=0,HelpMessage="Enter the name of a SQL server")]
[ValidateNotNullorEmpty()]
[Alias("CN")]
[string]$computername=$env:computername,
[Parameter(Position=1,HelpMessage="Enter the named instance such as Default")]
[ValidateNotNullorEmpty()]
[string]$Instance = "Default",
[switch]$IncludeSystem,
[ValidateNotNullorEmpty()]
[string]$Path="$env:temp\sqlrpt.htm",
[switch]$ShowReport
)
$scriptversion = "2.0"
Write-Verbose "Starting $($MyInvocation.Mycommand)"
#define the path to the graphic
$graphic = "db.png"
#the default location is the same directory as this script
$imagefile = Join-path -path (split-path $MyInvocation.InvocationName) -ChildPath $graphic
#define an empty array to hold all of the HTML fragments
$fragments=@("<br><br><br>")
#get uptime
Write-Verbose "Getting SQL Server uptime"
Try {
#try to connect to the SQL server
$starttime = Invoke-Sqlcmd -Query 'SELECT sqlserver_start_time AS StartTime FROM sys.dm_os_sys_info' -ServerInstance $computername -database master -ErrorAction Stop
}
Catch {
Write-warning "Can't connect to $computername. $($_.exception.message)"
#bail out
Return
}
Write-Verbose "Getting SQL Version"
$version = Invoke-Sqlcmd "Select @@version AS Version,@@ServerName AS Name" -ServerInstance $computername
#create an object
$uptime = New-Object -TypeName PSObject -Property @{
StartTime = $starttime.Item(0)
Uptime = (Get-Date)-$starttime.Item(0)
Version = $version.Item(0).replace("`n","|")
}
$tmp = $uptime | ConvertTo-HTML -fragment -AS List
#replace "|" place holder with <br>"
$fragments += $tmp.replace("|","<br>")
#get services
Write-Verbose "Querying SQL services"
$services = Get-Service -DisplayName *SQL* -ComputerName $computername |
Select Name,Displayname,Status
#add conditional formatting to display stopped services in yellow
[xml]$html = $services | ConvertTo-Html -fragment
#check each row, skipping the TH header row
for ($i=1;$i -le $html.table.tr.count-1;$i++) {
$class = $html.CreateAttribute("class")
#check the value of the last column and assign a class to the row
if ($html.table.tr[$i].td[-1] -ne 'Running') {
$html.table.tr[$i].lastChild.setAttribute("class","warn") | Out-Null
}
} #for
$fragments += "<h3>SQL Services</h3>"
$fragments += $html.InnerXML
#get database information
#path to databases
$dbpath = "SQLServer:\SQL$computername$instance\databases"
Write-Verbose "Querying database information from $dbpath"
if ($IncludeSystem) {
Write-Verbose "Including system databases"
$dbs = Get-ChildItem -path $dbpath -Force
} else {
$dbs = Get-ChildItem -path $dbpath
}
[xml]$html = $dbs | Select Name,
@{Name="SizeMB";Expression={$_.size}},
@{Name="DataSpaceMB";Expression={$_.DataSpaceUsage}},
@{Name="AvailableMB";Expression={$_.SpaceAvailable}},
@{Name="PercentFree";Expression={ [math]::Round((($_.SpaceAvailable/1kb)/$_.size)*100,2) }} |
Sort PercentFree | ConvertTo-HTML -fragment
for ($i=1;$i -le $html.table.tr.count-1;$i++) {
$class = $html.CreateAttribute("class")
#check the value of the last column and assign a class to the row
if (($html.table.tr[$i].td[-1] -as [double]) -le 15) {
$html.table.tr[$i].lastChild.SetAttribute("class","danger") | Out-Null
}
elseif (($html.table.tr[$i].td[-1] -as [double]) -le 25) {
$class.value = "warn"
$html.table.tr[$i].lastChild.SetAttribute("class","warn") | Out-Null
}
}
$fragments += "<h3>Database Utilization</h3>"
$fragments += $html.InnerXml
$fragments += "<h3>Database Backup</h3>"
$fragments += $dbs | Select Name,Owner,CreateDate,Last*,RecoveryModel | ConvertTo-Html -Fragment
#volume usage
Write-Verbose "Querying system volumes"
$data = Get-CimInstance win32_volume -filter "drivetype=3" -ComputerName $computername
$drives = foreach ($item in $data) {
$prophash = [ordered]@{
Drive = $item.DriveLetter
Volume = $item.DeviceID
Compressed = $item.Compressed
SizeGB = $item.capacity/1GB -as [int]
FreeGB = "{0:N4}" -f ($item.Freespace/1GB )
PercentFree = [math]::Round((($item.Freespace/$item.capacity) * 100),2)
}
#create a new object from the property hash
New-Object PSObject -Property $prophash
}
[xml]$html = $drives | ConvertTo-Html -fragment
#check each row, skipping the TH header row
for ($i=1;$i -le $html.table.tr.count-1;$i++) {
$class = $html.CreateAttribute("class")
#check the value of the last column and assign a class to the row
if (($html.table.tr[$i].td[-1] -as [int]) -le 25) {
$html.table.tr[$i].lastChild.SetAttribute("class","danger") | Out-Null
}
elseif (($html.table.tr[$i].td[-1] -as [int]) -le 35) {
$class.value = "warn"
$html.table.tr[$i].lastChild.SetAttribute("class","warn") | Out-Null
}
}
$fragments += "<h3>Volume Utilization</h3>"
$fragments += $html.innerxml
#define the HTML style
Write-Verbose "preparing report"
#encode the graphic file to embed into the HTML
$ImageBits = [Convert]::ToBase64String((Get-Content $imagefile -Encoding Byte))
$ImageHTML = "<img src=data:image/png;base64,$($ImageBits) alt='db utilization'/>"
#define a here string for the html header
$head = @"
<style>
body { background-color:#FAFAFA;
font-family:Arial;
font-size:12pt; }
td, th { border:1px solid black;
border-collapse:collapse; }
th { color:white;
background-color:black; }
table, tr, td, th { padding: 2px; margin: 0px }
tr:nth-child(odd) {background-color: lightgray}
table { margin-left:50px; }
img
{
float:left;
margin: 0px 25px;
}
.danger {background-color: red}
.warn {background-color: yellow}
</style>
$imagehtml
<br><br><br>
<H2>SQL Server Report: $($version.name)</H2>
<br>
"@
#HTML to display at the end of the report
$footer = @"
<br>
<i>
Date : $(Get-Date)<br>
Author : $env:USERDOMAIN$env:username<br>
Version: $scriptVersion<br>
</i>
"@
#create the HTML document
ConvertTo-HTML -Head $head -Body $fragments -PostContent $footer |
Out-File -FilePath $path -Encoding ascii
if ($ShowReport) {
#open the finished report
Write-Verbose "Opening report $path"
Invoke-Item $path
}
Write-Verbose "Ending $($MyInvocation.Mycommand)"
我的脚本包含一个图形文件以使其美观。你可以下载我的图形文件db.png。将图形放在与脚本相同的目录中。或者修改脚本以使用您自己的图形。然后您可以运行这样的命令来生成 HTML 报告。
c:\scripts\sqlserverreport.ps1 -computername chi-sql01 -IncludeSystem -path s:\chi-sql01.htm -verbose
此脚本缺少备用凭据的规定,并假设您正在运行的帐户具有必要的 SQL 权限。完成后您可以获得这样的报告。
我希望您能让我知道您的想法以及您是否认为这有用。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag