how to run a powershell script and a batch script in a batch script? - powershell

i have controller batch file named oneclick.bat, code as below:
rem batch controller
rem wait for email input for ssh key generation...
rem call copyEnv.bat
call generateSshKey.bat %1
call gitClone.bat
in generateSshKey.bat, i start a powershell script like this:
rem make sure powershell is able to run
powershell.exe set-executionpolicy remotesigned
rem start powershell and add ssh key to the server by ui aotumation
powershell.exe -noe -file SetSSHKeytoServer.ps1
and then gitClone.bat did not run in the command window
how can i get the gitClone.bat run?

When you want to return to a calling script you shouldn't run PowerShell with an option that prevents it from returning. Remove -noe from the last line of generateSshKey.bat.
-NoExit
Does not exit after running startup commands.

As Ansgar Wiechers pointed out, the -noe (short for -NoExit) is keeps the PowerShell session open, so generateSshKey.bat doesn't exit, and oneclick.bat doesn't get past the line that called it.
If you specifically want the powershell session that runs SetSSHKeytoServer.ps1 to remain active (since you used the -noe switch the second time but not the first time, I'm inferring that this was deliberate), you could change
call generateSshKey.bat %1
to
start generateSshKey.bat %1
Note that this means that oneclick.bat won't wait for generateSshKey.bat to finish before calling gitClone.bat, so if you need to run them in sequence, this won't work. It will work as long as it's okay for gitClone.bat to start while generateSshKey.bat is still running.

Related

I'm trying to run a powershell script from a batch file batch file but it appears to terminate the batch file

I have a PowerShell script that I'm trying to call in a batch file. The Powershell script renews some credentials used by a subsequent .exe file, which expire after an hour. I would then like to kill the PowerShell script, restart the PowerShell script, and restart the .exe. Here's a simple example that reproduces my problem:
Test.bat
::#echo off
title Started at %time%
pause
Call CallPSWrapper.bat
::Never hits this pause
pause
echo call the .exe here
CallPSWrapper.bat:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle hidden -ExecutionPolicy bypass -NoProfile -File .\MyPowerShell.ps1
MyPowerShell.ps1:
Write-Output "Waiting"
Start-Sleep -Seconds 30
I initially tried calling MyPowerShell.ps1 from test.bat, but tried changing it to being called by an intermediate batch file in the hope that PowerShell would only kill CallPSWrapper.bat.

Automatically execute .cmd file requiring user input

We have a .cmd file we want to be able to execute automatically using Powershell (from TFS Release).
The issue with using start-process in Powershell is that execution gets stuck waiting for user input (Press any key to continue...).
Is there any way that we can pass variables to this call or call it in a different way where we no longer require user input for this .cmd?
Don't use Start-Process.
"`n" | & 'C:\path\to\your.cmd'
Powershell isn't getting stuck, Start-Process is working correctly - it's running the process (assume cmd.exe) and waiting for that process to terminate before continuing.
cmd.exe is prompting for interactive user input (PAUSE) before continuing, again working as you have instructed it to.
The issue is you're running non-interactively, and aren't providing the input required to continue.
You can update the cmd file and add an optional parameter so you can 'skip' the PAUSE when running unattended:
if "%1"=="unattended" goto skippause
pause
:skippause
then run it using unattended:
CMD /c c:\folder\file.cmd unattended

Get bat file to contiune when lauching PowerShell

I have a bat file that is launching a powerShell script. I would like for the bat file to keep moving after it launches the script and not wait for the powerShell script to complete. Every time right now when i launch the powerShell script the bat files waits till the powerShell script finishes before it moves on. Here is how I'm calling my powerShell script:
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "&'C:\Users\sharph\Desktop\test.ps1'"
SS64 'start' help page
You'll want to start it with the start command, like this;
start "" "PowerShell"
This will start a program without waiting for it to close, although that behavior can be re-added with the /w or /wait option. The blank "" is in place of the title, not always needed but generally a safe thing to add.
Perhaps this will work?
start "" "PowerShell" -NoProfile -ExecutionPolicy Bypass -Command "^& 'C:\Users\sharph\Desktop\test.ps1'"
of course, the & had to be delimited to ^&.

How I can I start a powershell command line from a batch file and enforce it to stay open?

How I can I start a powershell session from a batch file so that it starts the Powershell command line (and imports some modules and then) stays open instead being closed once the commands are run in the bat. So that I can continue to work on the PS Shell command line with the modules imported.
Thanks
You could try this.
-noexit will stop powershell for terminating.
start powershell.exe -noexit

How to change the cmd's current-dir using PowerShell?

I read some file using PowerShell, and change current dir accordingly, but all I can do is change the current PowerShell's current dir, not the caller's dir (the cmd.exe environment that called that ps1 file). Things I tried:
powershell ch-dir.ps1 | cd
(won't work, obviously, since CD is internal command)
powershell cd $myDir
(changes current dir in PowerShell, but when script exits, the cmd environment still in original dir)
I really hope I won't need to find the script's caller process (the cmd), and make a change in it's cur-dir by-force... (or even worse - to save the dir I want in some env-var and then cd %my_var% since it would require two lines of command)
I'm not sure if this meets your needs, but if you set it up so that the only output from your powershell script is your desired new working directory, you could do this:
c:\>for /F %i IN ('powershell -noprofile -command "write-output 'c:\users'" ') DO #cd %i
c:\Users>
The cmd prompt is hosting your powershell session, unless you can figure out a way to return an exit code to the prompt that will (on exit code 99999) change directory to (predefined values, switch?). As far as powershell is concerned they're different processes.
Heres a good example for you to try:
Open a cmd prompt.
Open task manager, find cmd.exe
In your cmd prompt type Powershell
View powershell as a different process (check the PID.)
End the powershell process. Watch what happens.
Alternatively, if you need something run from cmd in a specific directory based on logic in your powershell script, you can invoke it with a cmd /c from within Powershell.