2017-03-16 54 views
0

我得到了一個SQL運行狀況檢查報告,爲此我想添加不存在的「版本」列,編輯了代碼並能夠獲取數據但是它正在寫在列的所有行上,我只是希望它在一行上。Powershell將檢索到的數據寫入單個行 - SQL

這是我做得到的版本

$sqlver = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "$instance1" 
$sqlver= $sqlver | select version 

輸出的圖像是在這裏: output

使用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

'$ sqlver = $ sqlver |選擇版本=>'$ sqlver = $ sqlver |選擇-expandproperty版本的初學者 – Matt

+0

感謝馬特,這清除了結果中捕獲的不需要的單詞。 – Att

回答

0

如果你想選擇一個特定的列則該列應該是首先應該是存在於SQL的查詢輸出。

然後,您可以使用選擇對象進行選擇。如果您可以發佈整個腳本,以便我們可以看到如何過濾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 
+0

感謝Ranadip,不確定我是否可以合併你的建議,因爲如果我沒有錯,腳本在一個循環下獲取所有細節,我想在這裏分享腳本,但它很長時間不適合評論,嘗試編輯帖子添加腳本,但它弄得一團糟。 更新 - 用腳本更新了帖子。 – Att

+0

@Att:把腳本放到github或pastebin中,你可以給我們提供相同的鏈接。那麼我們可以看看它。 –

+0

我在帖子中分享了腳本,希望它在正確的格式下。 – Att

0

最後我得到了答案,

我不得不宣佈它是這樣的:

$ V = $空(表示列)

如果($ v $ -eq NULL){$ sqlver = $ sqlver}其他{$ sqlver = $空}

$ V ++

對身體哪裏有

$ I ++

$ S(加入這個)++

$ V ++

該訣竅。感謝Matt清理結果,Ranadip花時間研究結果。欣賞它。

+0

雅說得好。 –