using runas with a password and a sql command - sql-server

I have a batch file with the following script (which i found here at stackoverflow)
#if (#CodeSection == #Batch) #then
#echo off
start runas "/user:domain\username" "cmd.exe"
CScript //nologo //E:JScript "%~F0"
goto :EOF
#end
WScript.CreateObject("WScript.Shell").SendKeys("mypass{ENTER}");
where mypass is the username's password.
this works perfectly and a new commandline window opens as the new user!
the problem is that i now need to run an sqlcmd with parameter and I can't find any way to write a batch that does both things together (open the new window as another user together with the command with parameters)
the sql command:
"C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE" -S "my.server.com, 1234" -d "myDataBase" -E -b -q "EXEC myDataBase.[dbo].[myTable] #parameter=1"
I tried moving the whole sql command to another batch (myBatch.bat) file and change the first batch to this:
start runas "/user:domain\username" "myBatch.bat"
but it doesn't work.
any help please?

runas /user:domain\user "cmd.exe /k c:\path\to\mybatch.bat test"
cmd /k to keep the console open after the batch execution, or cmd /c to close it

Related

Command Prompt (cmd) is not executing Plink SSH Command

I am trying to execute a batch file (.bat) with the following commands in the batch file.
I have a batch file containing the following;
#ECHO OFF
cmd.exe /K "cd C:\Program Files (x86)\PuTTY && C:"
set PATH=%PATH%;C:\Program Files (x86)\PuTTY
pause
plink.exe -ssh username#firewall1 -pw PassWord! < commands.txt > c:\output_.csv"
pause
the plink.exe command works when entered in manually.
commands.txt is just a simple firewall command for now.
All I see when running the batch file is a cmd window open point at the Putty folder, and that's it.
So how can i get this to run please?
Your batch file at line 2, opens a new cmd window. That's not probably what you want. Also it changes the current directory and the current drive, read HELP CD, and then change that line to just cd /d "C:\Program Files (x86)\PuTTY"
Also, as a bonus recommendation, you are changing the PATH every time you run this command, adding the putty directory that you mage as current anyway, so this command is unnecessary and redundant.
So, I would leave your bat as simple as
#ECHO OFF
CD /D "C:\Program Files (x86)\PuTTY"
plink.exe -ssh username#firewall1 -pw PassWord! <commands.txt >c:\output_.csv
I would suggest making the batch file simpler still:
#Start "" /D "%ProgramFiles(x86)%\PuTTY" plink.exe -ssh username#firewall1 -pw PassWord! <commands.txt >C:\output_.csv
You may consider running other options such as /MIN, type Start /? at the command prompt for usage information

Run all SQL files in multiple directories

I combined a couple of solutions I found online to try and make this happen.
https://stackoverflow.com/a/6504317/2471473
https://sqlandme.com/2013/03/25/sql-server-executing-multiple-script-files-using-sqlcmd/
I'm trying to run a single .cmd script (Script1.cmd) with folder locations of .sql files. That single script runs another script (Script2.cmd) to use sqlcmd to execute all the .sql files in that folder location. It mostly works, but it leaves a command window open that I have to exit from for each folder location.
Script1.cmd
start Script2.cmd "C:\Location1"
start Script2.cmd "C:\Location2"
Script2.cmd
#Echo Off
FOR /f %%i IN ('DIR %1\*.Sql /B') do call :RunScript %1 %%i
GOTO :END
:RunScript
Echo Executing Script: %2
cd %1
SQLCMD -S Server123 -d Database456 -E -i %2
Echo Completed Script: %2
:END
Official command line reference for Windows XP or for Windows Server 2003, Windows Vista (and above) seems to be too brief. Read this (extended) start command documentation:
Syntax: START "title" [/D path] [options] "command" [parameters]
Always include a TITLE this can be a simple string like "My Script" or
just a pair of empty quotes "". According to the Microsoft
documentation, the title is optional, but depending on the other
options chosen you can have problems if it is omitted.
If command is an internal cmd command or a batch file then the command
processor is run with the /K switch to cmd.exe. This means that the
window will remain after the command has been run.
Next Script1.cmd should work and close started command windows:
start "" cmd /C Script2.cmd "C:\Location1"
start "" cmd /C Script2.cmd "C:\Location2"

Batch file command not working

I am trying to run this batch file remotely It will kill the IE process's but when I try to open a .lnk file it won't do it. When I go onto that machine, open up the command prompt and type in the command to run the .lnk file it works with no issues.. please help!
Code to remotely execute batch file:
psexec -u Administrator -p password -i -d \\hostname "c:\Emergency_POD\test.bat"
Code on machine to run: (Only the taskill command works.. not the for command)
cd/
taskkill /im iexplore.exe /f
for %a in ("C:\Emergency_POD\*.lnk") do #start "" "%a"
Command to run on cmd (This command works with no issues:
for %a in ("C:\Emergency_POD\*.lnk") do #start "" "%a"
You'd probably be better off with %%a in place of %a in the batch file.

Automatically running a batch file as an administrator

How do I run a batch file from another batch file with administrator rights?
I have tried the RUNAS command, but it requires the administrator password.
I am searching for an alternative for running a batch file by right clicking on it and running as an administrator. I want to automate it from another batch file.
Put each line in cmd or all of theme in the batch file:
#echo off
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
"Put your command here"
it works fine for me.
On Windows 7:
Create a shortcut to that batch file
Right click on that shortcut file and choose Properties
Click the Advanced button to find a checkbox for running as administrator
Check the screenshot below
You can use PowerShell to run b.bat as administrator from a.bat:
set mydir=%~dp0
Powershell -Command "& { Start-Process \"%mydir%b.bat\" -verb RunAs}"
It will prompt the user with a confirmation dialog. The user chooses YES, and then b.bat will be run as administrator.
Use
runas /savecred /profile /user:Administrator whateveryouwanttorun.cmd
It will ask for the password the first time only. It will not ask for password again, unless the password is changed, etc.
If you're trying to invoke a Windows UAC prompt (the one that puts the whole screen black and asks if you're granting administrator privileges to the following task), RUNAS is not the smoothest way to do it, since:
You're not going to get prompted for UAC authorization, even if logged in as the administrator and
RUNAS expects that you have the administrator password, even if your user is setup as a local administrator, in which case the former password is not a sound security practice, specially in work environments.
Instead, try to copy & paste the following code to ensure that your batch file runs with administrator privileges:
#echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
echo Requesting Admin access...
goto goUAC )
else goto goADMIN
:goUAC
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:goADMIN
pushd "%CD%"
CD /D "%~dp0"
rem --- FROM HERE PASTE YOUR ADMIN-ENABLED BATCH SCRIPT ---
echo Stopping some Microsoft Service...
net stop sqlserveragent
rem --- END OF BATCH ----
This solution works 100% under Windows 7, 8.1 and 10 setups with UAC enabled.
Runas.exe won't work here. You can use VBScript to invoke the "Run as Administrator" shell verb. The Elevation Powertoys contain a batchfile that allows you to invoke an elevated command:
elevatecmd.exe
http://blogs.technet.com/b/elevationpowertoys/
CMD Itself does not have a function to run files as admin, but powershell does, and that powershell function can be exectuted through CMD with a certain command. Write it in command prompt to run the file you specified as admin.
powershell -command start-process -file yourfilename -verb runas
Hope it helped!
The complete solution I found that worked was:
#echo off
cd /D "%~dp0"
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
"Put your command here"
credit for:
https://stackoverflow.com/a/51472107/15087068
https://serverfault.com/a/95696
This a trick that i used if anyone wants they can try this in batch file.This will give you the admin prompt when you run the batch file
#echo off
cd \ && cd windows/system32 && command which needs admin credentials
pause

How do I execute cmd commands through a batch file?

I want to write a batch file that will do following things in given order:
Open cmd
Run cmd command cd c:\Program files\IIS Express
Run cmd command iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
Open Internet Explorer 8 with URL= http://localhost:8088/default.aspx
Note: The cmd window should not be closed after executing the commands.
I tried start cmd.exe /k "cd\ & cd ProgramFiles\IIS Express", but it is not solving my purpose.
So, make an actual batch file: open up notepad, type the commands you want to run, and save as a .bat file. Then double click the .bat file to run it.
Try something like this for a start:
c:\
cd c:\Program files\IIS Express
start iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
start http://localhost:8088/default.aspx
pause
I think the correct syntax is:
cmd /k "cd c:\<folder name>"
This fixes some issues with Blorgbeard's answer (but is untested):
#echo off
cd /d "c:\Program files\IIS Express"
start "" iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
timeout 10
start http://localhost:8088/default.aspx
pause
cmd /c "command" syntax works well. Also, if you want to include an executable that contains a space in the path, you will need two sets of quotes.
cmd /c ""path to executable""
and if your executable needs a file input with a space in the path a another set
cmd /c ""path to executable" -f "path to file""
#echo off
title Command Executer
color 1b
echo Command Executer by: YourNameHere
echo #################################
: execute
echo Please Type A Command Here:
set /p cmd=Command:
%cmd%
goto execute
start cmd /k "your cmd command1"
start cmd /k "your cmd command2"
It works in Windows server2012 while I use these command in one batch file.
cmd /k cd c:\
is the right answer
I was trying to run a couple of batch files parallely at startup, if a condition was true.
For this I made a parent batch file which should have checked for the condition and invoke the other child batch files if the condition was true.
I tried to achieve it via START but it gave me an empty black command prompt running in the directory of children batch files, instead of running the children batch files themselves
The thing which worked for me was by using a combination of START and CALL
As an example
condition ...
start call "C:\Users\Amd\conn\wsl_setup - conn1.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn2.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn3.bat"
I know DOS and cmd prompt DOES NOT LIKE spaces in folder names. Your code starts with
cd c:\Program files\IIS Express
and it's trying to go to c:\Program in stead of C:\"Program Files"
Change the folder name and *.exe name. Hope this helps

Resources