Which server is a batch file executed from? - batch-file

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.

Related

How to exit Plink used to forward a port of SQL server connection, when SQL server job is completed

I have setup PuTTY/Plink to connect to a remote server for loading data from the remote SQL server.
I can easily call the saved session by using the following:
c:\plink -l myusername savedsite
I can then run a SQL Server job that loads the data.
The issue I have is, that this SQL Server job is a scheduled job that runs every x minutes.
What I notice is that running the Plink command keeps the command window open. So on the subsequent run of the job, another command window opens.
What I want to do is to issue an exit/logout command to the Plink window when the task of loading the data is complete. How do I achiever this please?
OK, it does not looks like you will provide us enough information, so that we can give you a good answer.
So only briefly: Plink won't close for you. It cannot know when you are done.
You have to close it on your own. E.g. by:
Starting it with a redirected input and writing exit/logout to it; or
Remembering its process ID and killing it.
We cannot show you how, as we do not know anything about the way you run it and options your environment allow.

How to put Open VPN process in an SSIS task control flow

I'm learning how to use SSIS to create auto scheduled tasks. Everything went smoothly until the security policy has changed. Before whenever I wanted to connect to database, I just type in IP address, username and password in SSIS connection manager. Now I need to open up OpenVPN first which is still a manual process. So I really would like to put this bit into an SSIS task flow.
I had a search on Stackoverflow and over some other websites. Here is a link in stackoverflow. This example gives the idea that we can use VBA to setup the link. However that example shows we need a VPN connection name and VPN username which I don't really have and don't know where to find. Everytime I just right click on a .ovpn file and choose start openvpn on this config file. Also when I open that .ovpn file there is no command indicating which is my connection name or user name. Here is what it looks like when I open it up (there is no -auth-user-pass thing in this file and I can use this file without having to specify it):
client
dev tun
proto udp
remote a website here 1111
resolv-retry infinite
nobind
persist-key
persist-tun
comp-lzo
verb 3
ca ca.crt
cert office.crt
key office.key
askpass login.txt
Is it possible to find my connection name and user name? If I don't need connection name and user name, can I still write up VBA to build up an auto connection process?
I also notice there is an execute process task in SSIS. Is it possible that I can make use of it (because in my understanding obviously I use openvpn.exe to execute a .ovpn file)?
Please correct me if I'm saying something wrong since I'm new to both VPN and SSIS. And much appreciate for whoever can help me with this.
I don't know anything about OpenVPN, but I ran into a similar problem a while back with a server that insisted on an SSH tunnel using PuTTY as the only allowed connection method.
People who put these policies in place never think of the impact on SSIS, especially for unattended processing.
The solution I found was:
Find the command-line equivalent to the PuTTy client (which is called plink)
Have the detailed documentation to hand, because this is really difficult.
Have the SSIS package start plink.exe, passing various parameters (Note: don't use the Execute Process task, as that waits for the process to exit: launch the process in a Script task, and get the handle of plink.exe's ProcessID).
After a wait, have another Script task in SSIS connect to the plink process' InputBuffer, "typing in" a password. Yes, insecure and clunky, though you can store SSIS project parameters encrypted (this obviously requires SSIS 2012 or later)
The SSIS package can now connect to the server and do data stuff, as if it was a normal server without stupid access requirements.
At the end of the package, kill the plink.exe process.
Bear in mind, also, that on unattended execution (e.g. when scheduled in a job), your package will be running in a different security context from the one you work in to develop the package (depending on the SSIS service settings). If your access to OpenVPN has any dependency on ActiveDirectory, you may have to set up another OpenVPN account for the SSIS service's Logon user.
If this sounds horrible, nightmarishly complicated and unstable, it's because it is. Really, the TL;DR version is: tell whoever administers the server that if they really want you to access it using SSIS, they should put some more sensible access policy in place.
After working with my IT colleague, I think I solve the problem. Here are my answer and opinion to this question, and please correct me if I am wrong.
I think opening a VPN automatically is a case by case issue. In this case I am still using Execute Process Task in SSIS. Inside the task editor, what we are executing is not openVPN.exe, it should be a batch file. In the batch file (.bat file) it contains the command of opening .ovpn file with OpenVPN.exe
The process below gives you more detailed info.
The command line is what I use in the batch file:
#echo off
CD /D "C:\Program Files\OpenVPN\config"
start openvpn client.ovpn
TIMEOUT /T 10
exit
In SSIS execute process task
Hopefully this is useful

psftp batch script hangs on and off when scheduled from sql server agent

I have a psftp script to get files from a server every hour. The script works good when I manually run the bat file from its location and also runs well from the SQL Server Agent when I am logged in/monitoring it.
However, when am not logged in, the script hangs and continues to run forever, creating an empty log file.
Since this is an hourly run, this blocks out all subsequent runs as well creating delays in data load.
Could anyone advice what could possibly be causing this rather bizarre issue.
The log files created for each run are independent, with time stamp - so its not a lock out of log files causing the hanging.
Even though there is a prompt , a prompt file is supplied with value Y in it , that allows it to run without any requirement for explicitly entering a prompt value.
The script has been scanned for any pauses and other timeouts (Is not applicable really , since it works seamlessly when am logged in)
I tried setting set --trust-model=always, script still hangs when I am not logged in.
This said, I am not really sure if it has anything to do with me being logged in or if its just a coincidence. But basically, I am never able to catch the issue happening while am monitoring it!
This is probably a permission issue. Firstly check what user will be running the slq agent job when you are not logged in. Then ensure this user has full permissions/admin rights.
I think you might need admin to shell out - which I presume you are doing to run psftp.

Access macro did not run on the server when the database opened by a scheduled job

I have a scheduled job on the server. The job is to run a batch file at night when I am logged off. It simply opens an Access database and runs a macro. I have used /x to do this, and all references to files are UNC, not via mapped drives.
When I run the batch file by double clicking on it, it runs and finishes smoothly, however the scheduled job does not seems to be working. It opens the database and leaves it open until next morning and the macro is not executed. I have to clear MSACCESS.EXE from the Task Manager next morning.
I have tried with VBScript and found same result.
Does anyone have a clue?

Exe files run by Windows service remain in Task Manager and are not removed

I have a Windows service that is basically a job scheduler. When a job is supposed to be triggered it reads some parameters from an Sql Server database and runs an exe file.
In order for the service to read from the database I have to change my service properties to log on as another account who has access to the database. So I have to change my local account to an account e.g. john.smith#myserver.com with password=123
Things seem to be working fine, except when I leave the computer at night and a job is supposed to be run e.g. every hour. The morning after when I log on, I see that the jobs are successfully triggered but for each run of the job there is a new instance of the exe file in my processes. And I have to close all those instances. This obviously is not my desired behavior.
Does anything change when windows is in Locked mode? And either way, why the new instances of the program remain in task manager. Not to mention, the exe files are automatically closed whether they do their job correctly or not.
If you run the EXE from the command line, does it require any kind of keyboard input? Is it asking for parameters? Does it say something like "Press any key to exit"?

Resources