Running a UNC path using PSexec through CMD - windows

I got this code below, it will run the LS-PrePost-3.0-Win32_setup.exe using cmd.
My psexec is in c:\psexec. And I put put my batch file and the exe file in the same folder.
c:\psexec\psexec -d \\%%M cmd /c start /wait "%~dp0LS-PrePost-3.0-Win32_setup.exe" /quiet /silent /norestart
My code seems does nothing. It execute the code but the exe file didnt run the the remote PC.
Edit: I changed the directory of Psexec.

You can try with something like
c:\psexec\psexec \\%%M -d -c "%~dp0LS-PrePost-3.0-Win32_setup.exe" /quiet /silent /norestart
As you are not waiting (-d) you don't need the start /wait
As you are starting a executable file not an internal command, you don't need the cmd /c.
The file is executed in the remote machine, so, it must be available in the remote machine. Copy it (-c)
From psexec help
-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).
cmd Name of application to execute.
arguments Arguments to pass (note that file paths must be
absolute paths on the target system).

You can use pushd and popd to pass the the command through psexec for UNC paths.
psexec.exe //FQDN -d cmd /c "pushd \\UNCFOLDER\ && file2execute.exe && popd"

Related

Batch file doesn't continue until after I exit psexec remote connection

I'm trying to make a batch file to run a script on remote server. If I enter the commands below in the cmd prompt seperately it works fine but it seems to hang after I enter the psexec commands. It only continues the rest of the batch file when I exit the psexec remote connection. The rest of the commands are run on my local computer(which I don't want). Anyone have any ideas or suggestions?
psexec \\ServName -u DOMAIN\UserName -p password cmd.exe
pause
cd c:\Users\UserName
pause
cscript \\NetworkName\filepath\blankTest.vbs
You can bundle the commands into one batch file and then execute that using one line:
psexec \\ServName -u DOMAIN\UserName -p password cmd.exe -c mybatchfile.bat
That will cause the file to be copied to the remote machine first.
Alternatively if you have problems with that, copy the file first, then execute it; note the different parameter, /c vs -c:
copy mybatchfile.bat \\ServName\Admin$
psexec \\ServName -u DOMAIN\UserName -p password cmd.exe /c mybatchfile.bat
Personnaly if I want to start a background process in a batch I use this :
start /B cmd /C "mycommand" which in your case should give :
start /B cmd /C "psexec \\ServName -u DOMAIN\UserName -p password cmd.exe"

Batch file wont continue after call up a powershell script

I have a situation. I'm installing network printer on remote desktops using a batch file that calls up a Powershell script, but after printer is installed on the remote desktop, Powershell returns nothing - so my batch file wont be able to continue...I must manually hit ENTER twice to make it echo DONE and run the rest of the scripts. Any idea why?
#echo off
Copy /Y "\\AddPrinter\AddPrinter.ps1" "C:\scripts"
powershell.exe -ExecutionPolicy Bypass -Command "C:\scripts\AddPrinter.ps1"
Echo DONE
Del /F /Q "C:\ntfs3\scripts\AddPrinter.ps1"
This is my powershell script, I even put a exit in the end of script, but powershell on remote pc just wont exit.....so it will not return to batch
$PrinterPath = "\\server-01\Printer0101"
$net = new-Object -com WScript.Network
$net.AddWindowsPrinterConnection($PrinterPath)
exit
BTW, I m using psexec to call up this batch file with a txt file containing all pc names.
psexec #%1 -u admin -p xxxxxx -c -f "C:\AAA\AddPrinter.bat"
I did some research, some one said need to use call with %1 > null to make Powershell return to batch. But it does not work either.
Call powershell.exe -ExecutionPolicy Bypass -Command "C:\scripts\AddPrinter.ps1" %1 > null
I could only use batch command to install printer, but after installation, the new printer always setup as a default printer. but if use powershell to install it, it wont change default printer on remote desktop.
rundll32 printui.dll PrintUIEntry /ga /n\\server-01\printer0101
Hint please.
I have regularly seen problems calling PowerShell through psexec.exe. I would recommend ditching psexec.exe and configuring PowerShell Remoting instead. Then, you can deploy your script using the Invoke-Command command.
I could also suggest for DOS like commands to use invoke-expression. PowerShell will process the command that you are passing as it is. The great is that you will be able to pass also variables

Sikuli multiple scripts batch

I'm trying to create a batch in Windows 7 that will open at least two Sikuli scripts in sequence. I've tried using the solutions found here and couldn't get them to work. This is the batch command I've used:
cmd /C C:\path\Sikuli\runIDE.cmd -r C:\path\Sikuli\all.sikuli
cmd /C C:\path\Sikuli\runIDE.cmd -r C:\path\Sikuli\sikuli_test.sikuli
I've also tried:
start /i /b /wait C:\path\Sikuli\runIDE.cmd -r :\path\Sikuli\all.sikuli
start /i /b /:\path\Sikuli\runIDE.cmd -r :\path\Sikuli\sikuli_test.sikuli
The first Sikuli script executes but the second one does not. The problem seems to be within Sikuli IDE opening in cmd, which once it initializes doesn't allow any more commands in the batch to execute as Sikuli's monitoring process takes over the cmd prompt.
start /wait will wait for the executable to exit before continuing. Remove the /wait switch and batch will proceed to your second command.
have you tried
cd C:\SikuliX && runScript.cmd -r C:\path\Sikuli\all.sikuli
cd C:\SikuliX && runScript.cmd -r C:\path\Sikuli\sikuli_test.sikuli
What I did is I have two batch files to call the sikuli files ("Dummy1.sikuli" and "Dummy2.sikuli").
And then I have a 3rd batch file that will call all other batch files.
Sikuli opens by using the command window, and this way both .sikuli files have a command window.
My examples are all located in:
C:\Dummy
Files located here:
Dummy1.sikuli
Dummy2.sikuli
Dummy1.bat
Dummy2.bat
RunDummies.bat
Dummy1.bat
#ECHO OFF
REM Run Dummy1.sikuli
C:\Sikuli\runIDE.cmd -r C:\Dummy\Dummy1.sikuli
Dummy2.bat
#ECHO OFF
REM Run Dummy2.sikuli
C:\Sikuli\runIDE.cmd -r C:\Dummy\Dummy2.sikuli
RunDummies.bat
#ECHO OFF
start cmd.exe /C Dummy1.bat
start cmd.exe /C Dummy2.bat

Run a .cmd file through PowerShell

I am trying to run a .cmd file on a remote server with PowerShell.
In my .ps1 script I have tried this:
C:\MyDirectory\MyCommand.cmd
It results in this error:
C:\MyDirectory\MyCommand.cmd is not recognized as the name of a cmdlet,
function, script file, or operable program.
And this
Invoke-Command C:\MyDirectory\MyCommand.cmd
results in this error:
Invoke-Command : Parameter set cannot be resolved using the specified named
parameters.
I do not need to pass any parameters to the PowerShell script. What is the correct syntax that I am looking for?
Invoke-Item will look up the default handler for the file type and tell it to run it.
It's basically the same as double-clicking the file in Explorer, or using start.exe.
Go to C:\MyDirectory and try this:
.\MyCommand.cmd
Try invoking cmd /c C:\MyDirectory\MyCommand.cmd – that should work.
To run or convert batch files to PowerShell (particularly if you wish to sign all your scheduled task scripts with a certificate) I simply create a PowerShell script, for example, deletefolders.ps1.
Input the following into the script:
cmd.exe /c "rd /s /q C:\#TEMP\test1"
cmd.exe /c "rd /s /q C:\#TEMP\test2"
cmd.exe /c "rd /s /q C:\#TEMP\test3"
*Each command needs to be put on a new line, calling cmd.exe again.
This script can now be signed and run from PowerShell outputting the commands to command prompt / cmd directly.
It is a much safer way than running batch files!
First you can reach till that folder:
cd 'C:\MyDirectory'
and then use:
./MyCommand.cmd

open command prompt window and change current working directory

I'm terribly new to scripting on windows. Using windows 7 64.
I'm trying to make a .bat file that I can double click, and have it open a command prompt and automatically cd me to a certain directory.
I tried making a .bat file with
#ECHO OFF
cmd "cd C:\my\destination"
Which opens what looks like a command prompt, but doesn't seem to let me type any commands.
I then tried:
#ECHO OFF
start cmd "cd C:\my\destination"
But this just sent me into a loop opening tons and tons of prompts until my computer crashed :) The .bat file was located in the destination directory if that matters.
This works for me:
#ECHO OFF
cmd.exe /K "cd C:\my\destination && C:"
The quoted string is actually two commands (separated by a double ampersand): The first command is to change to the specified directory, the second command is to change to the specified drive letter.
Put this in a batch (.BAT) file and when you execute it you should see a Command Prompt window at the specified directory.
Use the /K switch:
#ECHO OFF
start cmd.exe /K "cd C:\my\destination"
But IMHO, the most useful switch is /?.
Starts a new instance of the Windows XP command interpreter
CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
[[/S] [/C | /K] string]
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
/S Modifies the treatment of string after /C or /K (see below)
/Q Turns echo off
...
And only if it does not work, then Google it, as #Neeraj suggested :D
This could be done like that:
#ECHO OFF
cd /D "C:\my\destination"
cmd.exe
If you need to execute a file or command after you open the cmd you can just replace the last line with:
cmd.exe /k myCommand
#ECHO OFF
%comspec% /K "cd /D d:\somefolder"
The /D will change folder and drive and works on 2000+ (Not sure about NT4)
If you take a look at Vista's open command here, it uses cmd.exe /s /k pushd \"%V\" but I don't think %V is documented. Using pushd is a good idea if your path is UNC (\\server\share\folder) To get UNC current directory working, you might have to set the DisableUNCCheck registry entry...
Why so complicated? Just create an alias to cmd.exe, right click on the alias and navigate to its settings. Change the "execute in" to the path you want to have as standard path. It will always start in this path.
just open a text editor and type
start cmd.exe
cd C:\desired path
Then save it as a .bat file. Works for me.
You can create a batch file "go-to-folder.bat" with the following statements:
rem changes the current directory
cd "C:\my\destination"
rem changes the drive if necessary
c:
rem runs CMD
cmd

Resources