SQL Installation command from vb.net code - sql-server

I am using following command through a batch file to install my required instance of SQL server. Problem is, when it runs, statement is shown on screen and .bat file is open for view.
How can I run this statement through my vb.net application?
start /wait SQLEXPR32and64.EXE /qb username="MyName" companyname="COName" addlocal=ALL disablenetworkprotocols="2" instancename="ERPTesting" SECURITYMODE="SQL" SAPWD="abc123"
Thanks

You should be able to pull that off using the accepted solution to this question: Hiding command window in vb.net when running processes

Related

Automatically run .bat file after EC2 reboot without having to remote connect

I am running an AWS Windows 2012 EC2 instance that has to run 24/7. On this instance, I run a Python 3.6 scraper script and to prevent me from having to regularly check up on the server whether the file is running, I have a .bat file in the shell:startup folder of my instance, that automatically restarts it on a daily base. The .bat file works as it will run the Python script and set a timer to restart/reboot the instance after (t=86400). The .bat file runs on the EC2 instance itself.
However, what the file does not do is run automatically after the reboot. I now first have to remote connect to the server before the .bat file will run. What I want it to do is run without me having to first remote connect into the server. How can I achieve this?
I use the following code in my .bat file. Located on my EC2 instance.
#ECHO OFF
START CMD /K (
CD C:/Users/Administrator/Documents/
python scraper.py
)
START CMD /K SHUTDOWN -t 86400 -r -f
I have tried looking into using AWS' Automations and other schedule based methods but couldn't get that to work.
If you want to use something native to Windows Server 2012, look at Schtasks -- this is more or less the Windows equivalent of cron.
I found the answer to my question by using Task Scheduler and looking at the following article: Run a batch file with Windows task scheduler
An important note here is that for my batch file to run I had to create a task that started CMD and run the batch file from there. Asking Task Scheduler to run the batch file directly doesn't work on Windows Server 2012. I ran the task with the following details:
Administrator account
"Run whether user is logged on or not"
"Run with the highest privileges"
"Start on system start-up"
Action: Start a program -> CMD
Add arguments (optional): /c start "" "C:\Users\Administrator\Desktop\file.bat"
More information on how to do this can be found in this answer: https://stackoverflow.com/a/27055435/7736676

Text File Permissions Issue

What is the default SSIS Account that is used in SSIS packages.
In one of my packages I have a Data Flow task that creates a flat file. A BAT file later runs and creates a file based on information in that .txt file. If I execute the .BAT in Windows Explorer it runs fine. When SSIS tries to execute it I can see the CMD window open and it tries to access the txt file and isnt able to. Says "Unable to access nameoffile.txt"
I assume the issue is that the permissions are probably inherited by the SSIS user account so I am trying to figure out which account that is.
If you are running it from the IDE then it uses your permissions. Most likely the problem is that your file is locked by some other process in your SSIS package. To verify:
Add a PAUSE to your batch file to make it wait for any key.
Run your package. While the dos prompt is waiting, go into windows explorer and run your batch file again - you'll find the same error.
I changed the attrib value in dos for that specific folder by attrib -r -s and it worked. It thought it was read-only.
I am using Windows Server 2012 server, with a SQL Server 2012 database.
Adding Modify, Read, Write permissions to the folder containing the required file to the NETWORK SERVICE user seems to work for me.

Executing a stored procedure using Windows task Scheduler

I've been trying to set up a schedule to run a stored procedure every hour in Windows Task Scheduler (as I'm using SQL Express and can't install 3rd party tools) but after trying various methods such as running a .bat file from task scheduler, opening SqlCmd utility from task scheduler and passing either the command line syntax or a .sql script file I'm having no luck.
I know this can be done and therefore I'm sure it's something I've missed but if anyone can share their experience of this I'd very much appreciate it.
The following command is in the batch file...
sqlcmd -E -i"C:\Users\Administrator\Desktop\test.sql" -o"C:\Users\Administrator\Desktop\dump.txt"
Thanks a lot
If you are an admin on the sql instance (Since you are using SQLExpress I bet you are trying to do this on your own computer so there is a high chance your user is an admin of the sql instance) you should not use -E at all, just ignore it.
Second, specify the server even if you are working on local.
Start with a simple sql command like below:
sqlcmd.exe -S "." -d MY_DATABASE -Q "SELECT * FROM MY_TABLE"
Replace MY_DATABASE and MY_TABLE with your dbname and table name. Make sure you can run it from command line. It should return the data from your table. (Beware command line options are case-sensitive so -s is not same as -S)
Last, do not try to feed parameters through task scheduler. Put the command with all parameters in a .bat file and just run the batch from task scheduler.
I have recently had a similar issue and my experience may assist you. I was calling a small app i.e. EXE from a batch file. I was scheduling the batch file to run from the Windows Task Scheduler. The app was accessing the SQL data using Windows Authentication.
I could run the app directly i.e. click on the EXE to run it.
I could run the app from the batch file.
But if I tried to run the scheduled task it seemed to start but did nothing and posted no errors that I could find.
I found if I changed the app to run with SQL Authentication it could be run from the Task Scheduler.
I suspect there is something about the context of the Windows Authentication when it is run from Task Scheduler that is not recognised by SQL.

Batch file to open excel works on sql server agent , but sql server job never completes and goes in a loop

I have a batch file to open a spreasheet and run the auto open macro. This work . Putting the batch file on a sql server agent job, again it works but the job never seems to end . Any ideas why ?
Code for batch file
call C:\Imports\Account.xlsb
exit
code for sql server agent
C:\Windows\System32\cmd.exe /c "C:\Imports\Test\OpenExcelFile.bat"
I'm not sure why you would need to run cmd.exe to run a batch file. You should only need to specify the file name name in quotes while using an "Operating system (CmdExec) job step. The step should use the following code:
"C:\Imports\Test\OpenExcelFile.bat"
If this doesn't work, then try running the batch file from the xp_cmdshell stored procedure. Here's the code you would need to execute:
EXEC master.dbo.xp_cmdshell 'C:\Imports\Test\OpenExcelFile.bat';
GO
This could be called by a Transact-SQL script (T-SQL) job step.
Are you calling this in a SSIS package? The post was tagged as SSIS, but you never mentioned in the post that you tried to call this from a SSIS package. If this is a SSIS package, then are you able to run the SSIS package successfully in BIDS? If you are running this from BIDS, then you shouldn't need to call the cmd.exe file. There is an Execute Process Control Flow task that you could use that does not require running a batch file from cmd.exe. If you are not using a SSIS package, then can you remove the SSIS tag?

How to create Log file in SSIS

I want to create a log file in SSIS. I know how to create through management studio, but I wanted to run my SSIS package through Command Prompt.
Can you help me find the Windows commands for that?
You can use the /L option, as documented here; however, you can also set Logging up in a much easier and finer way through the SSIS>Logging menu in Visual Studio.
Cheers,
Eric
In your SSIS you need to create and configure an SSIS Log provider before you can use it. Look into link text for more info.
You need to set up the a new connection string inside of Business Intelligence Development Studio (BIDS). where you have to specify the path of Log file...i mean here path will be D:\Sample_Examples\Log.log... and name of the connection manager will be Log.log
so now the command prompt will get changed into
dtexec /f "C:\\Package.dtsx" /l "DTS.LogProviderTextFile";"Log.log"
instead of
dtexec /f "C:\\Package.dtsx" /l "DTS.LogProviderTextFile;D:\Sample_Examples\Log.log"
we cant specify a direct path after DTS.LogProviderTextFile...because of that only i got error...
Thank you very much for the suggestions...
You can do this another way, without going into your SSIS package.
Create a batch file and run DTEXEC in a new instance of CMD, i.e.:
CMD /C DTEXEC your package and args > log file
When you do this, you can run this batch file and get your log file.
See http://richarddingwall.name/2009/01/26/capture-the-output-from-a-scheduled-task/ for more details
Yes, you have to create a connection manager for the log provider; however; there is no documentation on how to do this if you are using a custom log provider.

Resources