SQL BCP within Powershell hangs - sql

I am new to BCP. I am tying to launch bcp.exe utility from Powershell but it hangs. Same command works fine command prompt. I am using Invoke-Expression to launch bcp.exe.
I am able to launch SQLCMD.exe without any problems.
This is my powershell.
Set-Location -Path "C:\Program Files\Microsoft SQL Server\110\Tools\Binn"
SQLCMD.EXE -b -E -S CORPSYSSQLDEV -d CORPSYSDM -Q "select top 10 * from t_test"
$psCommand = "bcp.exe ""testDB.dbo.t_test"" in ""C:\temp\test\testFile20180919.txt"" -c -t""\t"" -T -S ""TESTSQLDEV"" -e c:\temp\NoahFolder\error.csv"
Write-Host $psCommand
Invoke-Expression $psCommand
This is the result of $psCommand.
bcp.exe "testDB.dbo.t_test" in "C:\temp\test\testFile20180919.txt" -c -t"\t" -T -S "TESTSQLDEV" -e c:\temp\test\error.csv
Which works fine from command prompt but when I run the powershell script its stuck.
I am able to launch SQLCMD.exe from powershell fine.
What am I doing wrong? Any ideas or pointers.

I should have searched more before asking here. This post helped me. I was able to run BCP from powershell using Start-Process command.
$bcp = 'c:\Program Files\Microsoft SQL Server\110\Tools\Binn\bcp.exe'
$arglist = #(
#add all arguments.
)
Start-Process -FilePath $bcp -ArgumentList $arglist

Related

Select after login via Batch (Microsoft SQL Server Management Studio)

I am trying to select data from table and save them info .csv file via .bat file.
My batch runs Microsoft SQL Server Management Studio and log me into database, but I am not able to execute sql scripts.
Here is what I have:
#echo off
cls
:begin
set /p SName=Server Name :
set /p DbName=Database Name :
set /p UName=User Name :
set /p DbPWD=Password :
pause
echo Running Microsoft SQL Server Management Studio, please wait
call ssms -S %SName% -d %DbName% -U %UName% -P %DbPWD% "select * from MYTABLE" -s "," -o " Export.csv"
echo Done
pause
:end
It does not work after line: ssms -S %SName% -d %DbName% -U %UName% -P %DbPWD%
UPDATE:
Error picture
Thank you
SSMS does not accept an in-line script from the command line, as you are trying to do. Please use SQLCMD for this work. that's why you got the error message.
See SSMS Command Line
Straight from Microsoft
sqlcmd -S <ComputerName>\<InstanceName> -i <MyScript.sql> -o <MyOutput.rpt>

Run .sql file from batch file

I'm new to batch files and would like to execute sql script from the batch file. Please can someone assist.Thanks in advance.
You can use sqlcmd.exe from the DOS prompt. If you run sqlcmd -? from the DOS prompt the program will tell you the command line parameters. Here's an example
sqlcmd -S MyDbServer -d DatabaseName -E -i "MyScript.sql"
The -E tells sqlcmd to use "trusted authentication" meaning your Windows login. If you're not using Windows Authentication then you'll want to use the -U and -P parameters.

Execute remote powershell script from local system

I am new to this powershell..Execuse me if anything wrong here.
Now coming straight to the question I have a powershell script stored in one of the drive say "D:\myfile.ps1" which is in remote system.
Now I need to execute this script from my local system and this is the way I have done
ExecuteCommand(Directory.GetCurrentDirectory & "\Psexec \\myservername -i -u username -p password powershell.exe Invoke-Command -ComputerName \\myservername -FilePath D:\myscript.ps1")
The result is showing me this way without any output giving me an error
"powershell exited on myservername with error code 1'
And If run this script to run .exe on the same server it's working fine
ExecuteCommand(Directory.GetCurrentDirectory & "\psexec \\myservername -u username -p password D:\myfile.exe")
So what's the correct way to execute my script to run on remote desktop ?
Update after suggestions:
Can you just try :
ExecuteCommand(Directory.GetCurrentDirectory & "\Psexec \\myservername -i -u username -p password powershell.exe -command "&{Invoke-Command -ComputerName \\myservername -FilePath D:\myscript.ps1}"

Executing series of SQL commands on the command line

I want to run a series of SQL statements against a SQL Server 2005 database from the command line.
When I launch 1st statement
osql -E -S <Server_Name>\<Instance_Name> -d <Server_Name>
it is going to prompt window 1> from there after I am unable to proceed further through script.
How to give input to 1> prompt I mean giving next SQL statement
BACKUP DATABASE TO DISK = 'c:\test.bak' WITH INIT,SKIP
and finally exit to that prompt
I tried with && but I guess that is for only commandline commands.
You ae looking for the -Q switch on the sqlcmd tool (don't use osql on sqlserver 2005 or higher) (type sqlcmd /? to see all options) or lookit up on msdn
sqlcmd -E -S <Server_Name>\<Instance_Name> -d <Server_Name> -Q "BACKUP DATABASE TO DISK = 'c:\test.bak' WITH INIT,SKIP"
Alternatively you can create a sqlscript file where you put all the sql statements in you want to execute. Assuming you name your file myscript.sql the osql command would go like this:
sqlcmd -E -S <Server_Name>\<Instance_Name> -d <Server_Name> -i myscript.sql
Perhaps you may want to try a small trick that emerged from other question in this forum (that was deleted unfortunately).
You may insert the input for a command directly in the lines below the command and then execute the file NOT as Batch file, but as input por cmd.exe (this is similar to a here document in Linux). For example:
script.TXT:
#echo off
osql -E -S <Server_Name>\<Instance_Name> -d <Server_Name>
BACKUP DATABASE TO DISK = 'c:\test.bak' WITH INIT,SKIP
exit
Execute previous "script" this way:
cmd < script.TXT
If you perform this test, please report the result...
Antonio

Running a T-SQL script under different credentials using SQLCMD.exe utility

I created a batch file and placed the following line into it:
runas /user:internal\c39293 "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" -E -S WLDZ9454 -d ChadDb -Q "usp_Test"
I was trying to run the stored procedure usp_Test on the server WLDZ9454 against the ChadDb database.
When I execute it, I just get info regarding param options, no error mesage, so it isn't clear to me what I am doing wrong.
Note that when I run the code minus the code that attempts to run under different credentials, it works:
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" -E -S WLDZ9454 -d ChadDb -Q "usp_Test"
I suspect it's because you have not enclosed the program you are running and its parameters in quotes.
SqlCmd is therefore executed without any parameters.
runas /user:internal\c39293 """C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE"" -E -S WLDZ9454 -d ChadDb -Q ""usp_Test"""