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
1
vote
1 answer

Invoke-Sqlcmd : The type initializer for 'Microsoft.Data.SqlClient.InOutOfProcHelper' threw an exception

I'm trying to execute SQL Query from Powershell. My Script: Invoke-Sqlcmd -Query "select 'Y' from sys.databases" -ServerInstance . Error : Invoke-Sqlcmd : The type initializer for 'Microsoft.Data.SqlClient.InOutOfProcHelper' threw an exception. At…
Prakash
  • 23
  • 4
1
vote
0 answers

Invoke-Sqlcmd variable format is invalid with Powershell, variable value contains equals sign

I took an example from the document to add the variable of Invoke-Sqlcmd $StringArray = "MYVAR1='String1'", "MYVAR2='String2'" Invoke-Sqlcmd -Query "SELECT `$(MYVAR1) AS Var1, `$(MYVAR2) AS Var2" -Variable $StringArray Var1 Var2 ---- …
Tan
  • 149
  • 1
  • 9
1
vote
0 answers

Invoke-Sqlcmd Command Script running in PS 5 works but failing under PS 7.3.1

I am getting the following error: The term 'Invoke-Sqlcmd' is not recognized as a name of a cmdlet, function, script file, or executable program. | Check the spelling of the name, or if a path was included, verify that the path is correct and try…
1
vote
1 answer

How to invoke-sqlcmd and export WITH headers and dashes/hyphens but tab-delimited

Say I run this: $HOME_DATABASE = 'mydb' $params = @{ 'Database' = $HOME_DATABASE 'ServerInstance' = 'mydb' 'Username' = 'myuser' 'Password' = 'mypass' 'Query' = 'select * from sometable' } $queryresults = Invoke-Sqlcmd…
mbourgon
  • 1,286
  • 2
  • 17
  • 35
1
vote
1 answer

Powershell: How to pick 1 result of many from Invoke-Sqlcmd Select statement

When I run a SELECT statement using Invoke-Sqlcmd, I'd like to choose 1 out of X results to move forward with a loop. In the example below, I have multiple results for 'Bob' and I'd like to choose the appropriate one to continue working with the…
Ebeeze
  • 25
  • 4
1
vote
1 answer

Several invokes to GUI from one thread

If I run several Invokes from a thread, does it run one after each other? For example, I have a ListView control and after getting the actual information from a server, I need to clear the items, update group names, add new items, etc. All this…
Dennis
  • 47
  • 6
1
vote
0 answers

Executing PowerShell script file that contains Invoke-Sqlcmd from C# form application

I have ps1 script that contains Invoke-Sqlcmd command to execute some sql queries. I am executing this ps1 file from the c# form application with the below code: var ps1File = @executeChangeScriptPs1FilePath; ProcessStartInfo startInfo = new…
nrllhcinar
  • 109
  • 5
1
vote
2 answers

Powershell Invoke-sqlcmd In-script Variable override

I am using the following: Powershell Command: Invoke-Sqlcmd -AbortOnError -InputFile "C:\FullPath\SQLQuery.sql" -ServerInstance WPU8V9011532 -Variable "PREF = 'NeededVal'" SQLQuery.sql file content: :setvar PREF "InternalVal" USE $(PREF)MyDB Select…
1
vote
1 answer

Run SQL files against a remote machine using powershell

What is the best way to execute the SQl scripts(ddl,sql) through powershell. I'm using the below commandline to execute the scripts. Invoke-Sqlcmd -InputFile "C:\filepath\file1.ddl" -ServerInstance "10.166.23.28" -Database "DB1" -Username "sa"…
mystack
  • 4,910
  • 10
  • 44
  • 75
1
vote
2 answers

Powershell, invoke-sqlcmd, export-csv fails to show data if there is more than one result

I have the below code to get data from a SQL DB and export it into a CSV file: #Server and Database names $SQLServer = "Servername" $DB = "DatabaseName" #SQL Command $FullScriptSQL="Select * from MyTable WHERE Column = 'TestData'" #Invoke the…
Chris
  • 219
  • 5
  • 18
1
vote
1 answer

Can an Azure SQL server be unavailable / unreachable for a period of time after creation?

I am creating an Azure SQL server & db via a terraform task within an Azure Devops Pipeline using hosted agents. The resources create without issue. However, the next job in my stage tries to run a db_init.sql script against the newly created db…
1
vote
1 answer

Powershell Invoke-SQLcmd Query argument inconsistent?

I do apologies if this has been asked before. At the time I wrote this I was unable to find this behavior addressed on this site. When using Invoke-SQLcmd in powershell I get inconsistencies. I receive SQL connection arguments at the scripts…
TheL0grus
  • 99
  • 4
1
vote
2 answers

How to Compare two SQL tables and get the difference value using Powershell

I have two tables in a SQL Server database. Table1 looks like: ServerName ServiceName ServiceStatus ---------- ----------- ------------- Server1 iis running Server2 sql running Server3 www …
ravi kanth
  • 11
  • 2
1
vote
1 answer

Using Get-AzureStorageFileContent to execute SQL script from Azure Storage file share

I am using Invoke-Sqlcmd to pick up -InputFile from an Azure Storage File share. I am using the Get-AzureStorageFileContent to get the file from Azure Storage but getting the error below. Any help or insight is…
cenko
  • 43
  • 7
1
vote
0 answers

How to stop Invoke-Sqlcmd from truncating query

One of our tables has very very long records in a particular column. These queries are used infrequently, but are necessary. I have been using sqlcmd utility in bash like so: -- getfile.sql SET NOCOUNT ON; -- Disable message for next USE…
Frankie
  • 11,508
  • 5
  • 53
  • 60
1 2
3
9 10