Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a returned value from powershell and get it in a batch file?

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%

like image 296
Yam Avatar asked Dec 24 '15 10:12

Yam


People also ask

How do I return a value from a PowerShell script?

The return keyword exits a function, script, or script block. It can be used to exit a scope at a specific point, to return a value, or to indicate that the end of the scope has been reached.

What is %% A in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is %% g'in batch file?

%%parameter : A replaceable parameter: in a batch file use %%G (on the command line %G) FOR /F processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'.

Is PowerShell compatible with batch?

PowerShell supports batch files and command-line tools. Moving to PowerShell doesn't mean you have to abandon batch files or command-line tools. Old batch files will still run in PowerShell, and command-line tools still work just fine. PowerShell's script language is a real programming language.


3 Answers

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%
like image 58
SachaDee Avatar answered Oct 09 '22 09:10

SachaDee


powershell "&{.\test.ps1 %* ;exit $LastExitCode}" set code=%errorlevel%
like image 5
Yam Avatar answered Oct 09 '22 08:10

Yam


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.

like image 2
Akram Alhinnawi Avatar answered Oct 09 '22 07:10

Akram Alhinnawi