A N

April 7, 2016

Create ODBC Connection with Powershell

Filed under: Tips — donrsh @ 8:55 am

Create ODBC Connection with Powershell.


<#
	 Created on:   	2016-04-07 
	 Created by:   	Arash Nabi
	 Email: 	    arash@nabi.nu 

#>


<#
	
    .SYNOPSIS
		Create ODBC Connection
	
	.DESCRIPTION
		Create ODBC Connection
	
	.PARAMETER SQLserver
		Provide a SQLserver with instance name. Ex SQLserver\InstanceName
	
	.PARAMETER Databases
		A description of the Databases parameter.
	
	.PARAMETER DsnType
		Data Source Name type
	
	.PARAMETER Platform
		X86 or X64 platform
	
	.PARAMETER DriverName
		An ODBC driver allows ODBC-enabled programs to get information from ODBC data sources.
	
	.PARAMETER Name
		A description of the Name parameter.
	
	.EXAMPLE
		PS C:\>  Create-ODBC -SQLserver SQLserver\InstanceName -Databases DBname -DsnType System -Platform 64-bit -Verbose
	
	.NOTES
		Additional information about the function.
#>
function Create-ODBC
{
	[CmdletBinding()]
	param
	(
		$SQLserver,
		$Databases,
		[ValidateSet('System', 'User')]
		$DsnType,
		[ValidateSet('64-bit', '32-bit')]
		$Platform
	)
	
	try
	{
		foreach ($Database in $Databases)
		{
			Add-OdbcDsn -Name $Database -DriverName "SQL Server Native Client 11.0" -DsnType $DsnType -Platform $Platform -SetPropertyValue @("Server=$SQLserver", "Trusted_Connection=Yes", "Database=$Database") -ErrorAction SilentlyContinue -PassThru
		}
	}
	catch [System.IO.IOException] {
		$_.Exception.Message
	}
	
	finally
	{
		Write-Verbose -Message "End of script"
	}
}



Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: