Install SQL Native Client with PowerShell

Few days ago, I had a task to check if SQL Native Client version 11 was installed on some server in our environment. Of course, I could have logged in to all of my servers and check manually, but why I should do that if I could achieve the same thing with PowerShell. And it would be faster and simpler.

Just a little bit of a background first: SQL Native Client 11 is installed when you install SQL Sever 2016. It could be installed separately, without installing SQL Server 2016, it is included with SQL Server 2012 Feature Pack. Also, you could download only SQL Native Client 11 .msi install file, from this web page:
SQL Native Client 11 download page

I went to Installed Programs and verified that SQL Native Client 11 was not installed:

SQL_Native_Slient_11_Not_Installed

Also, I have checked the registry -> HKLM:\Software\Microsoft – there was not SQL Native Client 11 installed:

SQL_Native_Slient_11_Registry_Not_Installed

In order to install SQL Native Client 11 with PowerShell, I will check the registry for the installation. If it is not there, I will start msiexec.msi and install SQL Native Client 11 silently.

So first, lets search the registry for the installation:

$checkClient = Get-ChildItem 'HKLM:\Software\Microsoft\*' -ea SilentlyContinue | Where {$_.name -like '*Client*'}

This will give us the list of all registry keys which are located under HKLM:\Software\Microsoft location.

It will look something like this:

Get-ChildItem_SQL_Native_Client_11

As you may see, we have a list of keys which contain ‘Client’ in its name, but none of those are SQL Native Client 11.

Now, we have to go thru this list and make sure that in case that there is no SQL Native Client 11 present, we could proceed with the installation.

I have split all the names into sections, using Split():

$checkClient.name.Split('\')

The result looks like this:

Split_SQL_Native_Client_11

Now that we have the names, we could do if loop and check if there is ‘SQL Native Client 11’ installed:

if ($checkClient.name.Split('\') -eq 'Microsoft SQL Server Native Client 11.0')

If there is SQL Native Client 11 installed, that is fine, we can finish because we have already installed. But in case we don’t have it installed, we could move forward and start the installation.

I have downloaded SQL Native Client 11, and have placed it in C:\temp\SQLNativeClient11.

We will use msiexec.msi tool for the installation of SQL Native Client 11.

The parameters we will use are:

  • /i — install
  • /qn — quiet installation
  • IACCEPTSQLNCLILICENSETERMS=YES — will accept license without asking us (needed for the silent install)
  • /L*V C:\temp\SQLNativeClient11\sqlNativeClientInstall.log — capture the log and save it
$client11Install = msiexec.exe /qn /i sqlncli.msi IACCEPTSQLNCLILICENSETERMS=YES /L*V C:\temp\SQLNativeClient11\sqlNativeClientInstall.log

In the end, we will check if the SQL Native Client 11 is installed, by doing almost the exactly the same thing like at the beginning, checking the registry.

$client11 = $false
$checkClient = Get-ChildItem 'HKLM:\Software\Microsoft\*' -ea SilentlyContinue | Where {$_.name -like '*Client*'}
if ($checkClient.name.Split('\') -eq 'Microsoft SQL Server Native Client 11.0')
  {
   Write-Host 'SQL Native Client 11.0 has been already installed'
   $client11 = $True
   } else {
      Write-Host 'Version 11 not present'
      $client11 = $false
    }
if ($client11 -eq $false)
{
 try {
   Write-Host 'Installing Native Client 11'
   cd C:\temp\SQLNativeClient11
   $client11Install = msiexec.exe /qn /i sqlncli.msi IACCEPTSQLNCLILICENSETERMS=YES /L*V C:\temp\SQLNativeClient11\sqlNativeClientInstall.log
  } Catch {
       Write-Host 'SQL Native Client 11 was not installed. Manual action required'
     }
  }
Start-Sleep 15
#Double check if Native Client 11 is installed
$checkClient = Get-ChildItem 'HKLM:\Software\Microsoft\*' -ea SilentlyContinue | Where {$_.name -like '*Client*'}
if ($checkClient.name.Split('\') -eq 'Microsoft SQL Server Native Client 11.0')
  {
    Write-Host 'SQL Native Client 11.0 has been successfully installed'
    $client11 = $True
  } else {
    Write-Host 'Version 11 is still not present after attempted installation. Manual action required.'
    $client11 = $false
}

So, that is it. It was really a small piece of code, but you could use this together with other installations, updates, and so on.