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:
Before running this script, make sure you have sqlserver module installed
To install sqlserver module use: Install-Module -Name sqlserver
Run the below powershell command:
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. :)
Here, we will use this powershell cmdlet to fetch data from SQL Server database. Let's assume, we have table [StudentDetails] as given below:
Before running this script, make sure you have sqlserver module installed
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
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
Post a Comment
Thanks for your comment. In case of any concerns, please contact me at er.ashishsharma@outlook.com