8

I'm trying to execute a powershell from a batch file with the commande: Powershell .\nameoffile.ps1

The PowerShell returns some values 1, 4, 0 and -1 . How can I get these values from the batch? When I use %errorlevel% it only returns 0 (which means that the script is okay). I have also tried using the Exit command in PowerShell (Exit 4) but It does not work. Can you help me?

EDIT I have found a solution if someone is interested.

powershell "&{.\test.ps1 %* ;exit $LastExitCode}" set code=%errorlevel%

Ajay
  • 18,086
  • 12
  • 59
  • 105
Yam
  • 177
  • 1
  • 2
  • 8

5 Answers5

17

If you need to use this value in your bat environment use FOR /F :

@echo off
for /f "delims=" %%a in ('powershell .\test.ps1') do Set "$Value=%%a"

Echo Value received from Powershell : %$Value%
SachaDee
  • 9,245
  • 3
  • 23
  • 33
  • Hi Sacha, That's will work but it will not log the powershell file. It will be executed in background. – Yam Dec 31 '15 at 10:17
6
powershell "&{.\test.ps1 %* ;exit $LastExitCode}" set code=%errorlevel%
Yam
  • 177
  • 1
  • 2
  • 8
2

I know it's a little bit late to answer this question, but I would like to give it a try just in case any one needs more detailed solution. So, here it goes.

I created a batch function that would execute ps script for you and return a value, something like this:

:: A function that would execute powershell script and return a value from it.
:: <PassPSCMD> pass the powreshell command, notice that you need to add any returning value witth Write-Host
:: <RetValue> the returned value
:RunPS <PassPSCMD> <RetValue>
  for /F "usebackq tokens=1" %%i in (`Powershell %1`) do set returnValue=%%i
  set "%2=%returnValue%"
Goto:eof
:: End of :RunPS function

Now, as an example to use it:

set psCmd="&{ Write-Host 'You got it';}"
call :RunPS %psCmd% RetValue
echo %RetValue%

This will display on console screen You got it

As a more complicated example, I would add:

Let's assume that we want to check if a VM is Up or Down, meaning if it's powered on or off, so we can do the following:

 :CheckMachineUpOrDown <returnResult> <passedMachineName>
   set userName=vCenterAdministratorAccount
   set passWord=vCenterAdminPW
   set vCenterName=vcenter.somedmain.whatever
   set psCmd="&{Add-PSSnapin VMware.VimAutomation.Core; Connect-VIServer -server %%vCenterName%% -User %userName% -Password %passWord%; $vmServer = Get-VM %2;Write-Host ($vmServer.PowerState -eq 'PoweredOn')}"

   call :RunPS %psCmd% RetValue
   if "%RetValue%" EQU "True" (set "%1=Up") else (set "%1=Down")
 Goto:eof

:: A function that would execute powershell script and return a value from it.
:: <PassPSCMD> pass the powreshell command, notice that you need to add any returning value witth Write-Host
:: <RetValue> the returned value
:RunPS <PassPSCMD> <RetValue>
  for /F "usebackq tokens=1" %%i in (`Powershell %1`) do set returnValue=%%i
  set "%2=%returnValue%"
  Goto:eof
:: End of :RunPS function

Now, how to use :CheckMachineUpOrDown function?

just follow this example:

set Workstation=MyVMName
call :CheckMachineUpOrDown VMStatus %Workstation%
echo %VMStatus%

This will display Up if the VM is Powered On or Down if the machine is Off.

abhi
  • 1,760
  • 1
  • 24
  • 40
  • Without an additional function, as in the example with VM, Powershell does not return control. When you insert an intermediate function, everything is OK.:SetCmd set psCmd="&{Write-Host You_got_it;Break Script}" call :RunPS %psCmd% RetValue Goto:eof – Garric May 08 '20 at 16:54
  • Thanks. I was looking for an example of working with Powershell without additional .ps1 files for a very long time. – Garric May 08 '20 at 17:08
  • https://newbedev.com/how-to-get-a-returned-value-from-powershell-and-get-it-in-a-batch-file – Ibrahim Mezouar Nov 11 '21 at 10:40
0

What about something like this?

test.bat

@echo off
powershell .\test.ps1 >output.log
type output.log

It just redirects the output of the powershell script to a text file and then outputs the contents of the text file to the console.

Here is my test.ps1 file

Write-Output "Hello World"
Exit

And here is the output:

C:\temp\batchtest>test.bat

Hello World

C:\temp\batchtest>

  • Hi Scott thanks for your comment. But I don't want to create a new file. I have found a method, I will refresh my post – Yam Dec 31 '15 at 10:16
-1

You can use tee-object.

This will display the returned value in host console.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Chand
  • 300
  • 3
  • 13