Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 September SQL Server: Use PowerShell Invoke-SQLCMD for T-SQL

SQL Server: Use PowerShell Invoke-SQLCMD for T-SQL

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In the previous articles, I have shared basic about the SQL Server PowerShell (sqlps) and how we can navigate SQL Server instance data using PowerShell.

SQL Server 2012: Working with the PowerShell Environment

In the SQLPS module, there are different commands available to extensively work with SQL Server.
The cmdlet command type contains most of the commands of SQLPS.

The Invoke-SQLCMD is also one command to execute T-SQL in PowerShell.
Here, I am sharing some of the examples of Invoke-SQLCMD to execute T-SQL in PowerShell.

First, list of all commands of SQLPS:

1
Get-command –module SQLPS

SQL Server PowerShell Get Modules

Check SQL Server Version:

1
Invoke-SQLCMD "select @@version;"

SQL Server SELECT Version

Check Default database name:

1
Invoke-SQLCMD "select db_name();"

SQL Server PowerShell Select DB Name

Execute SELECT statement:

1
Invoke-SQLCMD -Query "SELECT *FROM dbo.tbl_TestSnapshot;" –Database "dbrnd"

SQL Server PowerShell Select Table

Execute SELECT with PowerShell Variable:

1
2
$MyVar = "Anvesh"
Invoke-SQLCMD -Query "SELECT *FROM dbo.tbl_TestSnapshot WHERE Name = '$($MyVar)';" –Database "dbrnd"

SQL Server PowerShell Variable

Sep 17, 2016Anvesh Patel
SQL Server: The sqlps PowerShell for easy Database File NavigationSQL Server 2012: All Database and Log backups using PowerShell
Comments: 1
  1. kiquenet
    November 15, 2018 at 7:55 am

    Modules:
    SQLPS: This module is included with the SQL Server installation (for backwards compatibility), but is no longer being updated. The most up-to-date PowerShell module is the SqlServer module.
    SqlServer: This module includes new cmdlets to support the latest SQL features. The module also contains updated versions of the cmdlets in SQLPS.
    Invoke-SQLCMD not supports:
    # ## – Block of Code required to build SQL Authentication:
    # $MySQL.ConnectionContext.LoginSecure = $false;
    # $MySQL.ConnectionContext.set_Login($SQLUserName);
    # $SqlUserPwd = ConvertTo-SecureString $sqlPwd -AsPlainText -Force;
    # $MySQL.ConnectionContext.set_SecurePassword($SqlUserPwd);

    In Windows 10, I try Import-Module -name SQLPS, I get error: not found assembly ‘Microsoft.SqlServer.ConnectionInfo, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91’.
    Maybe I have not latest versión SQLPS. Anyways, SQLPS is no longer being updated.

Anvesh Patel
Anvesh Patel

Database Engineer

September 17, 2016 SQL ServerAnvesh Patel, database, database research and development, dbrnd, Invoke-SQLCMD, PowerShell, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....