我收到消息“错误:“|” 当我运行以下代码时,此时批处理脚本出乎意料:
@set CheckAccount=FOR /F "tokens=3" %A IN ('net user UserName /domain^|Findstr /ic: "active"') do SET Active=%A
%CheckAccount%
| was unexpected at this time.
Run Code Online (Sandbox Code Playgroud)
即使我更改"active"为active. 如果CheckAccount没有收到该错误消息,我似乎无法回声。
由于使用了中间变量CheckAccount,因此存在额外的解析级别。这需要将问题字符转义两次。
@set CheckAccount=FOR /F "tokens=3" %A IN ('net user UserName /domain^^^|Findstr /ic:"active"') do SET Active=%A
Run Code Online (Sandbox Code Playgroud)
该set命令将解析并存储^^^|为^|into CheckAccount,然后扩展%CheckAccount%将只留下管道|。
/ic:,感谢@Squashman 指出这一点。