second bat is not executed PowerShell script - windows

the below bat files are being called by PowerShell script, however only bat1.bat is executed the others bat2.bat and last.bat are not being called
#first bat
Start-Process "C:\bat1.bat" -Wait
#run second bat
Start-Process "C:\bat2.bat" -Wait
#run last bat
cmd.exe /c '\last.bat'
You r support is highly appreciated

Try using this:
Set-Location C:\temp
Start-Process .\bat1.bat -Wait
Write-Host "bat1.bat DONE"
Start-Process .\bat2.bat -Wait
Write-Host "bat2.bat DONE"
cmd /c .\last.bat
Write-Host "last.bat DONE"
I made the following .bat files:
bat1.bat
#ECHO OFF
ECHO bat1
PAUSE
bat2.bat
#ECHO OFF
ECHO bat2
PAUSE
final.bat
#ECHO OFF
ECHO final
When the PS script was ran, all 3 ran as one would expect.
Putting the Write-Host will show in the console if the previous process finished.
Edit:
I didn't give much explanation before, sorry.
Your issue is that one of the .bat files is not finishing.
The syntax you had should have run bat1.bat and bat2.bat. The "cmd.exe /c "\last.bat" would fail. You need the "c:\last.bat" if you aren't going to set the path to c:. If you did that, you would want ".\last.bat"
To make sure that your script would work, I made 3 basic .bat files whose functions were solely to log a string associated with them. Doing this guaranteed that if anything went wrong, it was with the PS code and not the .bat code.
After verifying your script would run, I made the one I provided. Using Set-location isn't necessary, but I think it makes the code look cleaner. The Write-Host after each process is logging in the PS console that the script is moving on to the next process.

Why not try
CALL "C:\bat1.bat"
CALL "C:\bat2.bat"
\last.bat
and avoid Powersmell altogether?

Related

Using a cmd command in powershell script?

I am trying to make a powershell script that automatically sets up this program and requires to use a cmd command to run do it.
I know that the cmd command work and I tried cmd.exe.
cmd.exe /c "java -jar fitnesse-standalone.jar -p 9090"
Error Message:
cmd.exe : The term 'cmd.exe' is not recognized
You can use start-process
Start-Process -FilePath "cmd.exe" -ArgumentList '/c "java -jar fitnesse-standalone.jar -p 9090"'
The /c and everything following it is just part of the one set of parameters being passed to cmd. If you want powershell to wait for the the Java app to close, add the -wait parameter.
There's also no real need to use CMD at all in this case (since your giving it /c to exit immediately anyway), you can call java directly:
Start-Process -FilePath "java.exe" -ArgumentList '-jar fitnesse-standalone.jar -p 9090'
and it should be the same.
Note: you'll need Java in your path or the current working directory, and fitnesse-standalone.jar in the current directory for either of these to work.

In powershell after changing to cmd unable to call a bat file through a batch script file

I created a bat file to setup my workspace by changing the directory to the workspace directory and calling the setupEnv.bat file. But while I'm executing the below bat file in PowerShell, the instructions after cmd are not executing.
I need to call the setupEnv.bat file in cmd. If I remove the cmd it will work fine. But I want call the setupEnv.bat on cmd not in PowerShell.
D:
cd D:\WorkSpace\
cmd
call setupEnv.bat
echo "Setup Completed"
After calling the setupEnv.bat and calling cmd, will it keep all the environment variable setup in the PowerShell ?
This article addresses your exact scenario:
Windows IT Pro - Take Charge of Environment Variables in PowerShell
The reason the variables disappear is that a .bat or .cmd runs in a separate cmd.exe process (when the process terminates, you lose the variables).
The article presents a PowerShell function you can use to run a .bat or .cmd script and retain the environment variables it sets:
# Invokes a Cmd.exe shell script and updates the environment.
function Invoke-CmdScript {
param(
[String] $scriptName
)
$cmdLine = """$scriptName"" $args & set"
& $Env:SystemRoot\system32\cmd.exe /c $cmdLine |
Select-String '^([^=]*)=(.*)$' |
ForEach-Object {
$varName = $_.Matches[0].Groups[1].Value
$varValue = $_.Matches[0].Groups[2].Value
Set-Item Env:$varName $varValue
}
}
The article also has a couple of functions for setting and restoring environment variable values in PowerShell.
try Something like this into your PowerShell script:
& start "C:\Temp\test.bat"
After calling the setupEnv.bat and calling cmd, will it keep all the environment variable setup in the PowerShell?
No. Any legacy commands invoked from PowerShell will run in a separate (child) process.
Proof:
wmic process where "name='powershell.exe' or name='cmd.exe'" get CommandLine, name, ParentProcessId, ProcessId /Value
Add above line to your batch-file, e.g. to setupEnv.bat and call it from powershell
either directly, e.g. D:\bat\setupEnv.bat,
or using & call operator & D:\bat\setupEnv.bat,
or . dot sourced & D:\bat\setupEnv.bat,
or modify all above methods calling cmd as
cmd /D /C D:\bat\setupEnv.bat, or
& cmd /D /C D:\bat\setupEnv.bat, or
. cmd /D /C D:\bat\setupEnv.bat.
Result is always the same or at least very alike:
PS D:\PShell> D:\bat\setupEnv.bat
"Setup Completed"
CommandLine="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
Name=powershell.exe
ParentProcessId=4280
ProcessId=6396
CommandLine=C:\Windows\system32\cmd.exe /c ""D:\bat\setupEnv.bat""
Name=cmd.exe
ParentProcessId=6396
ProcessId=4116
Paraphrased from this Foredecker's answer to similar question:
While a child process can inherit the current environment variables,
working directory etc. from parent one, there is no supported way for
a child process to reach back to the parent process and change the
parent's environment.
Solution: call Powershell from cmd/batch script rather than vice versa

call bat file in new windows from powershell

I want to launch a bat file in new window/independent instance from within powershell.
the bat file will take a computername from powershell
e.g.
mybat.bat $thiscomputer
Ive tried
start mybat.bat $thiscomputer
start "cmd /c" mybat.bat $thiscomputer
start /k mybat.bat $thiscomputer
start-process mybat.bat $thiscomputer
ordinarily the bat does run but stays within the powershell script.
I need the powershell script to launch the bat in anew window and loop back to the beginning.
Thanks
Confuseis
Start-Process runs console programs in a new window by default. The only problem with your syntax is that you need to use the -ArgumentList parameter to pass arguments to the program you've started. PowerShell doesn't let you simply list them after the program name.
PS> Start-Process $env:comspec -ArgumentList "mybat.bat $thiscomputer"
or, in shorter form,
PS> start mybat.bat -a $thiscomputer
-ArgumentList only takes one string, so if you have multiple arguments to pass, then you need to put them all into one string before passing to Start-Process.

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

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

Resources