繁体   English   中英

在批处理中使用Powershell变量

[英]Use Powershell variable in Batch

@echo off
Powershell.exe -executionpolicy remotesigned -File C:\Users\aborgetti\Desktop\getdate.ps1

FOR /F "usebackq delims=" %%v IN ('powershell -noprofile -File C:\Users\aborgetti\Desktop\getdate.ps1') DO set "d=%%v"

echo %d%

这是我感到困惑的部分...不确定如何将Powershell中的变量设置为.bat文件中的变量。

 FOR /F "usebackq delims=" %%v IN ('powershell -noprofile -File C:\Users\aborgetti\Desktop\getdate.ps1') DO set "d=%%v"

任何帮助将非常感激。

BTW

$ d变量用于名为Bluezone FTP 3.2的FTP程序的一组初始命令中

getdate.ps1看起来像这样:

$a = Get-Date
$b = $a.ToString('MMddyy')
write-host $b

您的powershell脚本需要输出值,而不是将其存储在变量中。 @zdan在一个不同的SO问题中的回答启发,提供了两种不同的解决方案(未经测试):

FOR /F "usebackq delims=" %%v IN (`powershell -noprofile "& { (get-date).ToString('MMddyy') }"`) DO set "d=%%v"

要么:

getdate.ps1:

$a = Get-Date
$b = $a.ToString('MMddyy')
$b

批量:

FOR /F "usebackq delims=" %%v IN (`powershell -noprofile -File "C:\Users\ab\Desktop\getdate.ps1"`) DO set "d=%%v"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM