Questions tagged [invoke-sqlcmd]

PowerShell cmdlet that runs a script containing statements from the languages (Transact-SQL and XQuery) and commands supported by the SQL Server sqlcmd utility.

For MicroSoft's official documentation on this cmdlet see: https://msdn.microsoft.com/en-GB/library/cc281720.aspx

Alternative options to this command exist. For a discussion of these see: https://dba.stackexchange.com/questions/17435/which-powershell-technique-should-i-use-to-talk-to-sql-server

148 questions
33
votes
2 answers

'Invoke-Sqlcmd' is not recognized as the name of a cmdlet

We have recently started using SQL Server 2012 SP3 and building the SQL server 2012 using a PowerShell script. There is a requirement in our automation process to run multiple database scripts on a db and I have found Invoke-Sqlcmd very reliable…
Gau
  • 431
  • 1
  • 5
  • 4
32
votes
5 answers

Unable to load adalsql.dll error when calling `Invoke-sqlcmd`

I have VS2015 with SSDT installed, along with SSMS and the SqlServer PowerShell module (which includes the invoke-sqlcmd comand), and yet If I try to execute a query against an Azure SQL Data Warehouse like so: invoke-sqlcmd -Query "Select top 5…
Jthorpe
  • 9,756
  • 2
  • 49
  • 64
13
votes
4 answers

Escape variable in sqlcmd / Invoke-SqlCmd

I am using powershell and using Invoke-SqlCmd. I am able to pass variables to SQL: $variables = @( "MyVariable='hello'" ) Invoke-SqlCmd ` -ServerInstance 'localhost' ` -Database 'master' ` -Username 'matthew' ` -Password 'qwerty'…
Matthew
  • 24,703
  • 9
  • 76
  • 110
11
votes
5 answers

Invoke-Sqlcmd unable to run

I have a PowerShell script that checks the CPU level of the server it is running on, and then if it is above a certain threshold it will run a SQL stored procedure and e-mail. The script runs correctly on my development server with the latest…
Ricky
  • 377
  • 2
  • 5
  • 16
7
votes
1 answer

PowerShell: invoke-sqlcmd with Get-Credential doesn't work

I've never seen a so easy script failing so royally: $SQLServer = "localhost" $cred = Get-Credential invoke-sqlcmd -ServerInstance $SQLServer -Credential $cred -Query "select @@version" the phrase says -Credentials is not recognized: Invoke-Sqlcmd…
Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113
6
votes
3 answers

SQL installed but powershell not picking up sqlps commands (like invoke-sqlcmd)

I installed ms sql server with chocolatey: choco install SQLServer2012DeveloperEditionWithSP1 -y -f -source 'http://choco.developers.tcpl.ca/chocolatey' -c "$env:WINDIR\temp" SQL seems to be installed and working well outside of powershell where it…
Chris
  • 169
  • 1
  • 2
  • 14
5
votes
3 answers

Invoke-Sqlcmd : Duplicate column names are not permitted in SQL PowerShell

I've spitted Glenn Berry's Performance Query in 14 queries but one of them (number 9) is not working. This is my PowerShell code: #Provide SQLServerName $SQLServer ="localhost" #Provide Database Name $DatabaseName ="master" #Prompt for user…
Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113
5
votes
3 answers

Invoke-sqlcmd output without "quotes" and headers

Output example: #TYPE System.Data.DataRow <--- "PersonalNr" <--- "00001005" "00001008" "00001009" "00001013" "00001019" "00001024" Requirements: I want a output without the 2 first lines and without the quote symbols, how can i do that? For the…
Moek
  • 99
  • 1
  • 1
  • 8
4
votes
1 answer

Error passing non-literal scripting variables to Invoke-Sqlcmd in PowerShell

I am trying to pass in some scripting variables to Invoke-Sqlcmd in PowerShell like so: $hello = "hello" $params = "greeting=" + $hello, "audience=world" Invoke-Sqlcmd -Query "select '`$(greeting) `$(audience)'" -Variable $params I get the…
Joel Christophel
  • 2,604
  • 4
  • 30
  • 49
4
votes
2 answers

Invoke-Sqlcmd with account that uses MFA in Azure

Anyone of you knows an option to execute a SQL command using PowerShell with Azure AD account that has MFA enabled? What is an alternative? Do I need to create a service principal for that? I had no luck but only found this cmdlet,…
borys86
  • 1,894
  • 2
  • 13
  • 13
4
votes
3 answers

Return Value from powershell to pipeline inside pipeline

In Jenkins Pipeline i want return a value from powershell to pipeline but i dont know how Example: pipeline { agent any stages { stage('Return Value') { steps { parameters([ …
4
votes
3 answers

How to execute SQL in a PowerShell script as part of a Visual Studio Team Services build task?

How to execute SQL against an Azure SQL database as part of a PowerShell script in a VSTS build task in a hosted agent? There are many tasks available for build definitions: we can execute a PowerShell script or deploy SQL using DACPAC or…
4
votes
1 answer

How do you access an SQL Server sequence from PowerShell with invoke-sqlcmd?

In my sql server 12.0 I have created a sequence: CREATE SEQUENCE [dbo].[SESSIONI_dv_seq] AS [bigint] START WITH 1 INCREMENT BY 1 MINVALUE -9223372036854775808 MAXVALUE 9223372036854775807 CYCLE CACHE; sql server management studio works…
edoardo
  • 61
  • 4
3
votes
1 answer

Powershell's Export-CSV command not including header (column names) when there are no results

I have a CSV file that is used as a resource by other files. It is expected that this CSV file will always have a header row at the very minimum which specifies the expected columns. Not having this header row causes errors in the other files. In my…
3
votes
3 answers

PowerShell - Invoke-Sqlcmd results to Export-Csv set batch row limit

I'm exporting about 1,000,000 rows via an Invoke-Sqlcmd script and it exports into a csv around 180MB. What I would like to do is be able to export it instead into 10 100,000 row files. How do I go about doing this as it doesn't seem to be a…
whoisearth
  • 4,080
  • 13
  • 62
  • 130
1
2 3
9 10