我收到了一份SQL运行状况检查报告,为此我想要添加“版本”列,而不是那里,我编辑了代码并且能够获取数据,但它正在写入列的所有行,我只想把它放在一排。
这就是我为获取版本所做的工作
$sqlver = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "$instance1"
$sqlver= $sqlver | select version
刚开始使用PowerShell,任何方向都将受到赞赏。
完整脚本:
$Fbkp=48
$Dbkp=24
$i=$null
$s=$null
$Servers = Get-Content "serverlist location"
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$bodyM = "<table cellpadding=3 cellspacing=1 bgcolor=#FF8F2F style='font-family:verdana; font-size:7pt;'>"
$bodyM += "<tr bgcolor=#DDDDDD><TH>InstanceName</TH><TH>Status</TH>"
$body = "<table cellpadding=3 cellspacing=1 bgcolor=#FF8F2F style='font-family:verdana; font-size:7pt;'>"
$body += "<tr bgcolor=#DDDDDD><TH>ServerName</TH><TH>Instance Name</TH><TH>Version</TH><TH>SQL Server Agent</TH><TH>Uptime</TH><TH>Databases</TH><TH>Status</Th><TH>Size(MB)</TH><TH>DataSpaceUsage(KB)</Th><TH>SpaceAvailable(KB)</TH><TH>RecoveryModel</TH><TH>Full Backup</TH><TH>Differential Backup</TH><TH>Log Backup</TH></tr>"
ForEach ($Server in $Servers) {
$s=$null
Try{
Get-WmiObject -computer $server win32_service -ErrorAction Stop >.\tmp.txt
$instances = Get-WmiObject -ComputerName $server win32_service | where {(($_.name -eq "MSSQLSERVER") -or ($_.name -like "MSSQL$*"))} | select-object name
}catch { $ErrorMessage = $_.Exception.Message ; $errrun11=11
$body += "<tr bgcolor=white align=center><td><b>$server</b></td><TD colspan=12 align=left><font color=red>$ErrorMessage</font></TD></tr>"
}
If ($errrun11 -ne "11") {
foreach ($instance in $instances) {
$Instname="$($instance.name)"
if ($instance.name -like "MSSQLSERVER") { $instance1=$server} else {
$instance = $($instance.name).Split("$")
$instance1="$server\$($instance[1])"
$Instname="$($instance[1])"
}
$instance1
try {
$srv = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "$instance1"
$SJruns=$srv.JobServer.Jobs | Where-Object {$_.CurrentRunStatus -eq "running"} | Select Name,LastRunDate,nextrundate,CurrentRunStatus
$dbstest=$srv.Databases
$dbstest >tmp.txt
} catch {$ErrorMessage = $_.Exception.Message; $runerr=7
If ($s -eq $null) {$Srnm=$server} else {$Srnm=$null}
$s++
$body += "<tr bgcolor=white align=center><td><b>$Srnm</b></td><td><b>$instance1</b></td><TD colspan=11 align=left><font color=red>$ErrorMessage</font></TD></tr>"
}
If ($runerr -ne 7) {
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$i=$null
$SQLServer=$null
$SQLServer = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $instance1
try{ Foreach($Database in $SQLServer.Databases){ break}} catch {$SQLServer=$null}
if (!$SQLServer) { $body += "<tr bgcolor=#D3D3D3 align=center><TD colspan=10 align=center><font color=red><B>$instance</B></font></TD></tr>" } else {
#$body += "<tr bgcolor=#D3D3D3 align=center><TD colspan=10 align=center><B>$Server</B></TD></tr>"
$UpTime=$null
if($SQLServer.Databases['tempdb']) {
$udb = $SQLServer.Databases['tempdb']
$CreateDate = $udb.CreateDate
$Time = (Get-Date) – $CreateDate
$UpTime ="$($Time.DAYS) days $($Time.HOURS) hrs $($Time.MINUTES) mins $($Time.SECONDS) sec"
}
Foreach($Database in $SQLServer.Databases)
{
$out=$Database.LastBackupDate
$outt=((Get-Date) - $out).TotalHours
$out1=$Database.LastDifferentialBackupDate
$outt1=((Get-Date) - $out1).TotalHours
$out2=$Database.LastLogBackupDate
$outt2=((Get-Date) - $out2).TotalHours
if($Database.LastBackupDate -eq "01/01/0001 00:00:00") {$LFBD="No Full Backup"}
elseif ( $outt -gt '48' -and ($Database.Name -NotContains ('master','model','msdb','tempdb') ) ) {$LFBD="<font color=red >$out</font>"}
else { $LFBD=$out}
if($Database.LastDifferentialBackupDate -eq "01/01/0001 00:00:00") { $LDBD ="No Diff Backup"}
elseif ( $outt1 -gt '24') {$LDBD="<font color=red >$out1</font>"}
else { $LDBD=$out1}
if($Database.LastLogBackupDate -eq "01/01/0001 00:00:00") { $LLBD ="No Log Backup"}
elseif($Database.RecoveryModel -Match "Simple"){$LLBD ="N/a"}
elseif ( $outt2 -gt '24') {$LLBD="<font color=red >$out2</font>"}
else { $LLBD=$out2}
$sqlver = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "$instance1"
$sqlver= $sqlver | select version
$Sqlagt = Get-WmiObject -ComputerName $server win32_service | where {($_.displayname -like "SQL Server Agent*") -and ($_.displayname -like "*($instname)*")}
If ($($Sqlagt.state) -eq "Running") {$AGTsts="<font color=green>$($Sqlagt.state)</font>"} else { $AGTsts="<font color=red>$($Sqlagt.state)</font>"}
If ($($Database.Status) -like "*offline*") {$DB1sts="<font color=red>$($Database.Status)</font>"}
elseIf ($($Database.Status) -like "*normal*") {$DB1sts="<font color=green>Online</font>"}
else { $DB1sts="$($Database.Status)"}
If ($s -eq $null) {$Srnm=$server} else {$Srnm=$null}
If ($i -eq $null) {$Instnm=$instance1; $Agtst=$Agtsts; $upt=$uptime} else {$Instnm=$Agtst=$upt=$null}
$i++
$s++
$body += "<tr bgcolor=white align=left><TD><b>$srnm</b></TD><TD><b>$Instnm</b></TD><TD><b>$sqlver</b></TD><TD>$Agtst</TD><TD>$upt</TD><TD><b>$($Database.Name)</b></TD><TD>$DB1sts</TD></TD><TD>$($Database.Size)</TD><TD>$($Database.DataSpaceUsage)</TD><TD>$($Database.SpaceAvailable)</TD><TD>$($Database.RecoveryModel)</TD><TD>$LFBD</TD><TD>$LDBD</TD><TD>$LLBD</TD></TR>"
$i++
$s++
}
}
} else {$runerr=$ErrorMessage=$null}#If ($runerr -ne 7)
}
} else {$errrun11=$null} #If ($errrun11 -ne "11")
}
$body += "</Table>"
$msgBody = "<table cellpadding=3 cellspacing=1 bgcolor=#FF8F2F>"
$msgBody += "<tr align=center>"
$msgBody += "<td bgcolor=#DDDDDD><FONT face=Verdana size=1.5 ><b>SQL Server(s) Health Report</b></font></td>"
$msgBody += "</tr>"
$msgBody += "<tr>"
$msgBody += "<td bgcolor=white><FONT face=Verdana size=1.5 >$body</font></td>"
$msgBody += "</tr>"
$msgBody += "</table>"
$date= get-date -format dd-MM-yyyy
$File = "outputfile.htm"
If ((Test-Path $File) -eq $true){ Remove-Item $File }
Add-Content -Path $File -Value $msgBody
"Created file - $File"
exit
go
答案 0 :(得分:0)
如果要选择特定的列,那么该列应首先出现在sql的查询输出中。
然后您可以使用 Select-Object 选择它。如果您可以发布整个脚本以便我们可以看到您如何过滤HTML报告中的列,那会更好。
要获得SQL版本,您可以尝试这样:
$connectionString ="server=blrvdilities02;uid=sa; pwd=nextgen;Integrated Security=False;"
$connection = New-Object System.Data.SqlClient.SqlConnection -ErrorAction Stop
$connection.ConnectionString = $connectionString
try{
$connection.Open()
$locationquery = 'select @@Version as Version'
$locationcommand = $connection.CreateCommand()
$locationcommand.CommandText = $locationquery
$locationresult = $locationcommand.ExecuteReader()
$locationdt = new-object "System.Data.DataTable"
$locationdt.Load($locationresult)
$version = $locationdt.Version
}
catch{
$version=$_.Exception.Message;
}
Write-Output $version
答案 1 :(得分:0)
最后我得到了答案,
我必须这样声明:
$ v = $ null(表示列)
if($ v -eq $ null){$ sqlver = $ sqlver} else {$ sqlver = $ null}
$ V ++
在身体上
$ I ++
$ S ++
(加上这个)
$ V ++
这就是诀窍。感谢Matt清除结果和Ranadip花时间调查结果。欣赏它。