Skip to main content

Connect with Microsoft SQL Databases using PowerShell

The Invoke-Sqlcmd cmdlet runs a script containing the languages and commands supported by the SQL Server SQLCMD utility. Invoke-SqlCmd is versatile because it can be used on its own or from within the SQL PowerShell Provider. When used within the SQL Server Provider it is possible to leave off some of the parameters, depending on where you have navigated. When navigating an instance, you can leave off the -ServerInstance parameter.

Here, we will use this powershell cmdlet to fetch data from SQL Server database. Let's assume, we have table [StudentDetails] as given below:

sqlcmdlet

Before running this script, make sure you have sqlserver module installed

sqlcmdlet3

To install sqlserver module use: Install-Module -Name sqlserver

Run the below powershell command:
$sqlserver= "sqlserver name"
$sqldb="database name"
$sqluser="SQL Username"
$sqlpass="SQL Password"
$sqlquery = "Select * from StudentDetails"
$result = Invoke-Sqlcmd -ServerInstance $sqlserver -Database $sqldb -Query $sqlquery -Username $sqluser -Password $sqlpass
$result

sqlcmdlet2

Refer this for more information related to Invoke-Sqlcmd cmdlet : https://docs.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd?view=sqlserver-ps

I hope you found this useful. Happy scripting. :)



Comments

Popular posts from this blog

Comparison between Azure Application Gateway V1 and V2

Microsoft has announced new version of Azure Application Gateway and its Web Application Firewall module (WAF). In this article, we will discuss about the enhancements and new highlights that are available in the new SKUs i.e. Standard_v2 and WAF_v2. Enhancements and new features: Scalability: It allows you to perform scaling of the number of instances on the traffic. Static VIP: The VIP assigned to the Application Gateway can be static which will not change over its lifecycle. Header Rewrite: It allows you to add, remove or update HTTP request and response headers on application gateway. Zone redundancy: It enables application gateway to survive zonal failures which allows increasing the resilience of applications. Improved Performance: Improvement in performance during the provisioning and during the configuration update activities. Cost: V2 SKU may work out to be overall cheaper for you relative to V1 SKU. For more information, refer Microsoft p...

Install Solr as an Azure App Service

After Sitecore 9.0.2, Solr is a supported search technology for Sitecore Azure PAAS deployments. In this article, we will install SOLR service 8.4.0 in Azure App Service for Sitecore 10. 1. Create Azure App Service Login to Azure and create Azure App service. Make sure Runtime stack should be Java. 2. Download Solr Download Solr 8.4.0 from https://archive.apache.org/dist/lucene/solr/ Extract the files and add the below web.config file in the Solr package. <?xml version="1.0" encoding="UTF-8"?> <configuration>  <system.webServer>      <handlers>      <add  name="httpPlatformHandler"            path="*"            verb="*"            modules="httpPlatformHandler"            resourceType="Uns...

Upgrade to Standard SKU public IP addresses in Azure by 30 September 2025—Basic SKU will be retired

Azure service updates Upgrade to Standard SKU public IP addresses in Azure by 30 September 2025—Basic SKU will be retired Basic SKU public IP addresses will be retired. Click here for more information.

Secure your web applications using Azure WAF

In this session, I have covered how to deploy WAF in Azure Application Gateway & Azure Front door to secure your web applications. I will also cover features of WAF, different rulesets, logging mechanism of Azure WAF. Refer the below YouTube video on how to configure WAF in Azure subscription.

Security Recommendations for Azure SQL Database

In this article, we will cover the security recommendations that you should follow for establishing a secure baseline configuration for Microsoft Azure SQL Services on your Azure Subscription. Image Source:  https://docs.microsoft.com/en-us/azure/azure-sql/database/security-overview 1. Enable auditing on SQL Servers & SQL databases: The Azure platform allows you to create a SQL server as a service. Enabling auditing at the server level ensures that all existing and newly created databases on the SQL server instance are audited. Auditing tracks database events and writes them to an audit log in your Azure storage account. It also helps you to maintain regulatory compliance, understand database activity, and gain insight into discrepancies and anomalies that could indicate business concerns or suspected security violations. Steps: For Azure SQL Server: 1. Go to Azure SQL Server and click on Auditing . 2. Enable Azure SQL Auditing and select your Storage account. You can also sele...