我得到了一個SQL運行狀況檢查報告,爲此我想添加不存在的「版本」列,編輯了代碼並能夠獲取數據但是它正在寫在列的所有行上,我只是希望它在一行上。Powershell將檢索到的數據寫入單個行 - 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
'$ sqlver = $ sqlver |選擇版本=>'$ sqlver = $ sqlver |選擇-expandproperty版本的初學者 – Matt
感謝馬特,這清除了結果中捕獲的不需要的單詞。 – Att