查找SQL Server实例&;使用PowerShell构建service pack
我试图创建一个脚本,从.txt文档中查看服务器列表,并查找该服务器上的SQL实例以及每个实例的SQL service pack版本。脚本运行,但不返回任何信息。我认为$svcPack变量和找到所需数据的相应变量有问题。任何帮助都将非常感激。谢谢查找SQL Server实例&;使用PowerShell构建service pack,sql,sql-server,powershell,wmi,Sql,Sql Server,Powershell,Wmi,我试图创建一个脚本,从.txt文档中查看服务器列表,并查找该服务器上的SQL实例以及每个实例的SQL service pack版本。脚本运行,但不返回任何信息。我认为$svcPack变量和找到所需数据的相应变量有问题。任何帮助都将非常感激。谢谢 # Continue even if there are errors $ErrorActionPreference = "Continue"; # load the SQL SMO assembly [System.Reflection.As
# Continue even if there are errors
$ErrorActionPreference = "Continue";
# load the SQL SMO assembly
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null
# Sets the server versions
$vs2005sp4 = "9.00.5000";
$vs2008sp3 = "10.00.5500.0";
$vs2008r2sp2 = "10.50.4000.0";
# EMAIL PROPERTIES
# Set the recipients of the report.
$users = "test@test.com"
#$users = "You@company.com" # I use this for testing by uing my email address.
#$users = "you@company.com", "manager@company.com", "etc@company.com"; # can be sent to individuals.
# REPORT PROPERTIES
# Path to the report
$reportPath = "c:\Scripts\Reports\";
# Report name
$reportName = "ServicePackRpt_$(get-date -format ddMMyyyy).html";
# Path and Report name together
$servicePackReport = $reportPath + $reportName
#Set colors for table cell backgrounds
$redColor = "#FF0000"
$greenColor = "#34F01F"
$yellowColor = "#F0EC22"
$orangeColor = "#F2991D"
$whiteColor = "#FFFFFF"
# Count if any computers have low disk space. Do not send report if less than 1.
$i = 0;
# Get computer list to check disk space
$servers = Get-Content "c:\Scripts\InPutFiles\servers.txt";
$datetime = Get-Date -Format "MM-dd-yyyy_HHmmss";
# Remove the report if it has already been run today so it does not append to the existing report
If (Test-Path $servicePackReport)
{
Remove-Item $servicePackReport
}
# Cleanup old files..
$Daysback = "-7"
$CurrentDate = Get-Date;
$DateToDelete = $CurrentDate.AddDays($Daysback);
Get-ChildItem $reportPath | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item;
# Create and write HTML Header of report
$titleDate = get-date -uformat "%m-%d-%Y - %A"
$header = "
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<title>Service Pack Report</title>
<STYLE TYPE='text/css'>
<!--
td {
font-family: Tahoma;
font-size: 11px;
border-top: 1px solid #999999;
border-right: 1px solid #999999;
border-bottom: 1px solid #999999;
border-left: 1px solid #999999;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
body {
margin-left: 5px;
margin-top: 5px;
margin-right: 0px;
margin-bottom: 10px;
table {
border: thin solid #000000;
}
-->
</style>
</head>
<body>
<table width='100%'>
<tr bgcolor='#CCCCCC'>
<td colspan='7' height='25' align='center'>
<font face='tahoma' color='#003399' size='4'><strong>DTG Environment Service Pack Report for $titledate</strong></font>
</td>
</tr>
</table>
"
Add-Content $servicePackReport $header
# Create and write Table header for report
$tableHeader = "
<table width='100%'><tbody>
<tr bgcolor=#CCCCCC>
<td width='10%' align='center'>Server</td>
<td width='15%' align='center'>Instance</td>
<td width='5%' align='center'>Version Build</td>
<td width='10%' align='center'>Version Name</td>
</tr>
"
Add-Content $servicePackReport $tableHeader
# Start processing disk space reports against a list of servers
foreach($computer in $servers)
{
#$svcPacks = Get-WmiObject -ComputerName $computer -Class win32_volume | Where-object {$_.label -ne $null} | Sort-Object -property "name"
#$svcPacks = Get-WmiObject -ComputerName $computer -Class "__NAMESPACE" -namespace "root\Microsoft\SqlServer\ReportServer"
$svcPacks = New-Object -typeName Microsoft.SqlServer.Management.Smo.Server($computer)
$computer = $computer.toupper()
foreach($packs in $svcPacks)
{
#$instanceID = $packs.Instance;
$versionBuild = $packs.VersionString;
$versionName = $packs.ProductLevel;
$color = $redColor;
# Set background color to green if service pack is 2008r2 SP2
if($versionBuild -eq $vs2008r2sp2)
{
$color = $greenColor
# Set background color to yellow if service pack is 2008 SP3
if($versionBuild -eq $vs2008sp3)
{
$color = $yellowColor
# Set background color to orange if service pack is 2005 SP4
if($versionBuild -eq $vs2005sp4)
{
$color = $orangeColor
# Create table data rows
#<td width='15%' align='center'>$instanceID</td>
$dataRow = "
<tr>
<td width='10%'>$computer</td>
<td width='5%' align='center'>$versionBuild</td>
<td width='10%' align='center'>$versionName</td>
</tr>
"
# If statement needed to remove label that were null
If ($versionID -ne 'null')
{
Add-Content $servicePackReport $dataRow;
Write-Host -ForegroundColor DarkYellow "$computer $deviceID service pack build = $versionBuild";
$i++
}
}
}
}
}
}
# Create table at end of report showing legend of colors for the critical and warning
$tableDescription = "
</table><br><table width='20%'>
<tr bgcolor='White'>
<td width='10%' align='center' bgcolor='#34F01F'>SQL Server 2008 R2 with SP2 - " + $vs2008r2sp2 +"</td>
<td width='10%' align='center' bgcolor='#F0EC22'>SQL Server 2008 with SP3 - " + $vs2008sp3 +"</td>
<td width='10%' align='center' bgcolor='#F2991D'>SQL Server 2005 with SP4 - " + $vs2005sp4 +"</td>
</tr>
"
Add-Content $servicePackReport $tableDescription
Add-Content $servicePackReport "</body></html>"
# Send Notification if alert $i is greater then 0
if ($i -gt 0)
{
foreach ($user in $users)
{
Write-Host "Sending Email notification to $user"
$smtpServer = "test.com"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$msg = New-Object Net.Mail.MailMessage
$msg.To.Add($user)
$msg.From = "NoReply@test.com"
$msg.Subject = "Environment Service Pack Report for $titledate"
$msg.IsBodyHTML = $true
$msg.Body = get-content $servicePackReport
$smtp.Send($msg)
$body = ""
}
}
#即使有错误也要继续
$ErrorActionPreference=“继续”;
#加载SQL SMO程序集
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.SMO”)| Out Null
[Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.ConnectionInfo”)| Out Null
[Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.SmoEnum”)| Out Null
[Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.Smo”)| Out Null
#设置服务器版本
$vs2005sp4=“9.00.5000”;
$vs2008sp3=“10.00.5500.0”;
$vs2008r2sp2=“10.50.4000.0”;
#电子邮件属性
#设置报告的收件人。
$users=”test@test.com"
#$users=”You@company.com“#我通过输入电子邮件地址来进行测试。
#$users=”you@company.com", "manager@company.com", "etc@company.com"; # 可以发送给个人。
#报告属性
#报告的路径
$reportPath=“c:\Scripts\Reports\”;
#报告名称
$reportName=“ServicePackRpt_$(获取日期-格式ddMMyyyy).html”;
#路径和报告名称一起
$servicePackReport=$reportPath+$reportName
#设置表格单元格背景的颜色
$redColor=“#FF0000”
$greenColor=“#34F01F”
$yellowColor=“#F0EC22”
$orangeColor=“#F2991D”
$whiteColor=“#FFFFFF”
#如果任何计算机的磁盘空间不足,请计数。如果小于1,则不发送报告。
$i=0;
#获取计算机列表以检查磁盘空间
$servers=获取内容“c:\Scripts\InPutFiles\servers.txt”;
$datetime=获取日期-格式为“MM-dd-yyyy\U HHmmss”;
#如果报表已在今天运行,请删除该报表,这样它就不会附加到现有报表中
If(测试路径$servicePackReport)
{
删除项目$servicePackReport
}
#清理旧文件。。
$Daysback=“-7”
$CurrentDate=获取日期;
$DateToDelete=$CurrentDate.AddDays($Daysback);
获取ChildItem$reportPath |其中对象{$\ LastWriteTime-lt$DatetoDelete}|删除项;
#创建和编写报告的HTML标题
$titleDate=获取日期-uformat“%m-%d-%Y-%A”
$header=”
服务包报告
DTG环境服务包$titledate报告
"
添加内容$servicePackReport$标头
#为报表创建和写入表头
$tableHeader=”
服务器
例子
版本构建
版本名
"
添加内容$servicePackReport$tableHeader
#开始根据服务器列表处理磁盘空间报告
foreach($服务器中的计算机)
{
#$svcPacks=Get wmioobject-ComputerName$computer-Class win32_volume | Where object{$\uu.label-ne$null}| Sort object-property“name”
#$svcPacks=Get WmiObject-ComputerName$computer-Class“\uu命名空间”-命名空间“root\Microsoft\SqlServer\ReportServer”
$svcPacks=New Object-typeName Microsoft.SqlServer.Management.Smo.Server($computer)
$computer=$computer.toupper()
foreach($svcPacks中的packs)
{
#$instanceID=$packs.Instance;
$versionBuild=$packs.VersionString;
$versionName=$packs.ProductLevel;
$color=$redColor;
#如果service pack为2008r2 SP2,则将背景色设置为绿色
if($versionBuild-eq$vs2008r2sp2)
{
$color=$greenColor
#如果service pack为2008 SP3,则将背景色设置为黄色
if($versionBuild-eq$vs2008sp3)
{
$color=$yellowColor
#如果service pack为2005 SP4,则将背景色设置为橙色
if($versionBuild-eq$vs2005sp4)
{
$color=$orangeColor
#创建表数据行
#$instanceID
$dataRow=”
$computer
$versionBuild
$versionName
"
#If语句需要删除空的标签
如果($versionID-ne'null')
{
添加内容$servicePackReport$dataRow;
写入主机-ForegroundColor DarkYellow“$computer$deviceID service pack build=$versionBuild”;
$i++
}
}
}
}
}
}
#在报告末尾创建表格,显示关键和警告的颜色图例
$tableDescription=”
SQL Server 2008 R2与SP2-“+$vs2008r2sp2+”
带SP3的SQL Server 2008-“+$vs2008sp3+”
SQL Server 2005与SP4-“+$vs2005sp4+”
"
添加内容$servicePackReport$tableDescription
添加内容$servicePackReport“”
#如果警报$i大于0,则发送通知
如果($i-gt 0)
{
foreach($users中的用户)
{
写入主机“向$user发送电子邮件通知”
$smtpServer=“test.com”
$smtp=新对象Net.Mail.SmtpClient($smtpServer)
$msg=新对象Net.Mail.MailMessage
$msg.To.Add($user)
$msg.From=”NoReply@test.com"
$msg.Subject=“环境服务包$titledate报告”
$msg.IsBodyHTML=$true
$msg.Body=获取内容$servicePackReport
$smtp.Send($msg)
$body=“”
}
}
从表面上看,这是一个小错误。在评估$VersionBuild时,您不需要
if($versionBuild -eq $vs2008r2sp2)
# Sets the server versions
$vs2005sp4 = "9.00.5000";
$vs2008sp3 = "10.00.5500";
$vs2008r2sp2 = "10.50.4000";
#...
# Set background color to green if service pack is 2008r2 SP2
if($versionBuild -match $vs2008r2sp2)
{
$color = $greenColor
}
else
{
# Set background color to yellow if service pack is 2008 SP3
if($versionBuild -match $vs2008sp3)
{
$color = $yellowColor
}
else
{
# Set background color to orange if service pack is 2005 SP4
if($versionBuild -match $vs2005sp4)
{
$color = $orangeColor
# Create table data rows
#<td width='15%' align='center'>$instanceID</td>
$dataRow = "
<tr>
<td width='10%'>$computer</td>
<td width='5%' align='center'>$versionBuild</td>
<td width='10%' align='center'>$versionName</td>
</tr>
"
}
else
{
# If statement needed to remove label that were null
If ($versionID -ne 'null')
{
Add-Content $servicePackReport $dataRow;
Write-Host -ForegroundColor DarkYellow "$computer $deviceID service pack build = $versionBuild";
$i++
}
}
}
}
$svcPacks = New-Object -typeName Microsoft.SqlServer.Management.Smo.Server($computer)