I'm pretty new to batch scripting.
I was looking for a simple way to start an executable on several remote computers.
What I have come up with, until now, is to make a batch file which calls psexec for all machines on the network, but it seems I can make it work for one computer only. I have two versions of the psexec part of the script:
FIRST VERSION
psexec -i \\PC-IP -u "xxxxx" -p "yyyyy" "C:\XXXXXXXX\my_executable.exe"
this is the very simple instruction I thought I should use for all the machines in the network. unfortunately, it works on the first computer but then, it opens a remote cmd, as far as I understood, and I don't know how to exit from it, to go to the next psexec.
SECOND VERSION
psexec -d \\PC-IP -u "xxxxx" -p "yyyyy" "C:\XXXXXXXX\my_executable.exe"
this is a variation of the first version, where I try to run the exec not interactively, so as not to be blocked and go on with the other instructions. This doesn't work, with the following error:
Logon failure: the user has not been granted the requested logon type
at this computer
can someone help me understand what I'm doing wrong?
thank you in advance.
I solved this problem.
The program was actually being started, but it was "stuck" as if waiting for something, and my batch script wasn't moving forward. Turns out, the program being started on first computer wasn't listed among the current session user's running programs (i.e. from task manager, the program was listed on the "Processes" tab, but not on the "Users" tab, under the logged in user).
To actually make it run properly (in my case, my executable would open a GUI), the proper psexec command was:
psexec -accepteula -i 1 -d \\COMPUTER_NAME -u "COMPUTER_NAME\xxxx" -p "yyyy" "C:\xxxxxxx\my_executable.exe"
where "1" indicates the ID of the user "xxxx" current session.
With this the GUI showed up, the script moved on and I could execute the program everywhere on the network.
I'm still not sure why I had to add both -i and -d (I thought that either I used one, or the other).
Related
my problem is little tricky. I am able to remotely run a batch file using PsExec tools. But now my task demand me to run another batch file on systems remotely and this time I have to enter parameter value as prompt.
However, this could be done if you got couple of users but I got many users running and calling that batch file simultaneously. So, I am unable to enter parameter value at user end machine each time.
What I was thinking that is that possible to set a parameter value before calling remote systems and later initially called parameter triggers .bat file parameter with same value.
Something like this:
set /p call=%trigger%
PsExec.exe -h \\%IPADDR% -u username -p password xyz.bat -h (%trigger%)
so when that xyz.bat file will open remotely then only %trigger% section will trigger out. Remember that me working of basic batch scripting on windows means no C language neither powershell scripting.
The following are performed via Task Scheduler, the two commands are run using the same task (just altering the trigger time) and I logoff the server before the task trigger time occurs.
If I create a batch file for the following command it executes fine (it creates the log files and writes the sqlcmd output to the file), furthermore the output in the log file is 1, which explains that the account running the batch file has permission to execute the stored procedure:
sqlcmd -E -S .\SQLEXPRESS -d WEDFUTtest -Q"select HAS_PERMS_BY_NAME('sp_DBMaintenance', 'OBJECT', 'EXECUTE')" -o %FILENAMEANDPATH%
If I edit the batch file (so it maintains the same permissions etc) with the following command:
sqlcmd -S .\SQLEXPRESS -E -Q "EXEC sp_DBMaintenance #dbName=TestDB, #FragmentationThresholdForReorganizeTableLowerLimit='5.0', #FragmentationThresholdForRebuildTableLowerLimit='30.0'" -d TestDB -o %FILENAMEANDPATH%
And logoff and allow the Task Scheduler to run it, it does not work i.e. there is no log file and the whole task executes in a very short time, sometimes under 1 second... and I know that the sp_DBMaintenance script takes over 20 minutes to finish... there are no error messages...
Furthermore (logged in on the domain administrator account) I can manually run the 2nd (non-working) command manually either via cmd or by right clicking the task in Task Scheduler and everything works fine.
So the question is, why isn't the 2nd command working correctly?
P.S. %FILENAMEANDPATH% is of the form SP_Log_23-11-2016_11-41-00.log
I'll put the 'answer' here in case it helps anyone encountering a similar issue...
Both the 1st and 2nd command have identical settings for the Task Scheduler...
On the off chance I unchecked the security option "Run with highest privileges" and the 2nd command now works?!?!?!?!? I don't understand why but now it works...
Taken from Technet
"If you select the checkbox labeled Run with highest privileges, Task Scheduler will run the task using an elevated privileges token rather than a least privileges (UAC) token..."
What baffles me is how the 1st command executed sqlcmd with 'Run with highest privileges' but the 2nd sqlcmd only works without it?!??!?!?!?!
I have to invoke a script through psexec which involves using an username that has hyphens in it. Unfortunately, the command does not work and does not throw any understandable error message. It just shows me how I should be using psexec i.e., various arguments and what they stand for.
My command goes something like this -
psexec.exe \\hostname -u domain\user-name-acct -p passwd -i 2 <script>
I am almost certain that hyphen is the culprit since I am able to invoke the same script by using a username that does not have hyphens. The account has admin privileges on the VM. Before I ask them to create / edit the username not to have hyphens, I just want to make sure that there is no way to run such commands.
Update - Command Result
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
PsExec executes a program on a remote system, where remotely executed console
applications execute interactively.
Usage: psexec [\\computer[,computer2[,...] | #file]][-u user [-p psswd][-n s][-r servicename][-h][-l][-s|-e][-x][-i [session]][-c [-f|-v]][-w directory][-d][-<priority>][-a n,n,...] cmd [arguments]
-a Separate processors on which the application can run with
commas where 1 is the lowest numbered CPU. For example,
to run the application on CPU 2 and CPU 4, enter:
"-a 2,4"
-c Copy the specified program to the remote system for
execution. If you omit this option the application
must be in the system path on the remote system.
-d Don't wait for process to terminate (non-interactive).
-e Does not load the specified account's profile.
-f Copy the specified program even if the file already
exists on the remote system.
-i Run the program so that it interacts with the desktop of the
specified session on the remote system. If no session is
specified the process runs in the console session.
-h If the target system is Vista or higher, has the process
run with the account's elevated token, if available.
-l Run process as limited user (strips the Administrators group
and allows only privileges assigned to the Users group).
On Windows Vista the process runs with Low Integrity.
-n Specifies timeout in seconds connecting to remote computers.
-p Specifies optional password for user name. If you omit this
you will be prompted to enter a hidden password.
-r Specifies the name of the remote service to create or interact.
with.
-s Run the remote process in the System account.
-u Specifies optional user name for login to remote
computer.
-v Copy the specified file only if it has a higher version number
or is newer on than the one on the remote system.
-w Set the working directory of the process (relative to
remote computer).
-x Display the UI on the Winlogon secure desktop (local system
only).
-priority Specifies -low, -belownormal, -abovenormal, -high or
-realtime to run the process at a different priority. Use
-background to run at low memory and I/O priority on Vista.
computer Direct PsExec to run the application on the remote
computer or computers specified. If you omit the computer
name PsExec runs the application on the local system,
and if you specify a wildcard (\\*), PsExec runs the
command on all computers in the current domain.
#file PsExec will execute the command on each of the computers listed
in the file.
cmd Name of application to execute.
arguments Arguments to pass (note that file paths must be
absolute paths on the target system).
-accepteula This flag supresses the display of the license dialog.
You can enclose applications that have spaces in their name with
quotation marks e.g. psexec \\marklap "c:\long name app.exe".
Input is only passed to the remote system when you press the enter
key, and typing Ctrl-C terminates the remote process.
If you omit a user name the process will run in the context of your
account on the remote system, but will not have access to network
resources (because it is impersonating). Specify a valid user name
in the Domain\User syntax if the remote process requires access
to network resources or to run in a different account. Note that
the password and command is encrypted in transit to the remote system.
Error codes returned by PsExec are specific to the applications you
execute, not PsExec.
I do not have a windows box to test my hypothesis on, but I believe you would enclose the domain\user-name in either single or double quotes:
psexec.exe \\hostname -u "domain\user-name-acct" -p passwd -i 2 <script>
or possibly:
psexec.exe \\hostname -u 'domain\user-name-acct' -p passwd -i 2 <script>
However, I would be leaning toward the double quotes..
Also, if this does not resolve your issue, perhaps post the errors, even if they are nonsense to you, they may mean something to others.
HTH
I am trying figure out how to run an application on local pc and then on my second pc both are at my home network, I am using several applications which merge together by entering second pc ip, so i though if i can anyway do something like
my spec: local 10.0.0.1
and second pc 10.0.0.4
need to run from my local
#echo off
start "" "C:\TEST\TESTAPP.EXE"
start 10.0.0.4 "C:\TEST2\TESTAPP.EXE" <-- just need find a way how to do this
didnt found anything regarding start command and ip address specified maybe it couldnt be achieved with bat script?
thanks everyone for any helpful response and sorry if its just dumb question
If I understand correctly you are trying to start a script on machine B from machine A. If this is correct I would recommend using PSExec from Sysinternals Suit.
Example
Psexec.exe \\<computername or ip> -u username -p password C:\ProgramToRun.exe
PSExec help file from http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx
Using PsExec
Usage: psexec [\\computer[,computer2[,...] | #file]][-u user [-p psswd][-n s][-r servicename][-h][-l][-s|-e][-x][-i [session]][-c [-f|-v]][-w directory][-d][-<priority>][-a n,n,...] cmd [arguments]
-a Separate processors on which the application can run with commas where 1 is the lowest numbered CPU. For example, to run the application on CPU 2 and CPU 4, enter: "-a 2,4"
-c Copy the specified program to the remote system for execution. If you omit this option the application must be in the system path on the remote system.
-d Don't wait for process to terminate (non-interactive).
-e Does not load the specified account’s profile.
-f Copy the specified program even if the file already exists on the remote system.
-i Run the program so that it interacts with the desktop of the specified session on the remote system. If no session is specified the process runs in the console session.
-h If the target system is Vista or higher, has the process run with the account's elevated token, if available.
-l Run process as limited user (strips the Administrators group and allows only privileges assigned to the Users group). On Windows Vista the process runs with Low Integrity.
-n Specifies timeout in seconds connecting to remote computers.
-p Specifies optional password for user name. If you omit this you will be prompted to enter a hidden password.
-r Specifies the name of the remote service to create or interact with.
-s Run the remote process in the System account.
-u Specifies optional user name for login to remote computer.
-v Copy the specified file only if it has a higher version number or is newer on than the one on the remote system.
-w Set the working directory of the process (relative to remote computer).
-x Display the UI on the Winlogon secure desktop (local system only).
-priority Specifies -low, -belownormal, -abovenormal, -high or -realtime to run the process at a different priority. Use -background to run at low memory and I/O priority on Vista.
computer Direct PsExec to run the application on the remote computer or computers specified. If you omit the computer name, PsExec runs the application on the local system, and if you specify a wildcard (\\*), PsExec runs the command on all computers in the current domain.
#file PsExec will execute the command on each of the computers listed in the file.
cmd Name of application to execute.
arguments Arguments to pass (note that file paths must be absolute paths on the target system).
-accepteula This flag suppresses the display of the license dialog.
How do I launch multiple sqlcmd windows from a batch file that all point to the same database? For example, when I run the .bat file I want it to spawn N number of windows based on a parameter that I pass into it (ex. 5). Each of these 5 windows should open on my desktop and all connect to the same database. That's what I want to do first. Once I have that working, I then want to have each of those 5 windows to run a distinct .sql script that performs inserts, queries, updates, deletes, calling stored procedures...essentially emulating a production environment to help us in debugging efforts (under a user load). I want to see the output of each .sql commend flying by in the sqlcmd window while it is being executed.
I found:
http://hammerora.sourceforge.net/
which is a GUI tool that is focused on TPC-C load testing, but it is not exactly what I want. I bring it up because it is a similar concept that I want to do only driven by batch files on a smaller scale (ex. 20 concurrent users max).
I created a system like this back in the late 90's for Oracle scalability testing but I've been out of the database business since then and can't remember how to do it and how different it would need to be to support SQL Server. So I know it is possible in Oracle, but just not sure about SQL Server given the command line tool and scripting capabilities.
Does anyone have any information about what it would take to make this work?
Ex. Create a launch3users.bat file that looks like:
sqlcmd -d MichaelTest -run this 1.sql file
Pause
sqlcmd -d MichaelTest -run this 2.sql file
Pause
sqlcmd -d MichaelTest -run this 3.sql file
Pause
where each of those would spawn a sqlcmd window and run the proper .sql script which could do DML operations or called stored procedures.
Thanks,
Michael
You simply add "start" to the beginning of commands.
start sqlcmd -d MichaelTest -i 1.sql
start sqlcmd -d MichaelTest -i 2.sql
start sqlcmd -d MichaelTest -i 3.sql