本文介绍了使用Powershell批量设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在想办法解决这个问题.
I have been racking my brain trying to figure this out.
此代码
@echo off
powershell $Yesterday = (get-date((get-date).addDays(-1)) -format yyyyMMdd)
echo %Yesterday%
::neither one of these echo anything
@echo off
powershell Set-Variable -Name "Yesterday" -Value (get-date((get-date).addDays(-1)) -format yyyyMMdd)
echo %Yesterday%
都应返回带有昨天日期的响应(格式为yyyMMdd),但是不会.使用powershell,以下代码确实可以工作并返回正确的响应:
should both return a response with yesterdays date (formatted as yyyMMdd), however, they do not. Using powershell, the following code does indeed work and return the correct response:
$Yesterday = (get-date((get-date).addDays(-1)) -format yyyyMMdd)
Write-Host $Yesterday
::Both of these work in powershell
Set-Variable -Name "Yesterday" -Value (get-date((get-date).addDays(-1)) -format yyyyMMdd)
Write-Host $Yesterday
但是,当批量使用时,它不起作用.有什么想法吗?我试图设置变量%Yesterday%
以便稍后在脚本中使用它,但是它的行为不符合我的预期.我敢肯定它很简单,但是现在看不到它是什么.
however it does not work when used in batch. Any ideas why? I am trying to set the variable %Yesterday%
in order to use it later in the script, but its not behaving itself as I expected. I'm sure its something simple, but I'm not seeing what it is right now.
推荐答案
这应该用作使用Powershell
和Batch
@echo off & color 0A
Title Setting a variable in batch using powershell
Set psCmd="get-date((get-date).addDays(-1)) -format yyyyMMdd"
Call :RunPS %psCmd% YesterDay
Echo YesterDay was %YesterDay%
pause & Exit
::----------------------------------------------------------------------
:RunPS <PassPSCMD> <Return value to be set as variable>
for /F "usebackq tokens=*" %%i in (`Powershell %1`) do set "%2=%%i"
Goto:eof
:: End of :RunPS function
::----------------------------------------------------------------------
这篇关于使用Powershell批量设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!