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
0
votes
0 answers

SQLCMD powershell script with data taken from a file unable to output the expected number

I have a csv file which has one column named PurchaseorderID and some numbers below it. I have a PS script which is supposed to fetch each row from that file and complete a SQL query and run it. However, when I run below script:- $csv = Import-Csv…
0
votes
1 answer

Should I use PowerShell subexpression when Invoke-Sqlcmd in Azure runbook?

We are using Azure Automation powershell runbook for Azure Sql Maintenance similar as it’s described in https://www.2azure.nl/2020/07/28/how-to-use-azure-automation-to-maintain-sql-indexes-and-statistics/ (and in Azure Automation Powersell Runbook…
Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
0
votes
1 answer

Script generated by Update-Database has syntax errors while a normal Update-Database works correctly

I have a .NET Framework 4.8 project with using Entity Framework 6.2.0 with many migrations. My problem is, when I remove my database from my local computer and run Update-Database the database is created successfully, but when I run Update-Database…
0
votes
2 answers

Japanese Characters are not recognized when using Invoke-SqlCmd -Query

I'm inserting data from a json file to an SQL server table using Invoke-SqlCmd and using a stored procedure as following: Invoke-SqlCmd -ServerInstance $servername -Database $database -Query "EXEC dbo.InsertDataFromJson @JSON='$json' The json is…
riwik
  • 41
  • 2
0
votes
1 answer

Invoke-Sqlcmd : Could not find stored procedure

I have a dump of a SQL database table which contains only data. It is one long list of INSERT statements. The file is about 10GB and when I try to import with Invoke-Sqlcmd or the SQL Server management studio it fails with the message "Not enough…
Timmy10000
  • 29
  • 2
0
votes
0 answers

Try/Catch Using Invoke-SQLCmd in PowerShell Not Working

Help! I'm trying to gather a bunch of information from a number of different SQL servers. I need to first verify if we can connect to the server (uses port 1433). If that doesn't work, I just want to output that the port is not open. If it does,…
Christopher Cass
  • 817
  • 4
  • 19
  • 31
0
votes
1 answer

rolling back datbabase changes with sqlcmd utility

I have a release pipeline which applies database changes with 'SqlCmd.exe'. I am trying to execute a stored procedure using this command-line utility: /opt/mssql-tools/bin/sqlcmd -S tcp:$(Server) -d $(Database) -U $(UserName) -P '$(Password)' -b -i…
0
votes
1 answer

How To Use Windows User For SQLCMD?

I need to Use Windows User for SQLCMD. C:\Users\Administrator>sqlcmd -S myserver\myinstance -U mydomain\myuser -P *** This Command Not Correct . How Can I figured This? Windows Authentication Is OK! Password is Correct! Windows User Added To SQL…
MAhmadi
  • 11
  • 4
0
votes
1 answer

How to use Invoke-Sqlcmd from inside SSMS?

I run the query below (from inside powershell), from different servers and all is fine: Invoke-Sqlcmd -Query " SELECT [ServerName]=@@servername,m.Match_id, m.HostfamilyId, hf.NIEFlag, ra.DS2019Sent FROM APIA_Repl_pub.dbo.repl_HostFamily hf INNER…
0
votes
1 answer

Cannot execute SQL script using invoke-sqlcmd

I'm trying to automate executing SQL scripts using Invoke-SQLCmd powershell command. As a source I have a one script generated by SSDT Schema Compare (4000 lines, all inside transaction). I'm triggering this script in the following…
Gerard Larwa
  • 101
  • 5
0
votes
0 answers

Invoke-Sqlcmd and Export-Csv consume huge amounts of memory?

Currently, we're exporting csv file from SQL Server by using Invoke-Sqlcmd and Export-Csv commands in PowerShell. To upload to Redshift, we needs to enclose the data by double-quotations and convert to UTF8 encoding. So we considered the combination…
Sachiko
  • 808
  • 1
  • 12
  • 31
0
votes
1 answer

How do you format XML output properly from Powershell using invoke-sqlcmd

Below is my PowerShell script that connects to a remote SQL server and stored the result into a XML file. $SQLResult = Invoke-Sqlcmd -inputfile $inputfile -ServerInstance $ServerInstance -Database $Database -Username $Username -Password $Password…
David
  • 63
  • 5
0
votes
1 answer

"Given path's format is not supported" when using relative paths in Powershell

I have a Powershell script which I have used successfully for some time but only recently it has stopped liking the relative paths that are setup. Any idea why or how this might happen? The location of the script I am trying to call is…
Alex
  • 3
  • 2
0
votes
2 answers

SqlCmd command execution stops

Iam new to sqlcmd and i'm trying to execute this sql cmd code: :Connect SERVERNAME !!if exist $(FullBackup) del $(FullBackup) GO !!if exist $(TransactionLog) del $(TransactionLog) GO I am passing variables $(FullBackup) and $(TransactionLog)…
Aditya Nair
  • 514
  • 1
  • 9
  • 18
0
votes
1 answer

How to process multiple columns from sql in powershell

I have this PowerShell script: $QUERY = "SELECT name FROM sys.databases"; $Databases = invoke-sqlcmd -serverinstance "SQLInstanceName" -database "master" -Query $QUERY foreach ($dbname in $Databases) { $dbname } Let's say I want the…
Ranga N
  • 19
  • 3