我有一个使用SMO获取SQL Server属性的脚本。我有脚本在html中显示属性但我无法设置它,以便即使SMO对象因连接或其他错误返回空,脚本仍然进入数据行。如果SMO对象返回空或null,我如何让它在数据行中输入字符串?
我已经尝试了一个if语句,其中$ serverObject -eq $ null并且没有打印出来。
foreach($instance in $instanceList)
{
$serverObject = New-Object Microsoft.SqlServer.Management.Smo.Server($instance)
$instance = $instance.toupper()
$serverName = $serverObject.ComputerNamePhysicalNetBIOS;
$instanceName = $serverObject.InstanceName;
$versionBuild = $serverObject.Information.ResourceVersion;
$servicePack = $serverObject.Information.ProductLevel;
$color = $redColor;
if($serverName -eq $null -and $instanceName -eq $null)
{
[string]$serverName = "Error Connecting"
$instanceName = $instance
}
else
{
if($instanceName -eq $null -and $versionBuild -eq $null)
{
[string]$instanceName = $instance
[string]$versionBuild = "Error"
}
}
}
# 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
$dataRow = "
<tr>
<td width='10%'>$serverName</td>
<td width='15%'>$instanceName</td>
<td width='5%' bgcolor=`'$color`' align='center'>$versionBuild</td>
<td width='10%' align='center'>$servicePack</td>
</tr>
"
# If statement needed to remove label that were null
If ($versionBuild -ne 'NaN')
{
Add-Content $servicePackReport $dataRow;
Write-Host -ForegroundColor DarkYellow "$serverName $instanceName service pack build = $versionBuild";
$i++
}
}
答案 0 :(得分:0)
由于您尝试检查实例是否存在,您遇到了麻烦。您进行检查,然后将变量设置为“错误”。这会导致问题,因为如果实例有效,您希望稍后检查$versionBuild
作为评估。这就是错误所在。您正在评估:
If ($versionBuild -ne 'NaN')
问题是你在比较它是否不等于字符串“NaN”。如果$versionBuild
为$null
(即实例不存在时),则不等于“NaN”,因此请写入该行。要修复它,请将其更改为:
If ($versionBuild -ne $null)
但除此之外,您可以通过检查实例是否存在来简化事情并稍微提高速度。如果是,那么 - 然后 - 拉出所有其他信息并创建数据行。
如果它不存在,那么你只是吐出一条错误信息。
foreach($instance in $instanceList)
{
$serverObject = New-Object Microsoft.SqlServer.Management.Smo.Server($instance)
$instance = $instance.toupper()
$serverName = $serverObject.ComputerNamePhysicalNetBIOS;
#Check to see if the instance exists
if($serverName -ne $null)
{
#The instance Exists, get the rest of the properties
$instanceName = $serverObject.InstanceName;
$versionBuild = $serverObject.Information.ResourceVersion;
$servicePack = $serverObject.Information.ProductLevel;
$color = $redColor;
# 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
$dataRow = "
<tr>
<td width='10%'>$serverName</td>
<td width='15%'>$instanceName</td>
<td width='5%' bgcolor=`'$color`' align='center'>$versionBuild</td>
<td width='10%' align='center'>$servicePack</td>
</tr>
"
Add-Content $servicePackReport $dataRow;
Write-Host -ForegroundColor DarkYellow "$serverName $instanceName service pack build = $versionBuild";
$i++
}
else
{
#The instance does not exist. Write error message to console
Write-Host -ForegroundColor Red "$instance Does NOT exist";
}
}