【问题标题】:How to get a returned value from powershell and get it in a batch file?如何从 powershell 获取返回值并将其获取到批处理文件中?
【发布时间】:2015-12-24 10:37:23
【问题描述】:

我正在尝试使用命令从批处理文件执行 powershell: Powershell .\nameofile.ps1

PowerShell 返回一些值 1、4、0 和 -1 。如何从批次中获取这些值?当我使用 %errorlevel% 时,它只返回 0(这意味着脚本没问题)。我也尝试在 PowerShell(退出 4)中使用 Exit 命令,但它不起作用。 你能帮帮我吗?

编辑 如果有人感兴趣,我已经找到了解决方案。

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

【问题讨论】:

    标签: powershell batch-file


    【解决方案1】:

    如果您需要在bat 环境中使用此值,请使用FOR /F

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

    【讨论】:

    • 嗨,Sacha,这会起作用,但它不会记录 powershell 文件。它将在后台执行。
    【解决方案2】:
    powershell "&{.\test.ps1 %* ;exit $LastExitCode}" set code=%errorlevel%
    

    【讨论】:

    • “表达式或语句中出现意外的标记‘set’”
    【解决方案3】:

    这样的事情呢?

    test.bat

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

    它只是将powershell脚本的输出重定向到一个文本文件,然后将文本文件的内容输出到控制台。

    这是我的 test.ps1 文件

    Write-Output "Hello World"
    Exit
    

    这是输出:

    C:\temp\batchtest>test.bat

    你好世界

    C:\temp\batchtest>

    【讨论】:

    • 嗨,斯科特,感谢您的评论。但我不想创建一个新文件。我找到了一个方法,我会刷新我的帖子
    【解决方案4】:

    我知道现在回答这个问题有点晚了,但我想试一试,以防万一有人需要更详细的解决方案。所以,就这样吧。

    我创建了一个批处理函数,它会为您执行 ps 脚本并返回一个值,如下所示:

    :: 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
    

    现在,作为一个使用它的例子:

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

    这将显示在控制台屏幕上你知道了

    作为一个更复杂的例子,我会补充:

    假设我们要检查 VM 是 Up 还是 Down,这意味着它是打开还是关闭,所以我们可以执行以下操作:

     :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
    

    现在,如何使用 :CheckMachineUpOrDown 函数?

    只要按照这个例子:

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

    如果 VM 开机,则显示 Up;如果机器关闭,则显示 Down。

    【讨论】:

    • 如果没有附加功能,如 VM 示例所示,Powershell 不会返回控制权。插入中间函数时,一切正常。:SetCmd set psCmd="&{Write-Host You_got_it;Break Script}" call :RunPS %psCmd% RetValue Goto:eof
    • 谢谢。很长一段时间以来,我一直在寻找一个在没有额外 .ps1 文件的情况下使用 Powershell 的示例。
    【解决方案5】:

    你可以使用 tee-object。

    这将在主机控制台中显示返回值。

    【讨论】:

      猜你喜欢
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多