VbScript or with Scripting - batch-file

we need to execute scripts locally, but how can we do it from a remote machine, so as to avoid going on to each machine and triggering the process manually. preferably with VbScript.

Generally in your VBScript you will specify the machine you want to query with the line
strComputer = "."
This can be changed to a computer name or IP address to remotely query a machine. However it is difficult to provide anything further as you've not posted your script or what you're trying to achieve with it..

Write your .vbs scripts and deploy them to a remote server - then on the remote server schedule a task to run whenever you'd like the script to run.
The script will run locally on that server.

Related

Execute process task fails to execute Rust script in job

I have written a small Rust script for an import job. I added the script execution into the SSIS package through the Execute process task and set the correct working directory. Using the debugger, the script works without a problem, as it does when executed normally, e.g. through cmd or PowerShell.
When I start the job in SSMS as an Agent Job, the package fails with
The process exit code was "-1073741515" while the expected was "0".
What I tried
Replacing the script with a very basic Rust script that just writes one single line to a file in the working directory to exclude the possibility that the script somehow panics, but still nothing.
The script is compiled with the i686-pc-windows-msvc toolchain. With 64bit the script does not work at all on the server.
Permissions look okay for the executing and the working directory. Full control for both folders.
Since execution works on the server I think this should most likely be a permissions issue but I can't for the life of me not figure out what is wrong.
Okay the problem was indeed with the default SQL Server Service Agent-account that ran the job. If I run the job with a Proxy-account it runs fine.
I'll have to look into the configuration of the Service Agent but until then, Proxy account it is.

Which server is a batch file executed from?

Is there a way to have a batch file determine the IP/Name of the server from which it is being triggered (i.e. when it's executed on a separate server to the one triggering the execution)?
My initial thought is to add the line netstat -a >> WhoDunnit.txt so that I can see all connections moments after the script's called, then trawl through that hoping the connection was still open; but asking here in case there's a more sophisticated solution?
Background
We recently found a deployment script was being run at 2am each week, with no one having any knowledge of why.
The script's commands mean it will only affect the server on which it's running, so it must be executing locally.
The script's host server has nothing defined in its scheduled tasks / we can't see anything on this machine that would trigger this script.
So the script must be being called from some other server on which the schedule's defined, but in such a way that it executes on the host server rather than on the triggering server.
If I can find which server's calling the script I can find the schedule and disable it.
Batch file with below in it.
echo %~dpnx0
When started by
C:\Users\User>"\\127.0.0.1\C$\users\user\cmdline.bat"
returns
C:\Users\User>echo \\127.0.0.1\C$\Users\User\cmdline.bat
\\127.0.0.1\C$\Users\User\cmdline.bat
Now I would turn process auditing on and wait for it to start.

Copy .bak file to another Windows server using job MS SQL Server agent

I am trying to copy a .bak file nightly from Server A to Server B.
Can I do that using SQL server Job Agent to run this every night?
I am thinking of adding the copy command as a statement within a step of a job.
Something like: 'copy "G:\source\folder\" "\target\folder\"'
inside the step and setting the type to Operating System(CmdExec).
Is there a way to do it?
is this question about the command to copy the files?
If you want to copy entire folder use robocopy instead of copy
You can make a SSIS package to do that, and then run it from the SQL agent.
However, don't use logical drives, such as G: -- if the server doesn't have the same mapping, it won't work. Use the actual named servers: \serverA\source\folder to \serverB\target\folder.
Short answer is yes. You can try SSIS package as described here or here. Another option is to use windows task scheduler (vs using SQL Server Agent) and a simple bat script to do the same thing.

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.

SQL Agent and Running SSIS Across Network Not Working

I have a vb script which moves files from one server to another and it works successfully when run manually but when a job is created in SQL Agent to automate the process it doesn't perform the task. SQL Agent doesn't fail it simply does not perform the move.
I granted local admin rights to the account performing the task and still nothing. I copied the files manually to the local SQL Server but I still get the same outcome when attempting to run the job in SQL Agent.
I also tried using FQDN but it still doesn't perform the process. Any suggestions? Please help and thanks in advance.
if you have 32 bit try:
C:\Windows\system32\config\systemprofile\Desktop
For the package to work. If you do not have Destop folder, create one and it should work.
Try to check: C:\Windows\SysWOW64\config\systemprofile\Desktop folder for the package to work.

Resources