批处理从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
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【关注我】。(●'◡'●)
因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/18748600
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
浙公网安备 33010602011771号