gpt4 book ai didi

powershell - 使用powershell批量设置变量

转载 作者:行者123 更新时间:2023-12-03 16:36:24 24 4
gpt4 key购买 nike

我一直在绞尽脑汁想弄清楚这一点。

这段代码

@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,以下代码确实有效并返回正确的响应:
$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%为了稍后在脚本中使用它,但它的行为不像我预期的那样。我确定它很简单,但我现在看不到它是什么。

similar question

最佳答案

这是您应该使用的代码来使用 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 - 使用powershell批量设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59209855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com