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
2 answers

How to catch only SQL timeout errors in PowerShell?

I am trying to capture only timeout errors in a script for PowerShell 5.1. The error prints out that the execution timed out, but when I print out the error name it comes back with…
1
vote
1 answer

Powershell -invokesql with character parameter

I have code that reads an excel file and searches the value in the sql database. This is a part of the code: for ($i=1; $i -le $rowMax-1; $i++) { $name = $sheet.Cells.Item($rowName+$i,$colName).text $mail =…
Nancy
  • 13
  • 2
1
vote
1 answer

How can I prevent the Powershell Invoke-sqlcmd truncating output at 80 columns?

In Powershell, Invoke-sqlcmd is truncating my output at 80 columns I've tried this code and things are still being truncated at the 80th column $ans=Invoke-Sqlcmd -ServerInstance $sqlServer -Database $MyDbName -Query $SQLSCRIPT -OutputAs DataTables…
cashonly
  • 119
  • 9
1
vote
0 answers

Executing large script file (1.7GB) on Microsoft SQL Server using SQLCMD

I have a generated script file with 1.7 GB size, I'm trying to run this script using the following sqlcmd sqlcmd -m1 -S 192.168.100.10\SQLHQ -U sa -P 123456 -i "E:/VMS2008R2.sql" When I run this line in CMD as administrator, the command take some…
Abdulsalam Elsharif
  • 4,773
  • 7
  • 32
  • 66
1
vote
1 answer

Powershell, Invoke-SqlCmd: How to get all the data from a column

I have a table that has a column containing JSON data. It can be fairly large. I want to run a query to select the JSON data from a single row, using PowerShell. The command looks like this: Invoke-Sqlcmd -ServerInstance xxxxxx -Database…
user1443098
  • 6,487
  • 5
  • 38
  • 67
1
vote
1 answer

Invoke-sqlcmd doesn't return data in rows

The Invoke-Sqlcmd doesn't return data in rows, it lists each item separately This is the code that I'm using: Invoke-Sqlcmd -ServerInstance "MyServer" -Query "SELECT * FROM [MyDB].[dbo].[MyTable]" I get something like this: Key : 81839 Type…
cashonly
  • 119
  • 9
1
vote
1 answer

Start-Job PowerShell two jobs getting created

I want to execute set of un-contained stored procedures, as jobs, to see if there are any issues in the code after making the database as contained database. After some time, I want to see whether the stored procedures are successful or not. But, I…
Venkataraman R
  • 12,181
  • 2
  • 31
  • 58
1
vote
1 answer

Invoke-sqlcmd output without headers

I know this question has already been asked but the answers don't quite fit the constrains i have So here we go again (sorry for that) : I have this line in my PowerShell script : $WITHOUTCLIENT = Invoke-Sqlcmd -h -1 -Database CM_00A -Query "Select…
Zedude
  • 11
  • 1
  • 3
1
vote
0 answers

Capturing logs for .sql statements though powershell

I'm trying to figure out a good way to run insert/select/update statements into a MSSQL database through PowerShell scripting and log it thoroughly. I have managed to use Invoke-Sqlcmd which does the inserts and select statements pretty fine. But my…
Moshin Khan
  • 153
  • 2
  • 3
  • 14
1
vote
0 answers

Invoke-Sqlcmd - how to provide a variable to SQL that contains a colon

I've used this type of coding in many PS scripts: $SQlScriptParameters = ( "FtpServer='$FtpServer'", "RotkatalogFörFTP='$RootFolderOnFtpServer'", "HostUrlForWebAuthorization='$HostUrlForWebAuthorization'", …
matsovef
  • 11
  • 1
1
vote
2 answers

Powershell - SQL query result to variable with properties

Why the stored output of SQLCMD has only Length property instead of column names?. Is it not possible to store sqlcmd output with its properties? Invoke-sqlcmd stores it correctly but Invoke-SQLcmd takes a bit longer to process so I'm trying to…
user630702
  • 2,529
  • 5
  • 35
  • 98
1
vote
1 answer

Powershell - Invoke-SQLCmd: how to check results

I am creating a script that will execute tSQLt Unit tests on the Dev server before a deployment o QA can proceed - this is part of Continuous Integration... here is my Powershell script: param( [Parameter(Mandatory=$true)][string]$DatabaseName ,…
Our Man in Bananas
  • 5,809
  • 21
  • 91
  • 148
1
vote
2 answers

invoke-sqlcmd empty variable

I am trying to perform an SQL query using Invoke-SqlCmd launching script as inputfile which contains SQLCMD variables, as $(varPROJECTNAME). For example: CREATE DATABASE [$(varPROJECTNAME)$(varDBNAME)] In such a case, i want to be able to set an…
Greg. T.
  • 33
  • 5
1
vote
3 answers

Invoke-Sqlcmd changes cases of uniqueidentifier

I'm trying to develop a script to routinely export correctly formatted CSV files as per RFC 4180. I'm using a PowerShell script to do this which invokes Sqlcmd. However, the output appears to change the case of some values in the database. For…
JimS
  • 1,123
  • 3
  • 14
  • 17
1
vote
2 answers

Passing multiple variables from csv file to invoke-sqlcmd

I'm trying to read values from a CSV file, embed them into a INSERT T-SQL statement and run that statement using Invoke-Sqlcmd. Here's my code: Push-Location; Import-Module SQLPS -DisableNameChecking; Pop-Location $InsertQry = "insert into…
paulH
  • 1,102
  • 16
  • 43