批处理从powershell中获取返回的值

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

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

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

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

=======================================================================================

Stack Overflow用户

回答已采纳

发布于 2016-01-20 14:07:35

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

Stack Overflow用户

发布于 2015-12-24 17:10:50

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

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

Stack Overflow用户

发布于 2017-04-26 22:43:45

我知道现在回答这个问题有点晚了,但我想尝试一下,以防有人需要更详细的解决方案。所以,它开始了。

我创建了一个批处理函数,它将为您执行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是打开还是关闭,这意味着它是打开还是关闭,所以我们可以这样做:

 :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是启动或下降,如果机器是关闭的。

 

 

出处:https://cloud.tencent.com/developer/ask/sof/112770969

=======================================================================================

再提供一个,通过powershell根据所在区域获取星期几,并在批处理中根据星期几进一步判断和执行

@echo off
for /f "delims=" %%i in ('powershell -Command "$d = Get-Date; $culture = [System.Globalization.CultureInfo]::CurrentCulture; $dayOfWeek = $culture.DateTimeFormat.GetDayName($d.DayOfWeek); echo $dayOfWeek"') do set wd=%%i
echo %wd%

 

出处:https://www.cnblogs.com/mq0036/p/3534311.html

posted on   jack_Meng  阅读(14)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
历史上的今天:
2023-03-03 Linux修改主机名,用户名+日期
2023-03-03 命令行设置修改IP地址DNS服务器地址
2021-03-03 Java多线程Thread/Runnable/Callable之间的区别
2016-03-03 系列文章--Node.js学习笔记系列
2016-03-03 系列文章--8天学通MongoDB
2015-03-03 解决----Word无法创建工作文件,请检查临时环境变量
2014-03-03 VC中Error spawning cl.exe错误的解决方法.

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏

主题色彩