Powershell循环抛出MissingVariableNameAfterForeach

问题描述 投票:0回答:1

为什么出现以下错误:“ MissingVariableNameAfterForeach”:

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "foreach($file in Get-ChildItem C:){((Get-Date)-$file.LastWriteTime).ToString('dd')}"

该命令应该打印出从今天开始在C:\中写入最后一个文件/文件夹的日期。

powershell cmd foreach
1个回答
0
投票

从帮助文本:

PS C:\> powershell.exe /?
If the value of Command is a script block, the script block must be enclosed
in braces ({}). You can specify a script block only when running PowerShell.exe
in Windows PowerShell.
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command {Get-ChildItem C: | ForEach-Object{($_.LastWriteTime).ToString('dd')}}
© www.soinside.com 2019 - 2024. All rights reserved.