Tuesday, June 17, 2014

Power Shell did not recognize invoke-sqlcmd on SQL Server 2008 R2 box for Power Shell/SQL scripts

OR
-How to load SQL Server Snapins into PowerShell Session. for SQL Server 2008 R2 box?
-It not required if it is SQL Server 2012 and above.

PowerShell run/execution issue for SQL Server 2012 box ps/scripts on SQL Server 2008 R2 box.
Developed powershell/sql scripts in SQL Server 2012 env box and moved/rollout to  SQL Server 2008 env box. The ps scripts got failed and got below error.

Error: "The term 'invoke-sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program..."
       "The specified module 'sqlps' was not loaded because no valid module file was found in any module directory."

Background:
The SQLPS module is Powershell module introduced in SQL Server 2012. Prior to the 2012 release Powershell support in SQL Server was provided by a mini-shell i.e. sqlps.exe introduced in SQL 2008 with some minor enhancements in SQL Server 2008 R2.

Execution options on different SQL Server version:

SQL Server 2012: If install sql server 2012, default sqlps module and comdlets will be installed. Can use below script and execute on SQL Server 2012 box.

TestPShell.ps1
Import-Module SQLPS -DisableNameChecking
invoke-sqlcmd -Servername ServerName -inputFile "C:\scripts\Test.sql" | out-File -filepath "C:\scripts\TestOutput.txt"

SQL Server 2008 R2: If install sql server 2008 R2 on box, then "Import-Module SQLPS" can not work due to sqlps module not available directly. In-order to work sqlps on sql server 2008 R2 box, add below code inplace "Import-Module SQLPS" for above "TestPShell.ps1" PS script. Then invoke-sqlcmd will work.

TestPShell.ps1
#Import-Module SQLPS -DisableNameChecking ----->This is not requied if it is SQL Server 2008 box(powershell).
if ( (Get-PSSnapin -Name sqlserverprovidersnapin100 -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PsSnapin sqlserverprovidersnapin100
}

if ( (Get-PSSnapin -Name sqlservercmdletsnapin100 -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PsSnapin sqlservercmdletsnapin100
}

invoke-sqlcmd -Servername ServerName -inputFile "C:\scripts\Test.sql" | out-File -filepath "C:\scripts\TestOutput.txt"

Note: Load the following 2 snapin's and execute ps/sql scripts using invoke-sqlcmd command.
SqlServerCmdletSnapin100
SqlServerProviderSnapin100




Monday, June 9, 2014

Always ON Availability Group (AAG) configuration on Cluster(Active/Passive) env servers and standalone servers


SQL Server 2012 or higher versions:  Always on Availability Group (AAG) can configure in 2 scenario's:
1.Cluster (Active/Passive)(Not supports Automatic failover)
2.Standalone Servers (Supports Automatic failover)

1.Cluster (Active/Passive) Env Servers:
 (This does not support automatic failover and it supports manual failover)

Pre-req:
1.Windows Server Faiover Cluster(WSFC) services need to install on both nodes (Windows Admin responsibility)
1.1 Then configure  the quorum ( add the nodes in quorum)
2.Make sure test the validation report for storage,cluster servers .....etc.
    If any issue then contact windows admin
3.IP addresses required for
    Node1 IP address
    Node2 IP address
    Windows Cluster IP
     SQL Server Virtual IP Address
    Listener Name and IP Address (for Always On configure)
    Optional :   MSDTC IP( if you use distributed transactions then it required)
   Note: If you use 3 rd server then submit for Node3 IP address
 4.Use AD group to install the SQL Server Instance(Best practice)
     or can use any Windows Admin group user to install
SQL Installation:
5. High level steps
   5.1 Install SQL Server on Node1
   5.2 Then add on Node2 to install SQL Server
   5.3 Reboot the both nodes
6.Configure Always On
    Create Availability group and add replicas.
    use Always On Listener Name/IP address

2.Standalone Servers: (This supports automatic failover(3rd node also required) and  manual failover(node1/node2 required) )
Pre-req:
1.Install basic Windows Server Failover Cluster(WSFC) services on both nodes (Windows Admin responsibility)
1.1 Then configure  the quorum(add the nodes in quorum)
2.Make sure test the validation report for storage,cluster servers .....etc.
    If any issue then contact windows admin
3.IP addresses required for
    Node1 IP address
    Node2 IP address
    Windows Cluster IP
    Listener Name/IP Address(for Always On configure)
    Optional :   MSDTC IP( if you use distributed transactions then it required)
     Note: If you use 3 rd server then submit for Node3 IP address for automatic failover
 4.Use AD group to install the SQL Server(Best practice)
     or can use any Windows Admin group user to install
SQL Installation:
5. High level steps
   5.1 Node1: Install SQL Server on Node1
   5.2 Node2: Install SQL Server on Node2
   5.3 Node3: This is required for automatic failover purpose. if you  use Node and File share or other options ...on Node3 then SQL Server installation not required.
Node3 is for just automatic failover purpose(voting purpose) only. If it manual failover then node2/node2 required.
Note:"Search in "quorum configuration" options(4 options and select any option to configure the quorum voting)
6. Reboot the all 3 nodes(node1/node2/node3)
7.Configure Always On
    Create Availability group and add replicas.
    use Always On Listener Name/IP address


Issues while create AlwaysON Availability Group:
Note: Before you create a Windows failover cluster, you have to check whether the domain user account that is running the Create Cluster Wizard has the "Create Computer Objects"permission.
Solution: http://support.microsoft.com/kb/2829783)
or
Check  "Steps for configuring the account for the person who installs the cluster" step below link.
http://technet.microsoft.com/en-us/library/cc731002(WS.10).aspx


SQL Server Cluster(Active/Passive) installation high level steps

SQL Server Cluster(Active/Passive) installation steps:

Pre-req:
1.Windows Cluster Services installed on both nodes (Windows Admin responsibility)
2.Make sure test the validation report for storage,cluster servers .....etc.
    If any issue then contact windows admin
3.IP addresses required for
    Node1 IP address
    Node2 IP address
    Windows Cluster IP
    SQL Server Virtual IP ( this could be used Cluster Network  Configuration step)
     (unmark the DHCP column and fill the address column with the value of this IP)
    optional :   MSDTC IP( if you use distributed transactions then it required)
 4.Use AD group to install the SQL Server
Install:
5. High level steps
   5.1 Install SQL Server on Node1
   5.2 Then add on Node2 to install SQL Server
   5.3 Reboot the both nodes    

Install SQLCMD/BCP and ODBC 11 drivers


1.SQLCMD
Microsoft Command Line Utilities 11 for SQL Server:
    The SQLCMD utility allows users to connect to, send Transact-SQL batches from, and output rowset information from SQL Server instances
http://www.microsoft.com/en-us/download/details.aspx?id=36433

1.Down load the "c" from above link
2.Run below to install or save in text as install.bat.Then double click on install.bat.
msiexec /quiet /passive /qn /i msodbcsql.msi IACCEPTMSODBCSQLLICENSETERMS=YES ADDLOCAL=ALL /log C:\testlogs.txt
3.Installation complete.

2.ODBC install:http://www.microsoft.com/en-us/download/details.aspx?id=36434
1.Download the "msodbcsql.msi" file from above
2.Run below to install or save in text as install.bat.Then double click on install.bat.
msiexec /quiet /passive /qn /i msodbcsql.msi IACCEPTMSSQLCMDLNUTILSLICENSETERMS=YES /log C:\testlogs.txt