Well, from the looks of it, it's a small mistake. When evaluating your $VersionBuild, you don't have any else statements. So if it fails the first evaluation:
if($versionBuild -eq $vs2008r2sp2)
Then it won't continue, and evaluate if it could be the other versions.
The other thing I would do is instead of using -eq
in your if statements, use -match
and shorten your version string. That way if your version string has a better chance of matching if the version string doesn't match exactly. e.g. You were trying to check the version $vs2005sp4 = "9.00.5000";
but the VersionString property is going to return "9.00.5000.00"
which if you use -eq
doesn't work, but if you use -match
it will work.
So, adding in the else statements, shortening the version strings, and using -match
your code block will look something like this:
# 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++
}
}
}
}
-- Edit
To answer @user1700796's question, I think that the real issue that @user1700796's encountering is that this script will not work against SQL servers that have multiple instances. i.e. when you get the Smo.Server object:
$svcPacks = New-Object -typeName Microsoft.SqlServer.Management.Smo.Server($computer)
Where $computer
is a server name, and on that server there is multiple instances; a single Smo.Server object is returned, which is essentially empty. i.e. pulling the VersionString returns an empty string.
If instead, you run the same command, except you put in the full SQL server name + the instance name, for each instance, then the Smo.Server object returns the correct information.
This is because the Smo.Server object uses a SQL connection string to connect to the SQL server (see the ConnectionContext
property), and it can only connect to a single instance at a time, and does not enumerate through your instances as you might have been expecting.
So, instead of using the foreach loop on the Smo.Server object, you need to:
- First use a couple of commands to take the computer name and enumerate the names of the SQL server instances on that box
- For ex. See: PowerShell list all SQL instances on my system
- Take those SQL server instances, and then do a foreach loop on those names
- You use those SQL Server instance names to get your Smo.Server object
- Then you can then pull the version string from the Smo.Server object.